# prakashwebdevx/ai-business-research-agent-backend architecture

> Architecture diagram of the GitHub repository prakashwebdevx/ai-business-research-agent-backend, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/prakashwebdevx/ai-business-research-agent-backend
- Repository: https://github.com/prakashwebdevx/ai-business-research-agent-backend

## Overview

#### Purpose and entry points
AI Business Research Agent answers business questions using internal SQLite data, live web research, or both. CLI entry is `app.py`; dashboard entry is FastAPI `backend/main.py` plus React `frontend/src/main.jsx`. The orchestrator is shared by both interfaces.

#### Agent orchestration and routing
`agent.py` defines `BusinessAgent`, a LangChain/Groq agent that uses centralized instructions to decide whether to invoke SQL, web search, both, or answer directly. `prompts.py` owns agent prompt templates; `tools.py` registers the callable SQL and web adapters. The primary control boundary is orchestrator versus specialized sub-agents.

Paths: `agent.py`, `prompts.py`, `tools.py`

#### Internal-data research pipeline
The SQL tool delegates natural-language database questions to `sql_agent.py`, which uses LangChain SQL tooling to generate and execute read-only queries. Persistent business entities and first-run sample-data initialization are owned by SQLAlchemy in `database.py`; the runtime database is SQLite (`database/employee.db`, generated rather than present in this tree). SQL safety explicitly excludes mutation and schema-changing commands.

Paths: `sql_agent.py`, `database.py`, `schemas.py`

#### External research pipeline
The web tool routes to `web_agent.py`, which calls Tavily Search and uses Groq to summarize results rather than return raw search payloads. This is a service boundary: Tavily provides external retrieval, while Groq powers web synthesis and the central routing/final response process.

Paths: `web_agent.py`, `tools.py`, `config.py`

#### Structured results and API layer
Agent/tool execution is represented by shared result and chart schemas, enabling rich dashboard output beyond plain text. FastAPI exposes chat, session, and export operations; it adapts requests/responses and maintains in-memory conversational sessions. A key concurrency limitation is the shared agent instance with per-session memory resynchronization rather than isolated multi-user execution.

Paths: `schemas.py`, `backend/main.py`, `backend/session_store.py`

#### Dashboard rendering and exports
React/Vite consumes the REST API through `frontend/src/lib/api.js`. `App.jsx` owns routing/layout; `ChatView.jsx` renders interaction flow, while message-level components render SQL tables, charts, and sourced web research. Backend export handling serializes SQL results as CSV, Excel, or JSON.

Paths: `frontend/src/App.jsx`, `frontend/src/components/ChatMessage.jsx`, `frontend/src/lib/api.js`; `backend/export_utils.py`

#### Material runtime and infrastructure
Backend runtime is Python 3.10+ with LangChain, Groq, Tavily, SQLAlchemy/SQLite, FastAPI/Uvicorn, pandas, and openpyxl. Frontend runtime is React 19/Vite, with Tailwind and Recharts. Required external credentials are Groq and Tavily API keys loaded from environment configuration; the Vite development server proxies `/api/*` to FastAPI.

Paths: `requirements.txt`, `frontend/package.json`, `frontend/vite.config.js`

