# j007ia/agente-1 architecture

> Architecture diagram of the GitHub repository j007ia/agente-1, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-03). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/j007ia/agente-1
- Repository: https://github.com/j007ia/agente-1

## Overview

##### Project type and purpose
- This is a backend-centric multi-agent system implemented in Python using LangChain. It exposes an API (via `src/main.py`) and offers both an HTTP interface and a Gradio UI (`gradio_app.py`). Its purpose is to route user queries to specialized agents (calculator, search/cinema, story, etc.) and orchestrate calls to underlying tools or external APIs.

##### File structure
- Top-level:
- `.env.example` + `.gitignore` + `pyproject.toml` – configuration & dependencies
- `gradio_app.py` – standalone Gradio-based UI
- `src/main.py` – API server entrypoint
- `src/agents/` – definitions of each Agent class (base, router, calculator, search, story, general)
- `src/tools/` – concrete tool implementations and a registry
- `src/services/` – glue layer encapsulating business logic and external-API calls (e.g., SerpAPI via `langsmith_service`)
- `src/routers/agent_router.py` – logic that inspects the input and delegates to the correct agent
- `src/schemas/` – Pydantic schemas for request/response models
- `src/memory/` – memory abstraction for agent contexts
- `src/logging_config.py` – centralized logging setup
- `src/templates/` – prompt or message templates

##### Key README insights
- Specialized agents each handle a specific domain
- A router agent dispatches incoming queries
- Environment variable support for API keys
- Usage via `poetry run python src/main.py`

a. External Clients
– CLI/HTTP client or Gradio front-end
b. API Layer (`src/main.py`)
– Receives HTTP requests, validates via Pydantic schemas (`src/schemas`), forwards payload to Agent Router
c. Agent Router (`src/routers/agent_router.py`)
– Applies routing logic to choose the right specialized agent
d. Specialized Agents (`src/agents/*_agent.py`)
– Each agent uses:
- Memory module (`src/memory`) to store/retrieve context
- Templates (`src/templates`) for prompt formatting
- Service layer (`src/services/agent_service.py`) for orchestration
- Underlying Tools (`src/tools/*.py`) or external API clients (`langsmith_service`)
e. Tools & External APIs
– CalculatorTool (pure logic), SearchTool (calls SerpAPI via `SERPAPI_KEY`), StoryTool, etc.
f. Logging & Configuration
– Centralized via `src/logging_config.py`

