# saiohmsaiepaine/cost-aware-llm-gateway architecture

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

- Interactive diagram: https://gitdiagram.com/saiohmsaiepaine/cost-aware-llm-gateway
- Repository: https://github.com/saiohmsaiepaine/cost-aware-llm-gateway

## Overview

This is an OpenAI-compatible LLM gateway that reduces inference cost and latency through semantic caching, complexity-based model selection, and failover. A chat request enters the API, is authenticated and rate-limited, embedded and classified, then checked against a guarded Redis cache; misses are dispatched to a local or cloud model, with circuit-breaker failover. The README also documents health/admin endpoints and Prometheus/Grafana observability. Relationships reflect the README and shown source/index references; unsampled implementation details are not inferred beyond documented capabilities.

Main entry points: [`src/api/router.py`](https://github.com/saiohmsaiepaine/cost-aware-llm-gateway/blob/main/src/api/router.py) (Chat API), [`src/routing/router.py`](https://github.com/saiohmsaiepaine/cost-aware-llm-gateway/blob/main/src/routing/router.py) (Tier Dispatch), [`src/cache/semantic_cache.py`](https://github.com/saiohmsaiepaine/cost-aware-llm-gateway/blob/main/src/cache/semantic_cache.py) (Cache Lookup & Writes), [`src/telemetry/stats.py`](https://github.com/saiohmsaiepaine/cost-aware-llm-gateway/blob/main/src/telemetry/stats.py) (Request Statistics)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_request["Request API"]
  node_gateway["Chat API<br/>[router.py]"]
  node_auth["Auth &amp; Limits<br/>[dependencies.py]"]
  node_api_models["API Schemas<br/>[schemas.py]"]
  node_settings["Runtime Settings<br/>[config.py]"]
end

subgraph group_cache["Semantic Cache"]
  node_embedder["ONNX Embedder<br/>[embedder.py]"]
  node_semantic_cache["Cache Lookup &amp; Writes<br/>[semantic_cache.py]"]
  node_guardrails["Cache Guardrails<br/>[guardrails.py]"]
  node_redis[("Redis Vector Index<br/>[redis_client.py]")]
end

subgraph group_routing["Model Routing"]
  node_classifier["Complexity Scorer<br/>[classifier.py]"]
  node_model_router["Tier Dispatch<br/>[router.py]"]
  node_breaker["Circuit Breaker<br/>[circuit_breaker.py]"]
  node_client_contract["LLM Client Contract<br/>[base.py]"]
  node_local_model["Local Model Client<br/>[vllm_client.py]"]
  node_cloud_model["Gemini Client<br/>[cloud_client.py]"]
end

subgraph group_telemetry["Operations"]
  node_health["Health &amp; Admin API<br/>[health.py]"]
  node_stats["Request Statistics<br/>[stats.py]"]
  node_metrics["Prometheus Metrics<br/>[metrics.py]"]
end

node_client_app(("API Client"))
node_prometheus["Prometheus<br/>[prometheus.yml]"]
node_grafana["Grafana Dashboard"]

node_client_app -->|"sends chat"| node_gateway
node_gateway -->|"authenticates"| node_auth
node_gateway -->|"validates"| node_api_models
node_gateway -->|"embeds prompt"| node_embedder
node_gateway -->|"classifies and dispatches"| node_model_router
node_gateway -->|"looks up and schedules writes"| node_semantic_cache
node_semantic_cache -->|"checks candidates"| node_guardrails
node_semantic_cache -->|"queries and stores"| node_redis
node_embedder -->|"builds semantic centroids"| node_classifier
node_model_router -->|"scores prompts"| node_classifier
node_model_router -->|"checks cloud availability"| node_breaker
node_model_router -->|"dispatches local"| node_local_model
node_model_router -->|"dispatches cloud"| node_cloud_model
node_local_model -->|"implements"| node_client_contract
node_cloud_model -->|"implements"| node_client_contract
node_gateway -->|"records request outcomes"| node_stats
node_health -->|"reads service statistics"| node_stats
node_health -->|"exposes metrics"| node_metrics
node_stats -->|"updates collectors"| node_metrics
node_settings -->|"configures policies"| node_model_router
node_settings -->|"configures index"| node_redis
node_prometheus -->|"scrapes endpoint"| node_metrics
node_grafana -->|"visualizes metrics"| node_prometheus

click node_gateway "https://github.com/saiohmsaiepaine/cost-aware-llm-gateway/blob/main/src/api/router.py"
click node_auth "https://github.com/saiohmsaiepaine/cost-aware-llm-gateway/blob/main/src/api/dependencies.py"
click node_api_models "https://github.com/saiohmsaiepaine/cost-aware-llm-gateway/blob/main/src/api/schemas.py"
click node_settings "https://github.com/saiohmsaiepaine/cost-aware-llm-gateway/blob/main/src/config.py"
click node_embedder "https://github.com/saiohmsaiepaine/cost-aware-llm-gateway/blob/main/src/cache/embedder.py"
click node_classifier "https://github.com/saiohmsaiepaine/cost-aware-llm-gateway/blob/main/src/routing/classifier.py"
click node_semantic_cache "https://github.com/saiohmsaiepaine/cost-aware-llm-gateway/blob/main/src/cache/semantic_cache.py"
click node_guardrails "https://github.com/saiohmsaiepaine/cost-aware-llm-gateway/blob/main/src/cache/guardrails.py"
click node_redis "https://github.com/saiohmsaiepaine/cost-aware-llm-gateway/blob/main/src/cache/redis_client.py"
click node_model_router "https://github.com/saiohmsaiepaine/cost-aware-llm-gateway/blob/main/src/routing/router.py"
click node_breaker "https://github.com/saiohmsaiepaine/cost-aware-llm-gateway/blob/main/src/routing/circuit_breaker.py"
click node_client_contract "https://github.com/saiohmsaiepaine/cost-aware-llm-gateway/blob/main/src/client/base.py"
click node_local_model "https://github.com/saiohmsaiepaine/cost-aware-llm-gateway/blob/main/src/client/vllm_client.py"
click node_cloud_model "https://github.com/saiohmsaiepaine/cost-aware-llm-gateway/blob/main/src/client/cloud_client.py"
click node_health "https://github.com/saiohmsaiepaine/cost-aware-llm-gateway/blob/main/src/api/health.py"
click node_stats "https://github.com/saiohmsaiepaine/cost-aware-llm-gateway/blob/main/src/telemetry/stats.py"
click node_metrics "https://github.com/saiohmsaiepaine/cost-aware-llm-gateway/blob/main/src/telemetry/metrics.py"
click node_prometheus "https://github.com/saiohmsaiepaine/cost-aware-llm-gateway/blob/main/config/prometheus.yml"
click node_grafana "https://github.com/saiohmsaiepaine/cost-aware-llm-gateway/blob/main/config/grafana_dashboard.json"

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_gateway,node_auth,node_api_models,node_settings,node_client_app toneBlue
class node_embedder,node_semantic_cache,node_guardrails,node_redis toneAmber
class node_classifier,node_model_router,node_breaker,node_client_contract,node_local_model,node_cloud_model toneMint
class node_health,node_stats,node_metrics toneRose
class node_prometheus,node_grafana toneTeal
```

## Components

### Request API

- [Chat API](https://github.com/saiohmsaiepaine/cost-aware-llm-gateway/blob/main/src/api/router.py): `src/api/router.py`
- [Auth & Limits](https://github.com/saiohmsaiepaine/cost-aware-llm-gateway/blob/main/src/api/dependencies.py): `src/api/dependencies.py`
- [API Schemas](https://github.com/saiohmsaiepaine/cost-aware-llm-gateway/blob/main/src/api/schemas.py): `src/api/schemas.py`
- [Runtime Settings](https://github.com/saiohmsaiepaine/cost-aware-llm-gateway/blob/main/src/config.py): `src/config.py`

### Semantic Cache

- [ONNX Embedder](https://github.com/saiohmsaiepaine/cost-aware-llm-gateway/blob/main/src/cache/embedder.py): `src/cache/embedder.py`
- [Cache Lookup & Writes](https://github.com/saiohmsaiepaine/cost-aware-llm-gateway/blob/main/src/cache/semantic_cache.py): `src/cache/semantic_cache.py`
- [Cache Guardrails](https://github.com/saiohmsaiepaine/cost-aware-llm-gateway/blob/main/src/cache/guardrails.py): `src/cache/guardrails.py`
- [Redis Vector Index](https://github.com/saiohmsaiepaine/cost-aware-llm-gateway/blob/main/src/cache/redis_client.py): `src/cache/redis_client.py`

### Model Routing

- [Complexity Scorer](https://github.com/saiohmsaiepaine/cost-aware-llm-gateway/blob/main/src/routing/classifier.py): `src/routing/classifier.py`
- [Tier Dispatch](https://github.com/saiohmsaiepaine/cost-aware-llm-gateway/blob/main/src/routing/router.py): `src/routing/router.py`
- [Circuit Breaker](https://github.com/saiohmsaiepaine/cost-aware-llm-gateway/blob/main/src/routing/circuit_breaker.py): `src/routing/circuit_breaker.py`
- [LLM Client Contract](https://github.com/saiohmsaiepaine/cost-aware-llm-gateway/blob/main/src/client/base.py): `src/client/base.py`
- [Local Model Client](https://github.com/saiohmsaiepaine/cost-aware-llm-gateway/blob/main/src/client/vllm_client.py): `src/client/vllm_client.py`
- [Gemini Client](https://github.com/saiohmsaiepaine/cost-aware-llm-gateway/blob/main/src/client/cloud_client.py): `src/client/cloud_client.py`

### Operations

- [Health & Admin API](https://github.com/saiohmsaiepaine/cost-aware-llm-gateway/blob/main/src/api/health.py): `src/api/health.py`
- [Request Statistics](https://github.com/saiohmsaiepaine/cost-aware-llm-gateway/blob/main/src/telemetry/stats.py): `src/telemetry/stats.py`
- [Prometheus Metrics](https://github.com/saiohmsaiepaine/cost-aware-llm-gateway/blob/main/src/telemetry/metrics.py): `src/telemetry/metrics.py`

### Other components

- **API Client**
- [Prometheus](https://github.com/saiohmsaiepaine/cost-aware-llm-gateway/blob/main/config/prometheus.yml): `config/prometheus.yml`
- [Grafana Dashboard](https://github.com/saiohmsaiepaine/cost-aware-llm-gateway/blob/main/config/grafana_dashboard.json): `config/grafana_dashboard.json`

## Connections

- API Client → Chat API: sends chat (evidence: `README.md`)
- Chat API → Auth & Limits: authenticates (evidence: `src/api/router.py`)
- Chat API → API Schemas: validates (evidence: `src/api/router.py`)
- Chat API → ONNX Embedder: embeds prompt (evidence: `src/api/router.py`)
- Chat API → Tier Dispatch: classifies and dispatches (evidence: `src/api/router.py`)
- Chat API → Cache Lookup & Writes: looks up and schedules writes (evidence: `src/api/router.py`)
- Cache Lookup & Writes → Cache Guardrails: checks candidates (evidence: `src/cache/semantic_cache.py`)
- Cache Lookup & Writes → Redis Vector Index: queries and stores (evidence: `src/cache/semantic_cache.py`)
- ONNX Embedder → Complexity Scorer: builds semantic centroids (evidence: `src/cache/embedder.py`)
- Tier Dispatch → Complexity Scorer: scores prompts (evidence: `src/routing/router.py`)
- Tier Dispatch → Circuit Breaker: checks cloud availability (evidence: `README.md`)
- Tier Dispatch → Local Model Client: dispatches local (evidence: `README.md`)
- Tier Dispatch → Gemini Client: dispatches cloud (evidence: `README.md`)
- Local Model Client → LLM Client Contract: implements (evidence: `src/client/vllm_client.py`)
- Gemini Client → LLM Client Contract: implements (evidence: `src/client/cloud_client.py`)
- Chat API → Request Statistics: records request outcomes (evidence: `README.md`)
- Health & Admin API → Request Statistics: reads service statistics (evidence: `src/api/health.py`)
- Health & Admin API → Prometheus Metrics: exposes metrics (evidence: `src/api/health.py`)
- Request Statistics → Prometheus Metrics: updates collectors (evidence: `src/telemetry/stats.py`)
- Runtime Settings → Tier Dispatch: configures policies (evidence: `src/routing/router.py`)
- Runtime Settings → Redis Vector Index: configures index (evidence: `src/cache/redis_client.py`)
- Prometheus → Prometheus Metrics: scrapes endpoint (evidence: `README.md`)
- Grafana Dashboard → Prometheus: visualizes metrics (evidence: `README.md`)
