# glaborie/agentguard architecture

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

- Interactive diagram: https://gitdiagram.com/glaborie/agentguard
- Repository: https://github.com/glaborie/agentguard

## Overview

AgentGuard is a self-hosted reliability control plane for RAG and agent applications. Its primary request path accepts chat through the API, dispatches by model mode, and returns direct, RAG-grounded, or agent-generated completions; a CLI also exposes RAG query and ingestion. Separate product branches manage knowledge ingestion and retrieval, runtime protection and configuration, evaluation and red teaming, and telemetry. The graph preserves documented capabilities whose source was not sampled, but their internal calls are omitted where evidence is unavailable.

Main entry points: [`app/rag/chain.py`](https://github.com/glaborie/agentguard/blob/main/app/rag/chain.py) (RAG chain), [`app/rag/service.py`](https://github.com/glaborie/agentguard/blob/main/app/rag/service.py) (RAG service), [`app/api/services/chat_service.py`](https://github.com/glaborie/agentguard/blob/main/app/api/services/chat_service.py) (Mode dispatcher), [`app/api/app.py`](https://github.com/glaborie/agentguard/blob/main/app/api/app.py) (HTTP API)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_interfaces["Product interfaces"]
  node_api["HTTP API<br/>[app.py]"]
  node_chat_route["Chat endpoint<br/>[chat.py]"]
  node_chat_service["Mode dispatcher<br/>[chat_service.py]"]
  node_direct["Direct completion<br/>[direct_llm.py]"]
  node_rag_completion["RAG completion<br/>[rag_llm.py]"]
  node_agent_service["Agent completion<br/>[agent_llm.py]"]
  node_agent_graph["Agent graph<br/>[graph.py]"]
  node_mcp["GitHub MCP client<br/>[mcp_client.py]"]
  node_cli["CLI commands<br/>[query.py]"]
  node_ingest_cli["Ingest command<br/>[ingest.py]"]
end

subgraph group_rag["RAG knowledge workflow"]
  node_rag_service["RAG service<br/>[service.py]"]
  node_ingestion["Document ingestion<br/>[ingest.py]"]
  node_rag_chain["RAG chain<br/>[chain.py]"]
  node_hybrid["Hybrid retrieval"]
  node_bm25["BM25 index<br/>[bm25_index.py]"]
end

subgraph group_runtime["Runtime controls"]
  node_feature_flags["Runtime flags<br/>[feature_flags.py]"]
  node_config_route["Config endpoint<br/>[config.py]"]
  node_pii["PII masking<br/>[pii.py]"]
  node_guardrails["Guardrails"]
  node_semantic_cache["Semantic cache<br/>[semantic_cache.py]"]
end

subgraph group_quality["Evaluation and testing"]
  node_eval_service["Evaluation service<br/>[service.py]"]
  node_experiment_runner["Experiment runner<br/>[runner.py]"]
  node_deepeval["DeepEval metrics"]
  node_ragas["RAGAS metrics<br/>[ragas_metrics.py]"]
  node_red_team["Red teaming<br/>[red_team.py]"]
end

subgraph group_observability["Observability"]
  node_tracing["Trace callbacks<br/>[tracing.py]"]
  node_telemetry["Telemetry setup<br/>[telemetry.py]"]
end

node_user(("AI user"))
node_qdrant[("Qdrant store")]
node_model_gateway["Model gateway"]
node_langfuse["Langfuse"]
node_otel["OpenTelemetry backends"]

node_user -->|"sends chat"| node_api
node_api -->|"routes requests"| node_chat_route
node_chat_route -->|"delegates"| node_chat_service
node_chat_service -->|"dispatches direct"| node_direct
node_chat_service -->|"dispatches RAG"| node_rag_completion
node_chat_service -->|"dispatches agent"| node_agent_service
node_agent_service -->|"runs graph"| node_agent_graph
node_agent_graph -->|"uses client"| node_mcp
node_rag_completion -->|"queries RAG"| node_rag_service
node_rag_service -->|"masks input"| node_pii
node_rag_service -->|"delegates query"| node_rag_chain
node_cli -->|"queries"| node_rag_service
node_ingest_cli -->|"starts ingestion"| node_rag_service
node_rag_service -->|"delegates ingestion"| node_ingestion
node_ingestion -->|"stores vectors"| node_qdrant
node_ingestion -->|"invalidates index"| node_bm25
node_rag_chain -->|"builds retriever"| node_hybrid
node_rag_chain -->|"retrieves vectors"| node_qdrant
node_rag_chain -->|"generates answer"| node_model_gateway
node_hybrid -->|"combines results"| node_bm25
node_api -->|"routes config"| node_config_route
node_config_route -->|"reads and updates"| node_feature_flags
node_rag_chain -->|"selects retrieval"| node_feature_flags
node_rag_completion -.->|"applies protection"| node_guardrails
node_chat_service -.->|"may use cache"| node_semantic_cache
node_eval_service -->|"runs metrics"| node_deepeval
node_experiment_runner -->|"evaluates chain"| node_rag_chain
node_experiment_runner -->|"uses metrics"| node_ragas
node_red_team -.->|"probes models"| node_model_gateway
node_api -->|"initializes"| node_telemetry
node_cli -->|"initializes"| node_telemetry
node_rag_chain -->|"uses callbacks"| node_tracing
node_tracing -->|"sends traces"| node_langfuse
node_telemetry -.->|"exports telemetry"| node_otel

click node_api "https://github.com/glaborie/agentguard/blob/main/app/api/app.py"
click node_chat_route "https://github.com/glaborie/agentguard/blob/main/app/api/routes/chat.py"
click node_chat_service "https://github.com/glaborie/agentguard/blob/main/app/api/services/chat_service.py"
click node_direct "https://github.com/glaborie/agentguard/blob/main/app/api/services/direct_llm.py"
click node_rag_completion "https://github.com/glaborie/agentguard/blob/main/app/api/services/rag_llm.py"
click node_agent_service "https://github.com/glaborie/agentguard/blob/main/app/api/services/agent_llm.py"
click node_agent_graph "https://github.com/glaborie/agentguard/blob/main/app/agent/graph.py"
click node_mcp "https://github.com/glaborie/agentguard/blob/main/app/agent/mcp_client.py"
click node_cli "https://github.com/glaborie/agentguard/blob/main/app/cli/commands/query.py"
click node_ingest_cli "https://github.com/glaborie/agentguard/blob/main/app/cli/commands/ingest.py"
click node_rag_service "https://github.com/glaborie/agentguard/blob/main/app/rag/service.py"
click node_ingestion "https://github.com/glaborie/agentguard/blob/main/app/rag/ingest.py"
click node_rag_chain "https://github.com/glaborie/agentguard/blob/main/app/rag/chain.py"
click node_hybrid "https://github.com/glaborie/agentguard/blob/main/app/rag/hybrid_retriever.py"
click node_bm25 "https://github.com/glaborie/agentguard/blob/main/app/rag/bm25_index.py"
click node_feature_flags "https://github.com/glaborie/agentguard/blob/main/app/core/feature_flags.py"
click node_config_route "https://github.com/glaborie/agentguard/blob/main/app/api/routes/config.py"
click node_pii "https://github.com/glaborie/agentguard/blob/main/app/core/pii.py"
click node_guardrails "https://github.com/glaborie/agentguard/blob/main/guardrails/custom_guardrails.py"
click node_semantic_cache "https://github.com/glaborie/agentguard/blob/main/guardrails/semantic_cache.py"
click node_eval_service "https://github.com/glaborie/agentguard/blob/main/app/eval/service.py"
click node_experiment_runner "https://github.com/glaborie/agentguard/blob/main/app/eval/experiments/runner.py"
click node_deepeval "https://github.com/glaborie/agentguard/blob/main/app/eval/deepeval_metrics.py"
click node_ragas "https://github.com/glaborie/agentguard/blob/main/app/eval/ragas_metrics.py"
click node_red_team "https://github.com/glaborie/agentguard/blob/main/app/cli/commands/red_team.py"
click node_tracing "https://github.com/glaborie/agentguard/blob/main/app/core/tracing.py"
click node_telemetry "https://github.com/glaborie/agentguard/blob/main/app/core/telemetry.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,node_chat_route,node_chat_service,node_direct,node_rag_completion,node_agent_service,node_agent_graph,node_mcp,node_cli,node_ingest_cli,node_user toneBlue
class node_rag_service,node_ingestion,node_rag_chain,node_hybrid,node_bm25,node_qdrant toneAmber
class node_feature_flags,node_config_route,node_pii,node_guardrails,node_semantic_cache toneMint
class node_eval_service,node_experiment_runner,node_deepeval,node_ragas,node_red_team toneRose
class node_tracing,node_telemetry,node_model_gateway,node_langfuse,node_otel toneIndigo
```

## Components

### Product interfaces

- [HTTP API](https://github.com/glaborie/agentguard/blob/main/app/api/app.py): `app/api/app.py`
- [Chat endpoint](https://github.com/glaborie/agentguard/blob/main/app/api/routes/chat.py): `app/api/routes/chat.py`
- [Mode dispatcher](https://github.com/glaborie/agentguard/blob/main/app/api/services/chat_service.py): `app/api/services/chat_service.py`
- [Direct completion](https://github.com/glaborie/agentguard/blob/main/app/api/services/direct_llm.py): `app/api/services/direct_llm.py`
- [RAG completion](https://github.com/glaborie/agentguard/blob/main/app/api/services/rag_llm.py): `app/api/services/rag_llm.py`
- [Agent completion](https://github.com/glaborie/agentguard/blob/main/app/api/services/agent_llm.py): `app/api/services/agent_llm.py`
- [Agent graph](https://github.com/glaborie/agentguard/blob/main/app/agent/graph.py): `app/agent/graph.py`
- [GitHub MCP client](https://github.com/glaborie/agentguard/blob/main/app/agent/mcp_client.py): `app/agent/mcp_client.py`
- [CLI commands](https://github.com/glaborie/agentguard/blob/main/app/cli/commands/query.py): `app/cli/commands/query.py`
- [Ingest command](https://github.com/glaborie/agentguard/blob/main/app/cli/commands/ingest.py): `app/cli/commands/ingest.py`

### RAG knowledge workflow

- [RAG service](https://github.com/glaborie/agentguard/blob/main/app/rag/service.py): `app/rag/service.py`
- [Document ingestion](https://github.com/glaborie/agentguard/blob/main/app/rag/ingest.py): `app/rag/ingest.py`
- [RAG chain](https://github.com/glaborie/agentguard/blob/main/app/rag/chain.py): `app/rag/chain.py`
- [Hybrid retrieval](https://github.com/glaborie/agentguard/blob/main/app/rag/hybrid_retriever.py): `app/rag/hybrid_retriever.py`
- [BM25 index](https://github.com/glaborie/agentguard/blob/main/app/rag/bm25_index.py): `app/rag/bm25_index.py`

### Runtime controls

- [Runtime flags](https://github.com/glaborie/agentguard/blob/main/app/core/feature_flags.py): `app/core/feature_flags.py`
- [Config endpoint](https://github.com/glaborie/agentguard/blob/main/app/api/routes/config.py): `app/api/routes/config.py`
- [PII masking](https://github.com/glaborie/agentguard/blob/main/app/core/pii.py): `app/core/pii.py`
- [Guardrails](https://github.com/glaborie/agentguard/blob/main/guardrails/custom_guardrails.py): `guardrails/custom_guardrails.py`
- [Semantic cache](https://github.com/glaborie/agentguard/blob/main/guardrails/semantic_cache.py): `guardrails/semantic_cache.py`

### Evaluation and testing

- [Evaluation service](https://github.com/glaborie/agentguard/blob/main/app/eval/service.py): `app/eval/service.py`
- [Experiment runner](https://github.com/glaborie/agentguard/blob/main/app/eval/experiments/runner.py): `app/eval/experiments/runner.py`
- [DeepEval metrics](https://github.com/glaborie/agentguard/blob/main/app/eval/deepeval_metrics.py): `app/eval/deepeval_metrics.py`
- [RAGAS metrics](https://github.com/glaborie/agentguard/blob/main/app/eval/ragas_metrics.py): `app/eval/ragas_metrics.py`
- [Red teaming](https://github.com/glaborie/agentguard/blob/main/app/cli/commands/red_team.py): `app/cli/commands/red_team.py`

### Observability

- [Trace callbacks](https://github.com/glaborie/agentguard/blob/main/app/core/tracing.py): `app/core/tracing.py`
- [Telemetry setup](https://github.com/glaborie/agentguard/blob/main/app/core/telemetry.py): `app/core/telemetry.py`

### Other components

- **AI user**
- **Qdrant store**
- **Model gateway**
- **Langfuse**
- **OpenTelemetry backends**

## Connections

- AI user → HTTP API: sends chat (evidence: `README.md`)
- HTTP API → Chat endpoint: routes requests (evidence: `app/api/app.py`)
- Chat endpoint → Mode dispatcher: delegates (evidence: `app/api/routes/chat.py`)
- Mode dispatcher → Direct completion: dispatches direct (evidence: `app/api/services/chat_service.py`)
- Mode dispatcher → RAG completion: dispatches RAG (evidence: `app/api/services/chat_service.py`)
- Mode dispatcher → Agent completion: dispatches agent (evidence: `app/api/services/chat_service.py`)
- Agent completion → Agent graph: runs graph
- Agent graph → GitHub MCP client: uses client (evidence: `app/agent/graph.py`)
- RAG completion → RAG service: queries RAG (evidence: `app/api/services/rag_llm.py`)
- RAG service → PII masking: masks input (evidence: `app/rag/service.py`)
- RAG service → RAG chain: delegates query (evidence: `app/rag/service.py`)
- CLI commands → RAG service: queries (evidence: `app/cli/commands/query.py`)
- Ingest command → RAG service: starts ingestion (evidence: `app/cli/commands/ingest.py`)
- RAG service → Document ingestion: delegates ingestion (evidence: `app/rag/service.py`)
- Document ingestion → Qdrant store: stores vectors (evidence: `app/rag/ingest.py`)
- Document ingestion → BM25 index: invalidates index (evidence: `app/rag/ingest.py`)
- RAG chain → Hybrid retrieval: builds retriever (evidence: `app/rag/chain.py`)
- RAG chain → Qdrant store: retrieves vectors (evidence: `app/rag/chain.py`)
- RAG chain → Model gateway: generates answer (evidence: `app/rag/chain.py`)
- Hybrid retrieval → BM25 index: combines results (evidence: `app/rag/chain.py`)
- HTTP API → Config endpoint: routes config (evidence: `app/api/app.py`)
- Config endpoint → Runtime flags: reads and updates (evidence: `app/api/routes/config.py`)
- RAG chain → Runtime flags: selects retrieval (evidence: `app/rag/chain.py`)
- RAG completion → Guardrails: applies protection (evidence: `README.md`)
- Mode dispatcher → Semantic cache: may use cache
- Evaluation service → DeepEval metrics: runs metrics (evidence: `app/eval/service.py`)
- Experiment runner → RAG chain: evaluates chain (evidence: `app/eval/experiments/runner.py`)
- Experiment runner → RAGAS metrics: uses metrics (evidence: `app/eval/experiments/runner.py`)
- Red teaming → Model gateway: probes models (evidence: `README.md`)
- HTTP API → Telemetry setup: initializes (evidence: `app/api/app.py`)
- CLI commands → Telemetry setup: initializes (evidence: `app/cli/app.py`)
- RAG chain → Trace callbacks: uses callbacks (evidence: `app/rag/chain.py`)
- Trace callbacks → Langfuse: sends traces (evidence: `app/core/tracing.py`)
- Telemetry setup → OpenTelemetry backends: exports telemetry (evidence: `README.md`)