##### Architectural patterns & design principles
- Layered architecture: presentation (API/Gradio) → routing → agents → tools/services → external systems
- Single Responsibility: each agent/tool has one domain
- Dependency injection via registries and services
- Chain-of-responsibility in the router agent

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User and UI Layer
    User["User"] 
    UI_HTTP["HTTP Client"]:::ui
    UI_GRADIO["Gradio UI"]:::ui
    click UI_GRADIO "https://github.com/j007ia/agente-1/blob/main/gradio_app.py"

    %% API Layer
    API["API Server\n(src/main.py)"]:::api
    click API "https://github.com/j007ia/agente-1/blob/main/src/main.py"
    Schemas["Pydantic Schemas\n(src/schemas/agent_schema.py)"]:::api
    click Schemas "https://github.com/j007ia/agente-1/blob/main/src/schemas/agent_schema.py"

    %% Router Layer
    ARouter["Agent Router\n(src/routers/agent_router.py)"]:::router
    click ARouter "https://github.com/j007ia/agente-1/blob/main/src/routers/agent_router.py"

    %% Agents Layer
    subgraph "Agents" 
        direction TB
        BaseAgent["BaseAgent\n(src/agents/base_agent.py)"]:::agent
        click BaseAgent "https://github.com/j007ia/agente-1/blob/main/src/agents/base_agent.py"
        RouterAgent["RouterAgent\n(src/agents/router_agent.py)"]:::agent
        click RouterAgent "https://github.com/j007ia/agente-1/blob/main/src/agents/router_agent.py"
        CalcAgent["CalculatorAgent\n(src/agents/calculator_agent.py)"]:::agent
        click CalcAgent "https://github.com/j007ia/agente-1/blob/main/src/agents/calculator_agent.py"
        SearchAgent["SearchAgent\n(src/agents/search_agent.py)"]:::agent
        click SearchAgent "https://github.com/j007ia/agente-1/blob/main/src/agents/search_agent.py"
        StoryAgent["StoryAgent\n(src/agents/story_agent.py)"]:::agent
        click StoryAgent "https://github.com/j007ia/agente-1/blob/main/src/agents/story_agent.py"
        GeneralAgent["GeneralAgent\n(src/agents/general_agent.py)"]:::agent
        click GeneralAgent "https://github.com/j007ia/agente-1/blob/main/src/agents/general_agent.py"
    end

    %% Memory and Templates
    Memory["Memory Module\n(src/memory/)"]:::infra
    click Memory "https://github.com/j007ia/agente-1/tree/main/src/memory/"
    Templates["Templates\n(src/templates/)"]:::infra
    click Templates "https://github.com/j007ia/agente-1/tree/main/src/templates/"

    %% Services and Tools
    subgraph "Services & Tools"
        direction TB
        AgentService["Agent Service\n(src/services/agent_service.py)"]:::service
        click AgentService "https://github.com/j007ia/agente-1/blob/main/src/services/agent_service.py"
        LangSmith["LangSmith Service\n(src/services/langsmith_service.py)"]:::service
        click LangSmith "https://github.com/j007ia/agente-1/blob/main/src/services/langsmith_service.py"
        Registry["Tool Registry\n(src/tools/tool_registry.py)"]:::service
        click Registry "https://github.com/j007ia/agente-1/blob/main/src/tools/tool_registry.py"
        CalcTool["CalculatorTool\n(src/tools/calculator_tool.py)"]:::service
        click CalcTool "https://github.com/j007ia/agente-1/blob/main/src/tools/calculator_tool.py"
        SearchTool["SearchTool\n(src/tools/search_tool.py)"]:::service
        click SearchTool "https://github.com/j007ia/agente-1/blob/main/src/tools/search_tool.py"
        StoryTool["StoryTool\n(src/tools/story_tool.py)"]:::service
        click StoryTool "https://github.com/j007ia/agente-1/blob/main/src/tools/story_tool.py"
    end

    %% External Systems and Configuration
    ExternalAPI["SerpAPI\n(External)"]:::external
    Config["Config & Logging\n(pyproject.toml,\n.env.example,\n.gitignore,\nsrc/logging_config.py)"]:::infra
    click Config "https://github.com/j007ia/agente-1/blob/main/src/logging_config.py"

    %% Connections
    User --> UI_HTTP
    User --> UI_GRADIO
    UI_HTTP --> API
    UI_GRADIO --> API
    API -->|"HTTP POST /query"| Schemas
    Schemas -->|"validated payload"| ARouter
    ARouter -->|routing logic| RouterAgent
    RouterAgent -->|delegates| CalcAgent
    RouterAgent -->|delegates| SearchAgent
    RouterAgent -->|delegates| StoryAgent
    RouterAgent -->|delegates| GeneralAgent
    CalcAgent --> Memory
    CalcAgent --> Templates
    CalcAgent --> AgentService
    SearchAgent --> Memory
    SearchAgent --> Templates
    SearchAgent --> AgentService
    StoryAgent --> Memory
    StoryAgent --> Templates
    StoryAgent --> AgentService
    GeneralAgent --> Memory
    GeneralAgent --> Templates
    GeneralAgent --> AgentService
    AgentService --> Registry
    AgentService --> CalcTool
    AgentService --> SearchTool
    AgentService --> StoryTool
    AgentService --> LangSmith
    LangSmith --> ExternalAPI
    Config --> API
    Config --> AgentService

    %% Styles
    classDef ui fill:#d4edda,stroke:#28a745,color:#155724
    classDef api fill:#cfe2ff,stroke:#004085,color:#002752
    classDef router fill:#ffe5b4,stroke:#d88c00,color:#663d00
    classDef agent fill:#ffdd99,stroke:#cc7a00,color:#663d00
    classDef service fill:#e2d5f7,stroke:#6f42c1,color:#351c75
    classDef external fill:#f0f0f0,stroke:#6c757d,color:#343a40
    classDef infra fill:#f8d7da,stroke:#721c24,color:#491217
```
