# gurdeepsingh-10/sql_agent architecture

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

- Interactive diagram: https://gitdiagram.com/gurdeepsingh-10/sql_agent
- Repository: https://github.com/gurdeepsingh-10/sql_agent

## Overview

This is an AI SQL Agent API: users authenticate, register a database connection, ask questions in natural language, and receive SQL results or a confirmation request. The query route selects a connection, assembles conversation memory, and hands work to the agent workflow; query history and dashboard telemetry support follow-up. The agent directory contains the distinct planning, schema, generation, validation, and execution stages. Several of those modules were not excerpted, so internal workflow links are marked unverified rather than asserted as observed calls.

Main entry points: [`app/core/agent/nodes.py`](https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/core/agent/nodes.py) (Workflow nodes), [`app/api/routes/agent.py`](https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/api/routes/agent.py) (Query API), [`app/main.py`](https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/main.py) (FastAPI app), [`app/database.py`](https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/database.py) (Application database)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["API and identity"]
  node_api_app["FastAPI app<br/>[main.py]"]
  node_auth_routes["Auth routes<br/>[auth.py]"]
  node_auth_deps["JWT auth dependency<br/>[deps.py]"]
  node_security["Token security<br/>[security.py]"]
  node_agent_route["Query API<br/>[agent.py]"]
  node_connection_routes["Connection API<br/>[connections.py]"]
end

subgraph group_agent["SQL agent"]
  node_agent_graph["Agent workflow<br/>[graph.py]"]
  node_agent_nodes["Workflow nodes<br/>[nodes.py]"]
  node_query_planner["Query planning<br/>[query_planner.py]"]
  node_schema_inspector["Schema inspection"]
  node_nlp_processor["Intent processing<br/>[nlp_processor.py]"]
  node_sql_generator["SQL generation<br/>[sql_generator.py]"]
  node_validator["SQL validation<br/>[validator.py]"]
  node_sql_guard["SQL risk guard<br/>[sql_guard.py]"]
  node_executor["SQL execution<br/>[executor.py]"]
  node_memory["Conversation memory<br/>[memory_manager.py]"]
  node_context_pruner["Context pruning<br/>[context_pruner.py]"]
  node_semantic_cache["Semantic cache<br/>[semantic_cache.py]"]
end

subgraph group_connections["Connections and storage"]
  node_connection_manager["Dynamic DB connections"]
  node_app_database[("Application database<br/>[database.py]")]
  node_user_model[("User records<br/>[user.py]")]
  node_connection_model[("Saved connections<br/>[user_connection.py]")]
end

subgraph group_analytics["History and analytics"]
  node_dashboard_route["Dashboard API<br/>[dashboard.py]"]
  node_history_model[("Query history<br/>[query_history.py]")]
end

node_user(("User"))
node_external_db[("User database")]
node_llm_provider["Groq language model"]

node_user -->|"uses"| node_api_app
node_api_app -->|"registers routes"| node_auth_routes
node_api_app -->|"registers routes"| node_agent_route
node_api_app -->|"registers routes"| node_connection_routes
node_api_app -->|"registers routes"| node_dashboard_route
node_auth_routes -->|"hashes and signs"| node_security
node_auth_deps -->|"decodes token"| node_security
node_auth_deps -->|"looks up user"| node_user_model
node_auth_routes -->|"creates account"| node_user_model
node_agent_route -->|"requires identity"| node_auth_deps
node_agent_route -->|"selects connection"| node_connection_model
node_agent_route -.->|"gets DB engine"| node_connection_manager
node_agent_route -->|"inspects schema"| node_schema_inspector
node_agent_route -->|"assembles context"| node_memory
node_agent_route -.->|"runs workflow"| node_agent_graph
node_agent_graph -.->|"dispatches steps"| node_agent_nodes
node_agent_nodes -.->|"plans query"| node_query_planner
node_agent_nodes -.->|"interprets request"| node_nlp_processor
node_agent_nodes -.->|"generates SQL"| node_sql_generator
node_agent_nodes -.->|"validates SQL"| node_validator
node_agent_nodes -.->|"checks risk"| node_sql_guard
node_agent_nodes -.->|"executes SQL"| node_executor
node_agent_nodes -.->|"trims context"| node_context_pruner
node_agent_nodes -.->|"checks cache"| node_semantic_cache
node_agent_route -.->|"records query"| node_history_model
node_connection_routes -.->|"manages connections"| node_connection_model
node_connection_manager -->|"opens connections"| node_external_db
node_executor -.->|"runs SQL"| node_external_db
node_agent_nodes -.->|"requests completion"| node_llm_provider
node_api_app -->|"initializes tables"| node_app_database
node_dashboard_route -->|"reads analytics"| node_history_model
node_dashboard_route -.->|"reads telemetry"| node_connection_manager
node_user_model -->|"persists records"| node_app_database
node_connection_model -->|"persists records"| node_app_database
node_history_model -->|"persists records"| node_app_database

