# asm313/base_code_for_agentic_ai architecture

> Architecture diagram of the GitHub repository asm313/base_code_for_agentic_ai, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/asm313/base_code_for_agentic_ai
- Repository: https://github.com/asm313/base_code_for_agentic_ai

## Overview

1. **Overall shape**
   - This is a **single Python backend service** organized as a layered monolith, not a set of microservices.
   - The runtime app lives under `src/`, while `evals/` is a separate offline evaluation pipeline and `grafana/` + `prometheus/` are deployment-time observability configs.

2. **Request entrypoint and API boundary**
   - `src/main.py` is the FastAPI bootstrap: it wires middleware, CORS, rate limiting, router registration, and startup/shutdown lifecycle.
   - `src/interface/router.py` is the HTTP aggregation layer, and `src/interface/auth.py` plus `src/interface/interaction.py` hold the route handlers for auth and chat/agent interactions.
   - This is the main boundary between external clients and the rest of the system.

3. **Configuration and environment management**
   - `src/config/settings.py` centralizes environment-driven config: model selection, JWT secrets, database connection details, rate limits, and observability settings.
   - The repo is designed to run across development/staging/production with different `.env` files and Docker Compose wiring.

4. **Security and request safeguards**
   - Security is enforced before business logic through `src/system/middleware.py`, `src/system/rate_limit.py`, and `src/utils/sanitization.py`.
   - `src/utils/auth.py` handles JWT minting and verification, while `src/interface/auth.py` uses those utilities to protect user/session operations.
   - The architecture assumes per-request abuse control, input sanitation, and context-aware auth checks.

5. **Data persistence layer**
   - `src/data/models/` contains the persistence entities: `user.py`, `session.py`, `thread.py`, plus shared base/database definitions.
   - `src/data/schemas/` contains Pydantic DTOs for API contracts, keeping external payloads separate from database models.
   - `src/data/db_manager.py` is the persistence boundary: connection/session management and CRUD coordination.
   - `schema.sql` suggests the database can also be provisioned explicitly outside the ORM path.

6. **Conversation and memory model**
   - The app distinguishes between **users**, **chat sessions**, and **LangGraph threads/checkpoints**.
   - `session` is the user-facing conversation container; `thread` is the lower-level state anchor for graph persistence.
   - This separation matters for architecture: user identity, chat history, and agent execution state are not the same thing.

7. **Agent orchestration**
   - `src/agent/workflow.py` is the core agent graph/orchestration layer.
   - `src/agent/prompts/system.md` holds the system prompt as a separate asset.
   - `src/agent/tools/web_search.py` exposes an external tool for the agent, which means the agent can branch into tool execution rather than only generating text.
   - The agent layer is responsible for prompt assembly, context handling, and tool-driven reasoning.

8. **LLM service abstraction**
   - `src/services/llm_provider.py` is the model-facing service boundary.
   - It isolates the rest of the app from direct provider calls, which is where retry/fallback behavior, model choice, and generation settings belong.
   - In the overall flow, the agent does not call the provider directly; it goes through this service layer.

9. **System utilities and observability**
   - `src/system/logs.py` provides structured logging, `src/system/telemetry.py` likely connects tracing/metrics, and `src/system/middleware.py` is where request context is attached.
   - The repo is observability-heavy: the runtime path emits logs/metrics/traces so the agent behavior and system health can be monitored together.
   - This aligns with the included `langfuse`, Prometheus, and Grafana support in the repo.

10. **External services and infrastructure**
   - The main external dependencies that affect architecture are **OpenAI** for LLM inference, **PostgreSQL** for persistence, **Langfuse** for tracing/evaluation, and **DuckDuckGo search** for the web tool.
   - The Docker setup uses a Postgres image with vector support in the README narrative, which implies the database is also part of long-term memory/state infrastructure.
   - `docker-compose.yml`, `Dockerfile`, and the `scripts/` folder show this project is meant to run as a containerized stack with supporting services.