Main entry points: [`agent.py`](https://github.com/prakashwebdevx/ai-business-research-agent-backend/blob/main/agent.py) (BusinessAgent), [`backend/main.py`](https://github.com/prakashwebdevx/ai-business-research-agent-backend/blob/main/backend/main.py) (FastAPI backend), [`tools.py`](https://github.com/prakashwebdevx/ai-business-research-agent-backend/blob/main/tools.py) (Tool registry), [`web_agent.py`](https://github.com/prakashwebdevx/ai-business-research-agent-backend/blob/main/web_agent.py) (Web sub-agent)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_interfaces["Interfaces"]
  node_cli["CLI entry<br/>Python CLI<br/>[app.py]"]
end

subgraph group_agent["Agent Core"]
  node_orchestrator{{"BusinessAgent<br/>LangChain/Groq orchestrator<br/>[agent.py]"}}
  node_prompts["Agent prompts<br/>prompt templates<br/>[prompts.py]"]
  node_tools["Tool registry<br/>callable adapters<br/>[tools.py]"]
  node_schemas["Result schemas<br/>shared result models<br/>[schemas.py]"]
end

subgraph group_internal["Internal Research"]
  node_sql_agent{{"SQL sub-agent<br/>NL-to-SQL<br/>[sql_agent.py]"}}
  node_database[("Business database<br/>SQLAlchemy + SQLite<br/>[database.py]")]
  node_sqlite[("Employee SQLite DB<br/>runtime database")]
end

subgraph group_external["External Research"]
  node_web_agent{{"Web sub-agent<br/>research synthesis<br/>[web_agent.py]"}}
  node_tavily(("Tavily Search<br/>external retrieval"))
  node_groq(("Groq<br/>LLM service"))
end

subgraph group_api["API &amp; Sessions"]
  node_backend["FastAPI backend<br/>API server<br/>[main.py]"]
  node_sessions[("Session store<br/>in-memory sessions<br/>[session_store.py]")]
  node_exports["Export serializer<br/>CSV/XLSX/JSON export<br/>[export_utils.py]"]
end

subgraph group_dashboard["Dashboard"]
  node_frontend["React entry<br/>React/Vite client<br/>[main.jsx]"]
  node_app["App layout<br/>React routing/layout<br/>[App.jsx]"]
  node_chat["Chat rendering<br/>message UI<br/>[ChatMessage.jsx]"]
  node_api_client["REST API client<br/>frontend API adapter<br/>[api.js]"]
end

node_cli -->|"invokes"| node_orchestrator
node_backend -->|"shared agent"| node_orchestrator
node_backend -->|"maintains"| node_sessions
node_backend -->|"exports results"| node_exports
node_orchestrator -->|"uses"| node_prompts
node_orchestrator -->|"selects tools"| node_tools
node_orchestrator -->|"returns"| node_schemas
node_orchestrator -->|"LLM inference"| node_groq
node_tools -->|"SQL adapter"| node_sql_agent
node_tools -->|"web adapter"| node_web_agent
node_sql_agent -->|"SQLAlchemy tooling"| node_database
node_database -->|"persists to"| node_sqlite
node_web_agent -->|"searches"| node_tavily
node_web_agent -->|"summarizes"| node_groq
node_frontend -->|"mounts"| node_app
node_app -->|"hosts"| node_chat
node_chat -->|"uses"| node_api_client
node_api_client -->|"/api requests"| node_backend
node_backend -->|"adapts structured output"| node_schemas

click node_cli "https://github.com/prakashwebdevx/ai-business-research-agent-backend/blob/main/app.py"
click node_backend "https://github.com/prakashwebdevx/ai-business-research-agent-backend/blob/main/backend/main.py"
click node_sessions "https://github.com/prakashwebdevx/ai-business-research-agent-backend/blob/main/backend/session_store.py"
click node_exports "https://github.com/prakashwebdevx/ai-business-research-agent-backend/blob/main/backend/export_utils.py"
click node_orchestrator "https://github.com/prakashwebdevx/ai-business-research-agent-backend/blob/main/agent.py"
click node_prompts "https://github.com/prakashwebdevx/ai-business-research-agent-backend/blob/main/prompts.py"
click node_tools "https://github.com/prakashwebdevx/ai-business-research-agent-backend/blob/main/tools.py"
click node_schemas "https://github.com/prakashwebdevx/ai-business-research-agent-backend/blob/main/schemas.py"
click node_sql_agent "https://github.com/prakashwebdevx/ai-business-research-agent-backend/blob/main/sql_agent.py"
click node_database "https://github.com/prakashwebdevx/ai-business-research-agent-backend/blob/main/database.py"
click node_web_agent "https://github.com/prakashwebdevx/ai-business-research-agent-backend/blob/main/web_agent.py"
click node_frontend "https://github.com/prakashwebdevx/ai-business-research-agent-backend/blob/main/frontend/src/main.jsx"
click node_app "https://github.com/prakashwebdevx/ai-business-research-agent-backend/blob/main/frontend/src/App.jsx"
click node_chat "https://github.com/prakashwebdevx/ai-business-research-agent-backend/blob/main/frontend/src/components/ChatMessage.jsx"
click node_api_client "https://github.com/prakashwebdevx/ai-business-research-agent-backend/blob/main/frontend/src/lib/api.js"

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 toneBlue
class node_orchestrator,node_prompts,node_tools,node_schemas toneAmber
class node_sql_agent,node_database,node_sqlite toneMint
class node_web_agent,node_tavily,node_groq toneRose
class node_backend,node_sessions,node_exports toneIndigo
class node_frontend,node_app,node_chat,node_api_client toneTeal
```

## Components

### Interfaces

- [CLI entry](https://github.com/prakashwebdevx/ai-business-research-agent-backend/blob/main/app.py): Python CLI · `app.py`

### Agent Core

- [BusinessAgent](https://github.com/prakashwebdevx/ai-business-research-agent-backend/blob/main/agent.py): LangChain/Groq orchestrator · Routes questions to SQL, web, both, or direct answers. · `agent.py`
- [Agent prompts](https://github.com/prakashwebdevx/ai-business-research-agent-backend/blob/main/prompts.py): prompt templates · `prompts.py`
- [Tool registry](https://github.com/prakashwebdevx/ai-business-research-agent-backend/blob/main/tools.py): callable adapters · `tools.py`
- [Result schemas](https://github.com/prakashwebdevx/ai-business-research-agent-backend/blob/main/schemas.py): shared result models · Defines structured results and chart output. · `schemas.py`

### Internal Research

- [SQL sub-agent](https://github.com/prakashwebdevx/ai-business-research-agent-backend/blob/main/sql_agent.py): NL-to-SQL · Generates and executes read-only SQL queries. · `sql_agent.py`
- [Business database](https://github.com/prakashwebdevx/ai-business-research-agent-backend/blob/main/database.py): SQLAlchemy + SQLite · Owns entities and first-run sample-data initialization. · `database.py`
- **Employee SQLite DB**: runtime database · Generated runtime file at database/employee.db.

### External Research

- [Web sub-agent](https://github.com/prakashwebdevx/ai-business-research-agent-backend/blob/main/web_agent.py): research synthesis · Summarizes search findings with Groq. · `web_agent.py`
- **Tavily Search**: external retrieval
- **Groq**: LLM service · Powers routing, final answers, and web synthesis.

### API & Sessions

- [FastAPI backend](https://github.com/prakashwebdevx/ai-business-research-agent-backend/blob/main/backend/main.py): API server · Exposes chat, session, and export operations. · `backend/main.py`
- [Session store](https://github.com/prakashwebdevx/ai-business-research-agent-backend/blob/main/backend/session_store.py): in-memory sessions · Resynchronizes shared agent memory per session. · `backend/session_store.py`
- [Export serializer](https://github.com/prakashwebdevx/ai-business-research-agent-backend/blob/main/backend/export_utils.py): CSV/XLSX/JSON export · `backend/export_utils.py`

### Dashboard

- [React entry](https://github.com/prakashwebdevx/ai-business-research-agent-backend/blob/main/frontend/src/main.jsx): React/Vite client · `frontend/src/main.jsx`
- [App layout](https://github.com/prakashwebdevx/ai-business-research-agent-backend/blob/main/frontend/src/App.jsx): React routing/layout · `frontend/src/App.jsx`
- [Chat rendering](https://github.com/prakashwebdevx/ai-business-research-agent-backend/blob/main/frontend/src/components/ChatMessage.jsx): message UI · Renders tables, charts, and sourced web research. · `frontend/src/components/ChatMessage.jsx`
- [REST API client](https://github.com/prakashwebdevx/ai-business-research-agent-backend/blob/main/frontend/src/lib/api.js): frontend API adapter · `frontend/src/lib/api.js`

## Connections

- CLI entry → BusinessAgent: invokes
- FastAPI backend → BusinessAgent: shared agent
- FastAPI backend → Session store: maintains
- FastAPI backend → Export serializer: exports results
- BusinessAgent → Agent prompts: uses
- BusinessAgent → Tool registry: selects tools
- BusinessAgent → Result schemas: returns
- BusinessAgent → Groq: LLM inference
- Tool registry → SQL sub-agent: SQL adapter
- Tool registry → Web sub-agent: web adapter
- SQL sub-agent → Business database: SQLAlchemy tooling
- Business database → Employee SQLite DB: persists to
- Web sub-agent → Tavily Search: searches
- Web sub-agent → Groq: summarizes
- React entry → App layout: mounts
- App layout → Chat rendering: hosts
- Chat rendering → REST API client: uses
- REST API client → FastAPI backend: /api requests · Vite proxies API requests to FastAPI during development.
- FastAPI backend → Result schemas: adapts structured output