click node_api_app "https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/main.py"
click node_auth_routes "https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/api/routes/auth.py"
click node_auth_deps "https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/api/deps.py"
click node_security "https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/core/security.py"
click node_agent_route "https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/api/routes/agent.py"
click node_connection_routes "https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/api/routes/connections.py"
click node_dashboard_route "https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/api/routes/dashboard.py"
click node_agent_graph "https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/core/agent/graph.py"
click node_agent_nodes "https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/core/agent/nodes.py"
click node_query_planner "https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/core/agent/query_planner.py"
click node_schema_inspector "https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/core/agent/schema_inspector.py"
click node_nlp_processor "https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/core/agent/nlp_processor.py"
click node_sql_generator "https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/core/agent/sql_generator.py"
click node_validator "https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/core/agent/validator.py"
click node_sql_guard "https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/core/sql_guard.py"
click node_executor "https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/core/agent/executor.py"
click node_memory "https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/core/agent/memory_manager.py"
click node_context_pruner "https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/core/agent/context_pruner.py"
click node_semantic_cache "https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/core/semantic_cache.py"
click node_connection_manager "https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/core/connection_manager.py"
click node_app_database "https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/database.py"
click node_user_model "https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/models/user.py"
click node_connection_model "https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/models/user_connection.py"
click node_history_model "https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/models/query_history.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_api_app,node_auth_routes,node_auth_deps,node_security,node_agent_route,node_connection_routes,node_user toneBlue
class node_agent_graph,node_agent_nodes,node_query_planner,node_schema_inspector,node_nlp_processor,node_sql_generator,node_validator,node_sql_guard,node_executor,node_memory,node_context_pruner,node_semantic_cache,node_external_db toneAmber
class node_connection_manager,node_app_database,node_user_model,node_connection_model toneMint
class node_dashboard_route,node_history_model toneRose
class node_llm_provider toneIndigo
```

## Components

### API and identity

- [FastAPI app](https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/main.py): `app/main.py`
- [Auth routes](https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/api/routes/auth.py): `app/api/routes/auth.py`
- [JWT auth dependency](https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/api/deps.py): `app/api/deps.py`
- [Token security](https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/core/security.py): `app/core/security.py`
- [Query API](https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/api/routes/agent.py): `app/api/routes/agent.py`
- [Connection API](https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/api/routes/connections.py): `app/api/routes/connections.py`

### SQL agent

- [Agent workflow](https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/core/agent/graph.py): `app/core/agent/graph.py`
- [Workflow nodes](https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/core/agent/nodes.py): `app/core/agent/nodes.py`
- [Query planning](https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/core/agent/query_planner.py): `app/core/agent/query_planner.py`
- [Schema inspection](https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/core/agent/schema_inspector.py): `app/core/agent/schema_inspector.py`
- [Intent processing](https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/core/agent/nlp_processor.py): `app/core/agent/nlp_processor.py`
- [SQL generation](https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/core/agent/sql_generator.py): `app/core/agent/sql_generator.py`
- [SQL validation](https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/core/agent/validator.py): `app/core/agent/validator.py`
- [SQL risk guard](https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/core/sql_guard.py): `app/core/sql_guard.py`
- [SQL execution](https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/core/agent/executor.py): `app/core/agent/executor.py`
- [Conversation memory](https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/core/agent/memory_manager.py): `app/core/agent/memory_manager.py`
- [Context pruning](https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/core/agent/context_pruner.py): `app/core/agent/context_pruner.py`
- [Semantic cache](https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/core/semantic_cache.py): `app/core/semantic_cache.py`

### Connections and storage

- [Dynamic DB connections](https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/core/connection_manager.py): `app/core/connection_manager.py`
- [Application database](https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/database.py): `app/database.py`
- [User records](https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/models/user.py): `app/models/user.py`
- [Saved connections](https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/models/user_connection.py): `app/models/user_connection.py`

### History and analytics

- [Dashboard API](https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/api/routes/dashboard.py): `app/api/routes/dashboard.py`
- [Query history](https://github.com/gurdeepsingh-10/sql_agent/blob/master/app/models/query_history.py): `app/models/query_history.py`

### Other components

- **User**
- **User database**
- **Groq language model**

## Connections

- User → FastAPI app: uses (evidence: `app/main.py`)
- FastAPI app → Auth routes: registers routes (evidence: `app/main.py`)
- FastAPI app → Query API: registers routes (evidence: `app/main.py`)
- FastAPI app → Connection API: registers routes (evidence: `app/main.py`)
- FastAPI app → Dashboard API: registers routes (evidence: `app/main.py`)
- Auth routes → Token security: hashes and signs (evidence: `app/api/routes/auth.py`)
- JWT auth dependency → Token security: decodes token (evidence: `app/api/deps.py`)
- JWT auth dependency → User records: looks up user (evidence: `app/api/deps.py`)
- Auth routes → User records: creates account (evidence: `app/api/routes/auth.py`)
- Query API → JWT auth dependency: requires identity (evidence: `app/api/routes/agent.py`)
- Query API → Saved connections: selects connection (evidence: `app/api/routes/agent.py`)
- Query API → Dynamic DB connections: gets DB engine (evidence: `app/api/routes/agent.py`)
- Query API → Schema inspection: inspects schema (evidence: `app/api/routes/agent.py`)
- Query API → Conversation memory: assembles context (evidence: `app/api/routes/agent.py`)
- Query API → Agent workflow: runs workflow (evidence: `app/api/routes/agent.py`)
- Agent workflow → Workflow nodes: dispatches steps
- Workflow nodes → Query planning: plans query
- Workflow nodes → Intent processing: interprets request
- Workflow nodes → SQL generation: generates SQL
- Workflow nodes → SQL validation: validates SQL
- Workflow nodes → SQL risk guard: checks risk
- Workflow nodes → SQL execution: executes SQL
- Workflow nodes → Context pruning: trims context
- Workflow nodes → Semantic cache: checks cache
- Query API → Query history: records query (evidence: `app/api/routes/agent.py`)
- Connection API → Saved connections: manages connections
- Dynamic DB connections → User database: opens connections (evidence: `app/core/connection_manager.py`)
- SQL execution → User database: runs SQL
- Workflow nodes → Groq language model: requests completion
- FastAPI app → Application database: initializes tables (evidence: `app/main.py`)
- Dashboard API → Query history: reads analytics (evidence: `app/api/routes/dashboard.py`)
- Dashboard API → Dynamic DB connections: reads telemetry (evidence: `app/api/routes/dashboard.py`)
- User records → Application database: persists records (evidence: `app/models/user.py`)
- Saved connections → Application database: persists records (evidence: `app/models/user_connection.py`)
- Query history → Application database: persists records (evidence: `app/models/query_history.py`)
