# bcg-x-official/agentkit architecture

> Architecture diagram of the GitHub repository bcg-x-official/agentkit, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-02-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/bcg-x-official/agentkit
- Repository: https://github.com/bcg-x-official/agentkit

## Overview

##### Project Type and Purpose
- AgentKit is a full-stack application designed to help developers rapidly build production-grade “chat-based” agent apps. Its purpose is to provide a modular framework built upon FastAPI (backend) and Next.js (frontend) that integrates GenAI tools, task scheduling (with Celery/Redis), SQL and vector databases, and a flexible agent routing mechanism.
- The README emphasizes key areas such as reliability, modular agent architecture, configurable action plans, and a responsive UI.

##### File Structure
- Notice the clearly separated directories for the backend and frontend which confirms the full-stack nature.
- In the “backend” directory, observe:
- The use of Dockerfiles, Alembic migrations, and configuration files (YAMLs) that configure the agent, extraction tools, and other settings.
- A modular structure under “app” (including subdirectories like “api”, “config”, “core”, “db”, “services”, and “schemas”) which suggests an MVC-like and layered design.
- Specific modules for agent functionalities such as service subdirectories under “chat_agent” – including helpers, tools, and router agents – which point to a highly decoupled design for handling agent logic.
- Utilities for database operations, streaming responses, and error handling.
- In the “frontend” directory, observe:
- A Next.js and React stack with directories for pages, components, stores, hooks, and API client code.
- Configuration for ESLint, TailwindCSS, and testing (cypress, jest) which indicates good separation of concerns on the UI side.
- Directories indicating integration with authentication (NextAuth) and layout components intended for a reactive chat interface.

a. Main Components:
- Frontend: Built with Next.js (including pages, components, API client) that renders the reactive chat UI.
- Backend API: FastAPI application organized into versioned endpoints (e.g., v1 endpoints for chat, SQL queries, statistics). It handles authentication, routing, and integrates with the agent logic.
- Agent Services: These include the meta agent, chat agent, and associated tooling (such as PDF, SQL, image generation tools). Represent these as subcomponents within the backend’s “services” and “tools” modules.
- Database Layer: SQL databases (including the provided Chinook.db) and possibly a vector database integration for document ingestion.
- Task Queue: Use of Celery (with modules under backend/app/app/core/celery.py) with Redis for asynchronous and long-running tasks.
- Configuration and External Services: YAML config files (for agents, tools, extraction) indicate that the system is highly configurable. Also note external authentication (NextAuth) interfacing between frontend and backend.

b. Relationships and Interactions:
- The Frontend calls the Backend API endpoints to fetch and send chat queries.
- The API endpoints invoke the Agent Services (e.g., meta agent selects an appropriate action plan and routes the request to specific tool modules).
- The Agent Tools might interact in parallel (as described in the README’s "How it works" section) to retrieve data from databases or external services.
- Data storage and persistence are handled by the Database Layer; additional asynchronous processing is handled via the Task Queue (Celery/Redis) for long-running tasks.
- Use Docker-compose to manage deployment and inter-container communications among these components.

c. Architectural Patterns and Technologies:
- Separation of frontend and backend layers, with clear API boundaries.
- Modular, layered design in the backend, with configuration-driven agent and tool management.
- Use of asynchronous processing (Celery) to scale the agent’s pipeline.
- Leveraging containerization (Dockerfiles and docker-compose) for replicable deployment.
- Technologies: Next.js, FastAPI, Celery/Redis, Postgres (with pgvector), LangChain, NextAuth among others.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Layer
    subgraph "Frontend Layer"
        FE["Frontend (Next.js/React)"]:::frontend
        EA["External Authentication (NextAuth)"]:::frontend
    end

    %% Backend Layer
    subgraph "Backend Layer"
        API["Backend API (FastAPI)"]:::backend
        AS["Agent Services (Chat Agent & Tools)"]:::backend
        CF["Configuration Files (YAML)"]:::config
    end

    %% Infrastructure Layer
    subgraph "Infrastructure"
        DB["Database Layer (SQL & Vector DB)"]:::infra
        CH["Chinook Database"]:::infra
        TQ["Task Queue (Celery/Redis)"]:::infra
    end

    %% Relationships
    FE -->|"APIcalls"| API
    FE -->|"authRequest"| EA
    EA -->|"authValidate"| API
    API -->|"invokeAgents"| AS
    API -->|"loadConfig"| CF
    API -->|"enqueueTask"| TQ
    AS -->|"dbQuery"| DB
    AS -->|"dbQuery"| CH
    CF -->|"configure"| AS

    %% Click Events
    click FE "https://github.com/bcg-x-official/agentkit/tree/main/frontend/"
    click API "https://github.com/bcg-x-official/agentkit/tree/main/backend/app/app/api/v1/"
    click AS "https://github.com/bcg-x-official/agentkit/tree/main/backend/app/app/services/chat_agent/"
    click DB "https://github.com/bcg-x-official/agentkit/tree/main/backend/app/app/db/"
    click CH "https://github.com/bcg-x-official/agentkit/blob/main/Chinook.db"
    click TQ "https://github.com/bcg-x-official/agentkit/blob/main/backend/app/app/core/celery.py"
    click CF "https://github.com/bcg-x-official/agentkit/tree/main/backend/app/app/config/"
    click EA "https://github.com/bcg-x-official/agentkit/blob/main/frontend/src/pages/api/auth/[...nextauth].ts"

    %% Styles
    classDef frontend fill:#E0F7FA,stroke:#00796B,stroke-width:2px;
    classDef backend fill:#E8EAF6,stroke:#303F9F,stroke-width:2px;
    classDef infra fill:#F1F8E9,stroke:#7CB342,stroke-width:2px;
    classDef config fill:#FFF9C4,stroke:#FBC02D,stroke-width:2px;
```