11. **Offline evaluation pipeline**
   - `evals/` is separate from request serving and should be drawn as an asynchronous quality-control subsystem.
   - `evals/evaluator.py`, `evals/helpers.py`, `evals/main.py`, and `evals/metrics/prompts/*.md` implement LLM-as-a-judge scoring over traces.
   - The evaluation loop consumes observability data, computes quality metrics like helpfulness/relevancy/hallucination/toxicity, and reports results back into the monitoring workflow.

12. **Monitoring and dashboards**
   - `prometheus/prometheus.yml` configures scraping, and `grafana/dashboards/` defines dashboards as code.
   - Operationally, metrics flow from the FastAPI service into Prometheus, then into Grafana for visualization.
   - This is separate from app logic, but it is part of the production architecture because it closes the feedback loop on latency, reliability, and agent quality.

13. **Typical end-to-end runtime flow**
   - A client request enters FastAPI → middleware applies logging/rate limiting/metrics → interface route validates auth/session → service layer fetches data and config → agent workflow prepares prompt/state/tools → LLM provider generates a response → results are returned to the client and persisted/traced.
   - Separately, the eval pipeline can read traces and score them offline without being on the request path.

14. **How to draw the diagram**
   - Use one central application box for `src/`.
   - Draw four major internal lanes: **interface/API**, **system/security**, **agent/LLM**, and **data persistence**.
   - Add external boxes for **PostgreSQL**, **OpenAI**, **DuckDuckGo**, **Langfuse**, **Prometheus**, and **Grafana**.
   - Show `evals/` as a sidecar/offline subsystem consuming traces rather than serving live traffic.

