A production-ready FastAPI backend demonstrating Agentic RAG. A system that combines Retrieval-Augmented Generation (RAG) with autonomous tool-calling capabilities to create intelligent, action-oriented AI agents.
While traditional RAG systems simply retrieve context and generate an answer (Query → Retrieve → Answer), AgenticRAG introduces an autonomous reasoning loop:
Query → Retrieve → Reason → Decide → Act → Answer
The agent is capable of:
- Retrieving relevant context from a vector knowledge base.
- Reasoning about whether the context is relevant to the user's action.
- Deciding if external tools need to be called.
- Acting by executing tools (e.g., scheduling meetings, sending emails).
- Answering the user with citations and confirmations of actions taken.
- Vector Search Engine: Powered by Supabase
pgvector. - Intelligent Routing: Multi-provider support (OpenAI, Azure OpenAI, Anthropic).
- Autonomous Agents: Built-in reasoning loop for tool execution.
- Tool Integrations: Google Calendar scheduling and Gmail sending out-of-the-box.
- Conversational Memory: Multi-turn chat history support.
- Citation Tracking: Source attribution for generated answers.
- Multi-Tenant Security: Row-Level Security (RLS) implemented in Supabase to ensure users only access their own vector embeddings via scoped JWT tokens.
- Human-in-the-Loop (HITL): High-risk tools (like calendar edits and emails) are paused in a
PENDING_APPROVALstate, requiring explicit frontend confirmation before execution. - Action Idempotency: Tool execution wrappers generate SHA-256 hashes of arguments to prevent accidental duplicate actions (like sending double emails) during LLM network retries.
- Observability (OTel & Langfuse): Fully instrumented with OpenTelemetry for FastAPI/OpenAI distributed tracing, combined with Langfuse for prompt monitoring, token counting, and evaluation.
- Audit Logging: Comprehensive system tracking (
audit_logger) that logs all tool executions, parameters, and their success/failure status to the database. - CI/CD Pipeline: Jenkins pipeline (
Jenkinsfile) integrated withpytestand mock LLM calls to automatically validate the RAG agent's logic on every commit.
AgenticRAG/
├── app/
│ ├── agents/ # Main agent reasoning loop & orchestrator
│ │ └── tools/ # Agent capabilities (Calendar, Email)
│ ├── services/ # Core RAG logic, embeddings, chunking
│ ├── schemas/ # Pydantic validation models
│ ├── config/ # Environment & database configurations
│ └── data/ # Default seeding documents
├── tests/ # Setup verification and debugging utilities
├── credentials/ # Secure storage for service accounts (gitignored)
├── sql/ # Database initialization scripts
└── static/ # Web chat interface UI
- Python 3.11+
- Supabase Account for PostgreSQL + pgvector
- OpenAI / Azure OpenAI / Anthropic API Keys
- Google Cloud Console (Optional: for Calendar & Email tools)
git clone https://github.com/ameynarwadkar/AgenticRAG.git
cd AgenticRAG
# Using uv for lightning-fast package management (or pip)
uv venv
source .venv/bin/activate
uv pip install -r requirements.txt- Create a new project in Supabase.
- Navigate to the SQL Editor.
- Copy and run the contents of
sql/init_supabase.sqlto create therag_chunkstable, vector indexes, and search functions.
Create your .env file from the template:
cp .env.example .envFill in your API keys, Supabase credentials, and preferred AI provider (openai, azure, or anthropic).
(Optional) For Google Workspace tools:
Place your Google Service Account JSON in credentials/service_account.json and configure GOOGLE_CALENDAR_EMAIL. Note: Domain-Wide Delegation is required for Google Workspace accounts.
Verify your setup using the built-in diagnostic tool:
uv run python tests/test_setup.pyStart the FastAPI server:
uv run uvicorn main:app --reload --port 8000Once the server is running, visit http://localhost:8000/docs for the interactive Swagger UI.
Populate the vector database with the default documents:
curl -X POST http://localhost:8000/seedAnswers questions strictly based on the retrieved context.
curl -X POST http://localhost:8000/answer \
-H "Content-Type: application/json" \
-d '{"query": "What is your return policy?", "top_k": 6}'Can answer questions AND take actions. The agent will use RAG context to influence tool parameters if needed.
curl -X POST http://localhost:8000/agent \
-H "Content-Type: application/json" \
-d '{
"query": "Schedule a consultation call with john@example.com for tomorrow at 2pm",
"top_k": 6
}'The architecture is highly extensible. To add a new agent capability:
- Create the Tool (
app/agents/tools/my_tool.py): Inherit fromBaseTooland implement theexecutemethod. - Register in Registry (
app/agents/tools/registry.py): Add your tool to theToolRegistry. - Add Tool Schema (
app/schemas/tool_schemas.py): Provide the JSON schema definition for the LLM to understand when and how to invoke the tool.
A Dockerfile is included for containerized deployment (e.g., Google Cloud Run, AWS AppRunner, Docker Compose).
docker build -t agentic-rag .
docker run -p 8080:8080 --env-file .env agentic-ragContributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License.