# lokesh-1511/llm-gateway architecture

> Architecture diagram of the GitHub repository lokesh-1511/llm-gateway, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/lokesh-1511/llm-gateway
- Repository: https://github.com/lokesh-1511/llm-gateway

## Overview

#### Purpose and Entry Points
PromptOps Gateway is a FastAPI reverse proxy for OpenAI-compatible chat-completion traffic. It sanitizes prompts, semantically reuses prior answers, forwards misses to Groq, and records request telemetry. The backend starts from `main.py`; the operator control plane starts at `frontend/src/main.jsx` and composes through `frontend/src/App.jsx`.

#### Request Gateway and Orchestration
`main.py` is the critical-path controller for `POST /v1/chat/completions`. It accepts client payloads, coordinates security, caching, upstream proxying, and background work while preserving an LLM-provider-style response. `models.py` defines shared request/response or persistence models, and `routing_service.py` likely centralizes route/provider selection policy.

#### Inline Security Boundary
The DLP boundary is implemented by `security_service.py`, with authentication/authorization concerns separated in `auth_service.py`. Before any external LLM request, the gateway analyzes prompt content with Presidio/SpaCy and substitutes sensitive entities with placeholders. This makes the sanitized payload—not the original client text—the material sent beyond the enterprise boundary.

#### Semantic Cache Layer
`cache_service.py` owns semantic lookup and response reuse; `compression_service.py` is an adjacent optimization component for reducing request/context size. Embeddings are backed by local ChromaDB persistence under `.chroma_db/chroma.sqlite3` and its collection data directories. A cache hit short-circuits provider invocation; a miss proceeds upstream and schedules cache population after the response.

#### Upstream LLM Proxy
`proxy_service.py` encapsulates asynchronous HTTP forwarding to the configured Groq OpenAI-compatible endpoint. `main.py` should remain the policy/orchestration layer, while this service owns provider transport, API-key use, request forwarding, and upstream response handling. The external dependency is Groq, configured through `GROQ_API_KEY` and `GROQ_BASE_URL`.

#### Telemetry, Persistence, and Background Work
`analytics.py` records latency, token estimates, PII detection, cache status, and cost-savings metrics. `database.py` provides SQLAlchemy/PostgreSQL connectivity, with Supabase as the intended managed PostgreSQL deployment; `llm_logs.db` suggests a local persisted log/database artifact is also present. Writes and cache inserts are deliberately deferred using FastAPI/Starlette background tasks so they do not delay the client response.

#### Control Plane and Supporting Runtime
The React/Vite frontend displays operational and administrative views through `frontend/src/Dashboard.jsx`, `frontend/src/AdminDashboard.jsx`, and `frontend/src/ChatPortal.jsx`. Material runtime dependencies are Python/FastAPI/Uvicorn, async `httpx`, Presidio/SpaCy NER, Sentence Transformers embeddings, ChromaDB, tiktoken, PostgreSQL/Supabase, and React/Vite. Heavy local NLP and vector operations are intended to run off the async event loop via threadpool execution.