Main entry points: [`src/main.py`](https://github.com/asm313/base_code_for_agentic_ai/blob/main/src/main.py) (FastAPI app), [`src/interface/auth.py`](https://github.com/asm313/base_code_for_agentic_ai/blob/main/src/interface/auth.py) (Auth routes), [`src/interface/interaction.py`](https://github.com/asm313/base_code_for_agentic_ai/blob/main/src/interface/interaction.py) (Chat routes), [`src/agent/workflow.py`](https://github.com/asm313/base_code_for_agentic_ai/blob/main/src/agent/workflow.py) (Agent workflow)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime app"]
  node_src_main["FastAPI app<br/>bootstrap<br/>[main.py]"]
  node_interface_router["API router<br/>http boundary<br/>[router.py]"]
  node_interface_auth["Auth routes<br/>endpoint handlers<br/>[auth.py]"]
  node_interface_interaction["Chat routes<br/>endpoint handlers<br/>[interaction.py]"]
  node_system_middleware["Middleware<br/>request guard<br/>[middleware.py]"]
  node_system_rate_limit["Rate limit<br/>abuse control<br/>[rate_limit.py]"]
  node_system_logs["Logging<br/>observability<br/>[logs.py]"]
  node_config_settings["Settings<br/>config<br/>[settings.py]"]
  node_utils_auth["JWT utils<br/>security<br/>[auth.py]"]
  node_utils_sanitization["Sanitization<br/>input safety<br/>[sanitization.py]"]
  node_db_manager["DB manager<br/>persistence boundary<br/>[db_manager.py]"]
  node_data_models[("Models<br/>persistence entities")]
  node_data_schemas["DTOs<br/>api schemas"]
  node_agent_workflow{{"Agent workflow<br/>orchestration<br/>[workflow.py]"}}
  node_agent_prompt["System prompt<br/>prompt asset<br/>[system.md]"]
  node_agent_web_search["Web search<br/>tool<br/>[web_search.py]"]
  node_llm_provider["LLM provider<br/>model service<br/>[llm_provider.py]"]
end

subgraph group_evals["Offline evals"]
  node_eval_main["Eval runner<br/>offline pipeline<br/>[main.py]"]
  node_eval_evaluator{{"Evaluator<br/>judge logic<br/>[evaluator.py]"}}
  node_eval_helpers["Eval helpers<br/>support code<br/>[helpers.py]"]
end

subgraph group_ops["Observability"]
  node_system_telemetry["Telemetry<br/>observability<br/>[telemetry.py]"]
  node_prometheus_cfg[("Prometheus<br/>metrics backend<br/>[prometheus.yml]")]
  node_grafana_dash["Grafana<br/>dashboards"]
end

node_postgres[("PostgreSQL<br/>database")]
node_openai["OpenAI<br/>llm api"]
node_duckduckgo["DuckDuckGo<br/>search api"]
node_langfuse[("Langfuse<br/>trace store")]

node_src_main -->|"registers"| node_interface_router
node_src_main -->|"installs"| node_system_middleware
node_src_main -->|"adds"| node_system_rate_limit
node_src_main -->|"loads"| node_config_settings
node_src_main -->|"initializes"| node_system_telemetry
node_system_middleware -->|"emits"| node_system_logs
node_system_middleware -->|"attaches context"| node_system_telemetry
node_interface_router -->|"routes to"| node_interface_auth
node_interface_router -->|"routes to"| node_interface_interaction
node_interface_auth -->|"verifies"| node_utils_auth
node_interface_auth -->|"sanitizes"| node_utils_sanitization
node_interface_auth -->|"reads/writes"| node_db_manager
node_interface_interaction -->|"sanitizes"| node_utils_sanitization
node_interface_interaction -->|"reads/writes"| node_db_manager
node_interface_interaction -->|"invokes"| node_agent_workflow
node_db_manager -->|"persists"| node_data_models
node_db_manager -->|"stores in"| node_postgres
node_data_models -.->|"schema for"| node_postgres
node_interface_auth -.->|"uses"| node_data_schemas
node_interface_interaction -.->|"uses"| node_data_schemas
node_agent_workflow -->|"loads"| node_agent_prompt
node_agent_workflow -->|"tool use"| node_agent_web_search
node_agent_workflow -->|"calls"| node_llm_provider
node_agent_workflow -->|"fetches context"| node_db_manager
node_llm_provider -->|"requests generation"| node_openai
node_agent_web_search -->|"searches"| node_duckduckgo
node_system_telemetry -->|"traces to"| node_langfuse
node_system_telemetry -->|"exposes metrics"| node_prometheus_cfg
node_prometheus_cfg -->|"feeds"| node_grafana_dash
node_eval_main -->|"runs"| node_eval_evaluator
node_eval_main -->|"uses"| node_eval_helpers
node_eval_main -->|"consumes traces"| node_langfuse
node_eval_evaluator -.->|"judges with"| node_openai
node_eval_evaluator -->|"reports to"| node_langfuse

click node_src_main "https://github.com/asm313/base_code_for_agentic_ai/blob/main/src/main.py"
click node_interface_router "https://github.com/asm313/base_code_for_agentic_ai/blob/main/src/interface/router.py"
click node_interface_auth "https://github.com/asm313/base_code_for_agentic_ai/blob/main/src/interface/auth.py"
click node_interface_interaction "https://github.com/asm313/base_code_for_agentic_ai/blob/main/src/interface/interaction.py"
click node_system_middleware "https://github.com/asm313/base_code_for_agentic_ai/blob/main/src/system/middleware.py"
click node_system_rate_limit "https://github.com/asm313/base_code_for_agentic_ai/blob/main/src/system/rate_limit.py"
click node_system_logs "https://github.com/asm313/base_code_for_agentic_ai/blob/main/src/system/logs.py"
click node_system_telemetry "https://github.com/asm313/base_code_for_agentic_ai/blob/main/src/system/telemetry.py"
click node_config_settings "https://github.com/asm313/base_code_for_agentic_ai/blob/main/src/config/settings.py"
click node_utils_auth "https://github.com/asm313/base_code_for_agentic_ai/blob/main/src/utils/auth.py"
click node_utils_sanitization "https://github.com/asm313/base_code_for_agentic_ai/blob/main/src/utils/sanitization.py"
click node_db_manager "https://github.com/asm313/base_code_for_agentic_ai/blob/main/src/data/db_manager.py"
click node_data_models "https://github.com/asm313/base_code_for_agentic_ai/tree/main/src/data/models"
click node_data_schemas "https://github.com/asm313/base_code_for_agentic_ai/tree/main/src/data/schemas"
click node_agent_workflow "https://github.com/asm313/base_code_for_agentic_ai/blob/main/src/agent/workflow.py"
click node_agent_prompt "https://github.com/asm313/base_code_for_agentic_ai/blob/main/src/agent/prompts/system.md"
click node_agent_web_search "https://github.com/asm313/base_code_for_agentic_ai/blob/main/src/agent/tools/web_search.py"
click node_llm_provider "https://github.com/asm313/base_code_for_agentic_ai/blob/main/src/services/llm_provider.py"
click node_eval_main "https://github.com/asm313/base_code_for_agentic_ai/blob/main/evals/main.py"
click node_eval_evaluator "https://github.com/asm313/base_code_for_agentic_ai/blob/main/evals/evaluator.py"
click node_eval_helpers "https://github.com/asm313/base_code_for_agentic_ai/blob/main/evals/helpers.py"
click node_prometheus_cfg "https://github.com/asm313/base_code_for_agentic_ai/blob/main/prometheus/prometheus.yml"
click node_grafana_dash "https://github.com/asm313/base_code_for_agentic_ai/tree/main/grafana/dashboards"

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_src_main,node_interface_router,node_interface_auth,node_interface_interaction,node_system_middleware,node_system_rate_limit,node_system_logs,node_config_settings,node_utils_auth,node_utils_sanitization,node_db_manager,node_data_models,node_data_schemas,node_agent_workflow,node_agent_prompt,node_agent_web_search,node_llm_provider toneBlue
class node_eval_main,node_eval_evaluator,node_eval_helpers toneAmber
class node_system_telemetry,node_prometheus_cfg,node_grafana_dash toneMint
class node_postgres,node_openai,node_duckduckgo,node_langfuse toneNeutral
```

## Components

### Runtime app

Live FastAPI service under src/

- [FastAPI app](https://github.com/asm313/base_code_for_agentic_ai/blob/main/src/main.py): bootstrap · Wires middleware, routers, and lifecycle · `src/main.py`
- [API router](https://github.com/asm313/base_code_for_agentic_ai/blob/main/src/interface/router.py): http boundary · Aggregates public HTTP routes · `src/interface/router.py`
- [Auth routes](https://github.com/asm313/base_code_for_agentic_ai/blob/main/src/interface/auth.py): endpoint handlers · Login and session/user endpoints · `src/interface/auth.py`
- [Chat routes](https://github.com/asm313/base_code_for_agentic_ai/blob/main/src/interface/interaction.py): endpoint handlers · Conversation and agent interaction endpoints · `src/interface/interaction.py`
- [Middleware](https://github.com/asm313/base_code_for_agentic_ai/blob/main/src/system/middleware.py): request guard · Request context, logging, and pre-processing · `src/system/middleware.py`
- [Rate limit](https://github.com/asm313/base_code_for_agentic_ai/blob/main/src/system/rate_limit.py): abuse control · Per-request throttling and protection · `src/system/rate_limit.py`
- [Logging](https://github.com/asm313/base_code_for_agentic_ai/blob/main/src/system/logs.py): observability · Structured application logs · `src/system/logs.py`
- [Settings](https://github.com/asm313/base_code_for_agentic_ai/blob/main/src/config/settings.py): config · Environment-driven app configuration · `src/config/settings.py`
- [JWT utils](https://github.com/asm313/base_code_for_agentic_ai/blob/main/src/utils/auth.py): security · Token minting and verification · `src/utils/auth.py`
- [Sanitization](https://github.com/asm313/base_code_for_agentic_ai/blob/main/src/utils/sanitization.py): input safety · Input cleanup and validation helpers · `src/utils/sanitization.py`
- [DB manager](https://github.com/asm313/base_code_for_agentic_ai/blob/main/src/data/db_manager.py): persistence boundary · Connection management and CRUD coordination · `src/data/db_manager.py`
- [Models](https://github.com/asm313/base_code_for_agentic_ai/tree/main/src/data/models): persistence entities · User, session, thread, and base database models · `src/data/models`
- [DTOs](https://github.com/asm313/base_code_for_agentic_ai/tree/main/src/data/schemas): api schemas · Pydantic request and response contracts · `src/data/schemas`
- [Agent workflow](https://github.com/asm313/base_code_for_agentic_ai/blob/main/src/agent/workflow.py): orchestration · Prompt assembly, context handling, and tool use · `src/agent/workflow.py`
- [System prompt](https://github.com/asm313/base_code_for_agentic_ai/blob/main/src/agent/prompts/system.md): prompt asset · Default system instructions for the agent · `src/agent/prompts/system.md`
- [Web search](https://github.com/asm313/base_code_for_agentic_ai/blob/main/src/agent/tools/web_search.py): tool · External search tool for agent reasoning · `src/agent/tools/web_search.py`
- [LLM provider](https://github.com/asm313/base_code_for_agentic_ai/blob/main/src/services/llm_provider.py): model service · Provider abstraction for generation calls · `src/services/llm_provider.py`

### Offline evals

Asynchronous quality-control pipeline

- [Eval runner](https://github.com/asm313/base_code_for_agentic_ai/blob/main/evals/main.py): offline pipeline · Entry point for trace-based evaluations · `evals/main.py`
- [Evaluator](https://github.com/asm313/base_code_for_agentic_ai/blob/main/evals/evaluator.py): judge logic · Scores traces with LLM-as-judge metrics · `evals/evaluator.py`
- [Eval helpers](https://github.com/asm313/base_code_for_agentic_ai/blob/main/evals/helpers.py): support code · Shared evaluation utilities · `evals/helpers.py`

### Observability

Metrics, dashboards, and tracing support

- [Telemetry](https://github.com/asm313/base_code_for_agentic_ai/blob/main/src/system/telemetry.py): observability · Tracing and metrics hooks · `src/system/telemetry.py`
- [Prometheus](https://github.com/asm313/base_code_for_agentic_ai/blob/main/prometheus/prometheus.yml): metrics backend · Scrapes service metrics · `prometheus/prometheus.yml`
- [Grafana](https://github.com/asm313/base_code_for_agentic_ai/tree/main/grafana/dashboards): dashboards · Operational dashboards as code · `grafana/dashboards`

### Other components

- **PostgreSQL**: database · Persistent store for users, sessions, and threads
- **OpenAI**: llm api · External model inference provider
- **DuckDuckGo**: search api · External web search backend
- **Langfuse**: trace store · Tracing and eval data backend

## Connections

- FastAPI app → API router: registers
- FastAPI app → Middleware: installs
- FastAPI app → Rate limit: adds
- FastAPI app → Settings: loads
- FastAPI app → Telemetry: initializes
- Middleware → Logging: emits
- Middleware → Telemetry: attaches context
- API router → Auth routes: routes to
- API router → Chat routes: routes to
- Auth routes → JWT utils: verifies
- Auth routes → Sanitization: sanitizes
- Auth routes → DB manager: reads/writes
- Chat routes → Sanitization: sanitizes
- Chat routes → DB manager: reads/writes
- Chat routes → Agent workflow: invokes
- DB manager → Models: persists
- DB manager → PostgreSQL: stores in
- Models → PostgreSQL: schema for
- Auth routes → DTOs: uses
- Chat routes → DTOs: uses
- Agent workflow → System prompt: loads
- Agent workflow → Web search: tool use
- Agent workflow → LLM provider: calls
- Agent workflow → DB manager: fetches context
- LLM provider → OpenAI: requests generation
- Web search → DuckDuckGo: searches
- Telemetry → Langfuse: traces to
- Telemetry → Prometheus: exposes metrics
- Prometheus → Grafana: feeds
- Eval runner → Evaluator: runs
- Eval runner → Eval helpers: uses
- Eval runner → Langfuse: consumes traces
- Evaluator → OpenAI: judges with
- Evaluator → Langfuse: reports to
