# anshlambagit/ai_data_agent architecture

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

- Interactive diagram: https://gitdiagram.com/anshlambagit/ai_data_agent
- Repository: https://github.com/anshlambagit/ai_data_agent

## Overview

This is a LangGraph-based natural-language data agent. Users submit a question to the router, which dispatches SQL requests to a safety-checked database-analysis workflow or ETL requests to an extraction/transformation agent. The SQL path gathers schema context, generates and judges SQL, then executes or cancels it; the ETL path invokes tools for API extraction or file transformation. The graph includes the shared state model and LLM selector. Database utility internals were not sampled, so their connection to PostgreSQL is documented rather than source-verified.

Main entry points: [`utils/llm_pick.py`](https://github.com/anshlambagit/ai_data_agent/blob/main/utils/llm_pick.py) (LLM selector), [`agents/data_agent.py`](https://github.com/anshlambagit/ai_data_agent/blob/main/agents/data_agent.py) (Query router), [`agents/sql_analyst.py`](https://github.com/anshlambagit/ai_data_agent/blob/main/agents/sql_analyst.py) (SQL safety judge), [`utils/etl_tools.py`](https://github.com/anshlambagit/ai_data_agent/blob/main/utils/etl_tools.py) (ETL toolkit)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_orchestration["Agent orchestration"]
  node_entry["CLI entry<br/>[main.py]"]
  node_router["Query router<br/>[data_agent.py]"]
  node_sql_dispatch["SQL dispatch<br/>[data_agent.py]"]
  node_etl_dispatch["ETL dispatch<br/>[data_agent.py]"]
end

subgraph group_sql["SQL analysis"]
  node_sql_curate["Question curation<br/>[sql_analyst.py]"]
  node_sql_context["Schema context<br/>[sql_analyst.py]"]
  node_sql_generate["SQL generation<br/>[sql_analyst.py]"]
  node_sql_judge["SQL safety judge<br/>[sql_analyst.py]"]
  node_sql_cancel["Unsafe query response<br/>[sql_analyst.py]"]
  node_sql_execute["Query execution<br/>[sql_analyst.py]"]
  node_sql_answer["Answer generation<br/>[sql_analyst.py]"]
end

subgraph group_etl["ETL processing"]
  node_etl_agent["ETL agent<br/>[etl_analyst.py]"]
  node_etl_tools["ETL toolkit<br/>[etl_tools.py]"]
end

subgraph group_shared["Shared services"]
  node_state["Agent state schemas<br/>[schema.py]"]
  node_llm_selector["LLM selector<br/>[llm_pick.py]"]
  node_database["Database utility<br/>[database.py]"]
end

node_user(("User"))
node_postgres[("PostgreSQL")]
node_api["Data API"]
node_files["Input and output files"]
node_llm_service["LLM providers"]

node_user -->|"submits query"| node_entry
node_entry -->|"invokes"| node_router
node_router -->|"routes SQL"| node_sql_dispatch
node_router -->|"routes ETL"| node_etl_dispatch
node_router -->|"uses state"| node_state
node_router -->|"selects router model"| node_llm_selector
node_llm_selector -->|"configures provider client"| node_llm_service
node_sql_dispatch -->|"invokes SQL agent"| node_sql_curate
node_etl_dispatch -->|"invokes ETL agent"| node_etl_agent
node_sql_curate -->|"passes curated query"| node_sql_context
node_sql_curate -->|"selects model"| node_llm_selector
node_sql_context -->|"fetches schema"| node_database
node_sql_context -->|"passes prompt context"| node_sql_generate
node_sql_generate -->|"selects model"| node_llm_selector
node_sql_generate -->|"passes generated SQL"| node_sql_judge
node_sql_judge -->|"selects judge model"| node_llm_selector
node_sql_judge -->|"cancels unsafe SQL"| node_sql_cancel
node_sql_judge -->|"dispatches safe SQL"| node_sql_execute
node_sql_execute -->|"executes query"| node_database
node_sql_execute -->|"passes execution result"| node_sql_answer
node_sql_answer -->|"selects response model"| node_llm_selector
node_database -.->|"queries database"| node_postgres
node_etl_agent -->|"selects agent model"| node_llm_selector
node_etl_agent -->|"invokes ETL tools"| node_etl_tools
node_etl_tools -->|"selects transform model"| node_llm_selector
node_etl_tools -->|"requests JSON data"| node_api
node_etl_tools -->|"reads and writes datasets"| node_files

click node_entry "https://github.com/anshlambagit/ai_data_agent/blob/main/main.py"
click node_router "https://github.com/anshlambagit/ai_data_agent/blob/main/agents/data_agent.py"
click node_sql_dispatch "https://github.com/anshlambagit/ai_data_agent/blob/main/agents/data_agent.py"
click node_etl_dispatch "https://github.com/anshlambagit/ai_data_agent/blob/main/agents/data_agent.py"
click node_state "https://github.com/anshlambagit/ai_data_agent/blob/main/Models/schema.py"
click node_llm_selector "https://github.com/anshlambagit/ai_data_agent/blob/main/utils/llm_pick.py"
click node_sql_curate "https://github.com/anshlambagit/ai_data_agent/blob/main/agents/sql_analyst.py"
click node_sql_context "https://github.com/anshlambagit/ai_data_agent/blob/main/agents/sql_analyst.py"
click node_sql_generate "https://github.com/anshlambagit/ai_data_agent/blob/main/agents/sql_analyst.py"
click node_sql_judge "https://github.com/anshlambagit/ai_data_agent/blob/main/agents/sql_analyst.py"
click node_sql_cancel "https://github.com/anshlambagit/ai_data_agent/blob/main/agents/sql_analyst.py"
click node_sql_execute "https://github.com/anshlambagit/ai_data_agent/blob/main/agents/sql_analyst.py"
click node_sql_answer "https://github.com/anshlambagit/ai_data_agent/blob/main/agents/sql_analyst.py"
click node_database "https://github.com/anshlambagit/ai_data_agent/blob/main/utils/database.py"
click node_etl_agent "https://github.com/anshlambagit/ai_data_agent/blob/main/agents/etl_analyst.py"
click node_etl_tools "https://github.com/anshlambagit/ai_data_agent/blob/main/utils/etl_tools.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_entry,node_router,node_sql_dispatch,node_etl_dispatch,node_user toneBlue
class node_sql_curate,node_sql_context,node_sql_generate,node_sql_judge,node_sql_cancel,node_sql_execute,node_sql_answer,node_postgres toneAmber
class node_etl_agent,node_etl_tools,node_api toneMint
class node_state,node_llm_selector,node_database toneRose
class node_files,node_llm_service toneIndigo
```

## Components

### Agent orchestration

- [CLI entry](https://github.com/anshlambagit/ai_data_agent/blob/main/main.py): `main.py`
- [Query router](https://github.com/anshlambagit/ai_data_agent/blob/main/agents/data_agent.py): `agents/data_agent.py`
- [SQL dispatch](https://github.com/anshlambagit/ai_data_agent/blob/main/agents/data_agent.py): `agents/data_agent.py`
- [ETL dispatch](https://github.com/anshlambagit/ai_data_agent/blob/main/agents/data_agent.py): `agents/data_agent.py`

### SQL analysis

- [Question curation](https://github.com/anshlambagit/ai_data_agent/blob/main/agents/sql_analyst.py): `agents/sql_analyst.py`
- [Schema context](https://github.com/anshlambagit/ai_data_agent/blob/main/agents/sql_analyst.py): `agents/sql_analyst.py`
- [SQL generation](https://github.com/anshlambagit/ai_data_agent/blob/main/agents/sql_analyst.py): `agents/sql_analyst.py`
- [SQL safety judge](https://github.com/anshlambagit/ai_data_agent/blob/main/agents/sql_analyst.py): `agents/sql_analyst.py`
- [Unsafe query response](https://github.com/anshlambagit/ai_data_agent/blob/main/agents/sql_analyst.py): `agents/sql_analyst.py`
- [Query execution](https://github.com/anshlambagit/ai_data_agent/blob/main/agents/sql_analyst.py): `agents/sql_analyst.py`
- [Answer generation](https://github.com/anshlambagit/ai_data_agent/blob/main/agents/sql_analyst.py): `agents/sql_analyst.py`

### ETL processing

- [ETL agent](https://github.com/anshlambagit/ai_data_agent/blob/main/agents/etl_analyst.py): `agents/etl_analyst.py`
- [ETL toolkit](https://github.com/anshlambagit/ai_data_agent/blob/main/utils/etl_tools.py): `utils/etl_tools.py`

### Shared services

- [Agent state schemas](https://github.com/anshlambagit/ai_data_agent/blob/main/Models/schema.py): `Models/schema.py`
- [LLM selector](https://github.com/anshlambagit/ai_data_agent/blob/main/utils/llm_pick.py): `utils/llm_pick.py`
- [Database utility](https://github.com/anshlambagit/ai_data_agent/blob/main/utils/database.py): `utils/database.py`

### Other components

- **User**
- **PostgreSQL**
- **Data API**
- **Input and output files**
- **LLM providers**

## Connections

- User → CLI entry: submits query (evidence: `main.py`)
- CLI entry → Query router: invokes (evidence: `main.py`)
- Query router → SQL dispatch: routes SQL (evidence: `agents/data_agent.py`)
- Query router → ETL dispatch: routes ETL (evidence: `agents/data_agent.py`)
- Query router → Agent state schemas: uses state (evidence: `agents/data_agent.py`)
- Query router → LLM selector: selects router model (evidence: `agents/data_agent.py`)
- LLM selector → LLM providers: configures provider client (evidence: `utils/llm_pick.py`)
- SQL dispatch → Question curation: invokes SQL agent (evidence: `agents/data_agent.py`)
- ETL dispatch → ETL agent: invokes ETL agent (evidence: `agents/data_agent.py`)
- Question curation → Schema context: passes curated query (evidence: `agents/sql_analyst.py`)
- Question curation → LLM selector: selects model (evidence: `agents/sql_analyst.py`)
- Schema context → Database utility: fetches schema (evidence: `agents/sql_analyst.py`)
- Schema context → SQL generation: passes prompt context (evidence: `agents/sql_analyst.py`)
- SQL generation → LLM selector: selects model (evidence: `agents/sql_analyst.py`)
- SQL generation → SQL safety judge: passes generated SQL (evidence: `agents/sql_analyst.py`)
- SQL safety judge → LLM selector: selects judge model (evidence: `agents/sql_analyst.py`)
- SQL safety judge → Unsafe query response: cancels unsafe SQL (evidence: `agents/sql_analyst.py`)
- SQL safety judge → Query execution: dispatches safe SQL (evidence: `agents/sql_analyst.py`)
- Query execution → Database utility: executes query (evidence: `agents/sql_analyst.py`)
- Query execution → Answer generation: passes execution result (evidence: `agents/sql_analyst.py`)
- Answer generation → LLM selector: selects response model (evidence: `agents/sql_analyst.py`)
- Database utility → PostgreSQL: queries database (evidence: `README.md`)
- ETL agent → LLM selector: selects agent model (evidence: `agents/etl_analyst.py`)
- ETL agent → ETL toolkit: invokes ETL tools (evidence: `agents/etl_analyst.py`)
- ETL toolkit → LLM selector: selects transform model (evidence: `agents/etl_analyst.py`)
- ETL toolkit → Data API: requests JSON data (evidence: `utils/etl_tools.py`)
- ETL toolkit → Input and output files: reads and writes datasets (evidence: `utils/etl_tools.py`)