Main entry points: [`main.py`](https://github.com/lokesh-1511/llm-gateway/blob/main/main.py) (Chat Completions Gateway), [`database.py`](https://github.com/lokesh-1511/llm-gateway/blob/main/database.py) (SQLAlchemy Database), [`cache_service.py`](https://github.com/lokesh-1511/llm-gateway/blob/main/cache_service.py) (Semantic Cache), [`proxy_service.py`](https://github.com/lokesh-1511/llm-gateway/blob/main/proxy_service.py) (Groq Proxy)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_gateway["Gateway Runtime"]
  node_main["Chat Completions Gateway<br/>FastAPI controller<br/>[main.py]"]
  node_models["Shared Models<br/>Python models<br/>[models.py]"]
  node_routing["Routing Policy<br/>routing service<br/>[routing_service.py]"]
  node_auth["Auth Service<br/>security service<br/>[auth_service.py]"]
  node_dlp["DLP Sanitizer<br/>Presidio/SpaCy NER"]
  node_compression["Context Compression<br/>optimization service"]
  node_proxy["Groq Proxy<br/>async HTTP transport<br/>[proxy_service.py]"]
end

subgraph group_data["Data &amp; Background"]
  node_cache["Semantic Cache<br/>cache service<br/>[cache_service.py]"]
  node_chromadb[("ChromaDB Store<br/>vector database<br/>[chroma.sqlite3]")]
  node_background["Background Tasks<br/>FastAPI tasks"]
  node_analytics["Request Analytics<br/>telemetry service<br/>[analytics.py]"]
  node_database[("SQLAlchemy Database<br/>persistence service<br/>[database.py]")]
  node_local_logs[("Local Log Database<br/>SQLite artifact<br/>[llm_logs.db]")]
end

subgraph group_control["Operator Control Plane"]
  node_frontend_entry["Frontend Entry<br/>React/Vite entry<br/>[main.jsx]"]
  node_app["Control Plane App<br/>React composition<br/>[App.jsx]"]
  node_operations_ui["Operations Views<br/>React dashboards<br/>[Dashboard.jsx]"]
end

subgraph group_external["External Services"]
  node_supabase[("Supabase PostgreSQL<br/>managed database")]
  node_groq{{"Groq API<br/>external LLM provider"}}
end

node_client(("API Clients"))

node_client -->|"POST chat completions"| node_main
node_main -->|"uses contracts"| node_models
node_main -->|"authorizes request"| node_auth
node_main -->|"selects route"| node_routing
node_main -->|"sanitizes prompt"| node_dlp
node_main -->|"optimizes context"| node_compression
node_main -->|"semantic lookup"| node_cache
node_cache -->|"embeddings and vectors"| node_chromadb
node_main -->|"cache miss"| node_proxy
node_proxy -->|"async OpenAI-compatible request"| node_groq
node_main -.->|"schedules deferred work"| node_background
node_background -.->|"populate on miss"| node_cache
node_background -.->|"record telemetry"| node_analytics
node_analytics -->|"persists metrics"| node_database
node_database -.->|"managed deployment"| node_supabase
node_database -.->|"local artifact"| node_local_logs
node_frontend_entry -->|"mounts"| node_app
node_app -->|"renders"| node_operations_ui
node_operations_ui -->|"operates gateway"| node_main

click node_main "https://github.com/lokesh-1511/llm-gateway/blob/main/main.py"
click node_models "https://github.com/lokesh-1511/llm-gateway/blob/main/models.py"
click node_routing "https://github.com/lokesh-1511/llm-gateway/blob/main/routing_service.py"
click node_auth "https://github.com/lokesh-1511/llm-gateway/blob/main/auth_service.py"
click node_dlp "https://github.com/lokesh-1511/llm-gateway/blob/main/security_service.py"
click node_compression "https://github.com/lokesh-1511/llm-gateway/blob/main/compression_service.py"
click node_cache "https://github.com/lokesh-1511/llm-gateway/blob/main/cache_service.py"
click node_chromadb "https://github.com/lokesh-1511/llm-gateway/blob/main/.chroma_db/chroma.sqlite3"
click node_proxy "https://github.com/lokesh-1511/llm-gateway/blob/main/proxy_service.py"
click node_analytics "https://github.com/lokesh-1511/llm-gateway/blob/main/analytics.py"
click node_database "https://github.com/lokesh-1511/llm-gateway/blob/main/database.py"
click node_local_logs "https://github.com/lokesh-1511/llm-gateway/blob/main/llm_logs.db"
click node_frontend_entry "https://github.com/lokesh-1511/llm-gateway/blob/main/frontend/src/main.jsx"
click node_app "https://github.com/lokesh-1511/llm-gateway/blob/main/frontend/src/App.jsx"
click node_operations_ui "https://github.com/lokesh-1511/llm-gateway/blob/main/frontend/src/Dashboard.jsx"

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_main,node_models,node_routing,node_auth,node_dlp,node_compression,node_proxy toneBlue
class node_cache,node_chromadb,node_background,node_analytics,node_database,node_local_logs toneAmber
class node_frontend_entry,node_app,node_operations_ui toneMint
class node_supabase,node_groq toneRose
class node_client toneNeutral
```

## Components

### Gateway Runtime

FastAPI request path and policy services.

- [Chat Completions Gateway](https://github.com/lokesh-1511/llm-gateway/blob/main/main.py): FastAPI controller · Owns POST /v1/chat/completions orchestration and provider-compatible responses. · `main.py`
- [Shared Models](https://github.com/lokesh-1511/llm-gateway/blob/main/models.py): Python models · Request, response, and persistence model definitions. · `models.py`
- [Routing Policy](https://github.com/lokesh-1511/llm-gateway/blob/main/routing_service.py): routing service · Centralizes route and provider selection. · `routing_service.py`
- [Auth Service](https://github.com/lokesh-1511/llm-gateway/blob/main/auth_service.py): security service · Separates authentication and authorization from DLP. · `auth_service.py`
- [DLP Sanitizer](https://github.com/lokesh-1511/llm-gateway/blob/main/security_service.py): Presidio/SpaCy NER · Replaces detected sensitive entities before external transmission. · `security_service.py`
- [Context Compression](https://github.com/lokesh-1511/llm-gateway/blob/main/compression_service.py): optimization service · Reduces request or context size. · `compression_service.py`
- [Groq Proxy](https://github.com/lokesh-1511/llm-gateway/blob/main/proxy_service.py): async HTTP transport · Forwards misses to the Groq OpenAI-compatible API. · `proxy_service.py`

### Data & Background

Semantic storage, telemetry, and deferred writes.

- [Semantic Cache](https://github.com/lokesh-1511/llm-gateway/blob/main/cache_service.py): cache service · Finds semantically similar requests and reuses answers. · `cache_service.py`
- [ChromaDB Store](https://github.com/lokesh-1511/llm-gateway/blob/main/.chroma_db/chroma.sqlite3): vector database · Local persistent embeddings and collection data. · `.chroma_db/chroma.sqlite3`
- **Background Tasks**: FastAPI tasks · Defers cache population and telemetry writes after responding.
- [Request Analytics](https://github.com/lokesh-1511/llm-gateway/blob/main/analytics.py): telemetry service · Records latency, tokens, PII, cache status, and savings. · `analytics.py`
- [SQLAlchemy Database](https://github.com/lokesh-1511/llm-gateway/blob/main/database.py): persistence service · Provides application database connectivity. · `database.py`
- [Local Log Database](https://github.com/lokesh-1511/llm-gateway/blob/main/llm_logs.db): SQLite artifact · `llm_logs.db`

### Operator Control Plane

React/Vite operational UI.

- [Frontend Entry](https://github.com/lokesh-1511/llm-gateway/blob/main/frontend/src/main.jsx): React/Vite entry · `frontend/src/main.jsx`
- [Control Plane App](https://github.com/lokesh-1511/llm-gateway/blob/main/frontend/src/App.jsx): React composition · Composes operational and administrative views. · `frontend/src/App.jsx`
- [Operations Views](https://github.com/lokesh-1511/llm-gateway/blob/main/frontend/src/Dashboard.jsx): React dashboards · Dashboard, admin dashboard, and chat portal. · `frontend/src/Dashboard.jsx`

### External Services

Upstream inference and managed persistence.

- **Supabase PostgreSQL**: managed database · Intended managed PostgreSQL deployment.
- **Groq API**: external LLM provider · Configured by GROQ_API_KEY and GROQ_BASE_URL.

### Other components

- **API Clients**: client

## Connections

- API Clients → Chat Completions Gateway: POST chat completions
- Chat Completions Gateway → Shared Models: uses contracts
- Chat Completions Gateway → Auth Service: authorizes request
- Chat Completions Gateway → Routing Policy: selects route
- Chat Completions Gateway → DLP Sanitizer: sanitizes prompt
- Chat Completions Gateway → Context Compression: optimizes context
- Chat Completions Gateway → Semantic Cache: semantic lookup
- Semantic Cache → ChromaDB Store: embeddings and vectors
- Chat Completions Gateway → Groq Proxy: cache miss · Only sanitized payload proceeds upstream.
- Groq Proxy → Groq API: async OpenAI-compatible request
- Chat Completions Gateway → Background Tasks: schedules deferred work
- Background Tasks → Semantic Cache: populate on miss
- Background Tasks → Request Analytics: record telemetry
- Request Analytics → SQLAlchemy Database: persists metrics
- SQLAlchemy Database → Supabase PostgreSQL: managed deployment
- SQLAlchemy Database → Local Log Database: local artifact
- Frontend Entry → Control Plane App: mounts
- Control Plane App → Operations Views: renders
- Operations Views → Chat Completions Gateway: operates gateway
