# mb-cardoso/expense-agent architecture

> Architecture diagram of the GitHub repository mb-cardoso/expense-agent, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-23). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mb-cardoso/expense-agent
- Repository: https://github.com/mb-cardoso/expense-agent

## Overview

Expense agent is a shared conversational service exposed through a terminal and Streamlit page. A user message is interpreted by the agent, which requests model responses and executes declared expense tools; deletion pauses for user confirmation before changing data. The package also supports alternate model providers, file or MongoDB storage, Langfuse tracing, and a documented similar-ticket example. Edges reflect the shown callers and README-described integrations; the example’s internal implementation was not sampled.

Main entry points: [`expense_agent/tools.py`](https://github.com/mb-cardoso/expense-agent/blob/main/expense_agent/tools.py) (Expense tools), [`expense_agent/llm.py`](https://github.com/mb-cardoso/expense-agent/blob/main/expense_agent/llm.py) (Model adapters), [`expense_agent/agent.py`](https://github.com/mb-cardoso/expense-agent/blob/main/expense_agent/agent.py) (Conversation loop), [`expense_agent/tracing.py`](https://github.com/mb-cardoso/expense-agent/blob/main/expense_agent/tracing.py) (Trace instrumentation)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_interfaces["User interfaces"]
  node_cli["Terminal interface<br/>[cli.py]"]
  node_web["Web interface<br/>[app.py]"]
end

subgraph group_agent["Agent and tools"]
  node_agent_loop["Conversation loop<br/>[agent.py]"]
  node_prompt["Instructions and tags<br/>[prompts.py]"]
  node_tool_dispatch["Expense tools<br/>[tools.py]"]
  node_argument_rules["Tool argument rules<br/>[schemas.py]"]
end

subgraph group_data["Settings and data"]
  node_settings["Runtime settings<br/>[config.py]"]
  node_store_factory["Store selection<br/>[store.py]"]
  node_file_store[("JSON expense store<br/>[store.py]")]
  node_mongo_store[("Mongo expense store<br/>[store.py]")]
end

subgraph group_integrations["External integrations"]
  node_model_adapter["Model adapters<br/>[llm.py]"]
  node_tracing["Trace instrumentation<br/>[tracing.py]"]
  node_similar_example["Similar-ticket example<br/>[similar_tickets.py]"]
end

node_user(("User"))
node_model_provider{{"Gemini or Claude"}}
node_mongo_atlas[("MongoDB Atlas")]
node_langfuse_service{{"Langfuse"}}
node_vector_search[("Atlas Vector Search")]

node_user -->|"enters messages"| node_cli
node_user -->|"enters messages"| node_web
node_cli -->|"calls"| node_agent_loop
node_web -->|"calls"| node_agent_loop
node_cli -->|"creates model"| node_model_adapter
node_web -->|"creates model"| node_model_adapter
node_cli -->|"runs tools"| node_tool_dispatch
node_web -->|"runs tools"| node_tool_dispatch
node_agent_loop -->|"formats messages"| node_prompt
node_agent_loop -->|"sends requests"| node_model_adapter
node_agent_loop -->|"records turns"| node_tracing
node_tool_dispatch -->|"validates arguments"| node_argument_rules
node_tool_dispatch -->|"reads and writes"| node_store_factory
node_tool_dispatch -->|"records tool events"| node_tracing
node_model_adapter -->|"reads provider config"| node_settings
node_model_adapter -->|"records generations"| node_tracing
node_model_adapter -.->|"calls provider"| node_model_provider
node_store_factory -->|"selects backend"| node_settings
node_store_factory -->|"creates fallback"| node_file_store
node_store_factory -->|"creates when configured"| node_mongo_store
node_mongo_store -->|"reads and writes"| node_mongo_atlas
node_tracing -->|"reads trace config"| node_settings
node_tracing -.->|"sends observations"| node_langfuse_service
node_similar_example -->|"uses Gemini embeddings"| node_model_provider
node_similar_example -->|"searches ticket vectors"| node_vector_search
node_vector_search -->|"runs within Atlas"| node_mongo_atlas
node_user -->|"confirms deletion"| node_tool_dispatch

click node_cli "https://github.com/mb-cardoso/expense-agent/blob/main/expense_agent/cli.py"
click node_web "https://github.com/mb-cardoso/expense-agent/blob/main/app.py"
click node_agent_loop "https://github.com/mb-cardoso/expense-agent/blob/main/expense_agent/agent.py"
click node_prompt "https://github.com/mb-cardoso/expense-agent/blob/main/expense_agent/prompts.py"
click node_tool_dispatch "https://github.com/mb-cardoso/expense-agent/blob/main/expense_agent/tools.py"
click node_argument_rules "https://github.com/mb-cardoso/expense-agent/blob/main/expense_agent/schemas.py"
click node_model_adapter "https://github.com/mb-cardoso/expense-agent/blob/main/expense_agent/llm.py"
click node_settings "https://github.com/mb-cardoso/expense-agent/blob/main/expense_agent/config.py"
click node_store_factory "https://github.com/mb-cardoso/expense-agent/blob/main/expense_agent/store.py"
click node_file_store "https://github.com/mb-cardoso/expense-agent/blob/main/expense_agent/store.py"
click node_mongo_store "https://github.com/mb-cardoso/expense-agent/blob/main/expense_agent/store.py"
click node_tracing "https://github.com/mb-cardoso/expense-agent/blob/main/expense_agent/tracing.py"
click node_similar_example "https://github.com/mb-cardoso/expense-agent/blob/main/examples/similar_tickets.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_cli,node_web,node_user toneBlue
class node_agent_loop,node_prompt,node_tool_dispatch,node_argument_rules,node_mongo_atlas,node_vector_search toneAmber
class node_settings,node_store_factory,node_file_store,node_mongo_store toneMint
class node_model_adapter,node_tracing,node_similar_example toneRose
class node_model_provider,node_langfuse_service toneIndigo
```

## Components

### User interfaces

- [Terminal interface](https://github.com/mb-cardoso/expense-agent/blob/main/expense_agent/cli.py): `expense_agent/cli.py`
- [Web interface](https://github.com/mb-cardoso/expense-agent/blob/main/app.py): `app.py`

### Agent and tools

- [Conversation loop](https://github.com/mb-cardoso/expense-agent/blob/main/expense_agent/agent.py): `expense_agent/agent.py`
- [Instructions and tags](https://github.com/mb-cardoso/expense-agent/blob/main/expense_agent/prompts.py): `expense_agent/prompts.py`
- [Expense tools](https://github.com/mb-cardoso/expense-agent/blob/main/expense_agent/tools.py): `expense_agent/tools.py`
- [Tool argument rules](https://github.com/mb-cardoso/expense-agent/blob/main/expense_agent/schemas.py): `expense_agent/schemas.py`

### Settings and data

- [Runtime settings](https://github.com/mb-cardoso/expense-agent/blob/main/expense_agent/config.py): `expense_agent/config.py`
- [Store selection](https://github.com/mb-cardoso/expense-agent/blob/main/expense_agent/store.py): `expense_agent/store.py`
- [JSON expense store](https://github.com/mb-cardoso/expense-agent/blob/main/expense_agent/store.py): `expense_agent/store.py`
- [Mongo expense store](https://github.com/mb-cardoso/expense-agent/blob/main/expense_agent/store.py): `expense_agent/store.py`

### External integrations

- [Model adapters](https://github.com/mb-cardoso/expense-agent/blob/main/expense_agent/llm.py): `expense_agent/llm.py`
- [Trace instrumentation](https://github.com/mb-cardoso/expense-agent/blob/main/expense_agent/tracing.py): `expense_agent/tracing.py`
- [Similar-ticket example](https://github.com/mb-cardoso/expense-agent/blob/main/examples/similar_tickets.py): `examples/similar_tickets.py`

### Other components

- **User**
- **Gemini or Claude**
- **MongoDB Atlas**
- **Langfuse**
- **Atlas Vector Search**

## Connections

- User → Terminal interface: enters messages (evidence: `README.md`)
- User → Web interface: enters messages (evidence: `README.md`)
- Terminal interface → Conversation loop: calls (evidence: `expense_agent/cli.py`)
- Web interface → Conversation loop: calls (evidence: `app.py`)
- Terminal interface → Model adapters: creates model (evidence: `expense_agent/cli.py`)
- Web interface → Model adapters: creates model (evidence: `app.py`)
- Terminal interface → Expense tools: runs tools (evidence: `expense_agent/cli.py`)
- Web interface → Expense tools: runs tools (evidence: `app.py`)
- Conversation loop → Instructions and tags: formats messages (evidence: `expense_agent/agent.py`)
- Conversation loop → Model adapters: sends requests (evidence: `expense_agent/agent.py`)
- Conversation loop → Trace instrumentation: records turns (evidence: `expense_agent/agent.py`)
- Expense tools → Tool argument rules: validates arguments (evidence: `expense_agent/tools.py`)
- Expense tools → Store selection: reads and writes (evidence: `expense_agent/tools.py`)
- Expense tools → Trace instrumentation: records tool events (evidence: `expense_agent/tools.py`)
- Model adapters → Runtime settings: reads provider config (evidence: `expense_agent/llm.py`)
- Model adapters → Trace instrumentation: records generations (evidence: `expense_agent/llm.py`)
- Model adapters → Gemini or Claude: calls provider
- Store selection → Runtime settings: selects backend (evidence: `expense_agent/store.py`)
- Store selection → JSON expense store: creates fallback (evidence: `expense_agent/store.py`)
- Store selection → Mongo expense store: creates when configured (evidence: `expense_agent/store.py`)
- Mongo expense store → MongoDB Atlas: reads and writes (evidence: `expense_agent/store.py`)
- Trace instrumentation → Runtime settings: reads trace config (evidence: `expense_agent/tracing.py`)
- Trace instrumentation → Langfuse: sends observations (evidence: `README.md`)
- Similar-ticket example → Gemini or Claude: uses Gemini embeddings (evidence: `README.md`)
- Similar-ticket example → Atlas Vector Search: searches ticket vectors (evidence: `README.md`)
- Atlas Vector Search → MongoDB Atlas: runs within Atlas (evidence: `README.md`)
- User → Expense tools: confirms deletion (evidence: `README.md`)
