# vikaschz/llm-gateway architecture

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

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

## Overview

llm-gateway is an LLM API gateway. A client submits a chat-completions request; the gateway checks its semantic cache, reserves tokens, selects a configured provider key, forwards the request, and records usage and cache data. Key rotation and rate limiting are distinct operational stages, with Redis-backed Lua scripts supporting rotation. The README also advertises cost tracking and budgets, but their implementation details are not visible in the excerpts. Edges below are limited to relationships shown in excerpts, the source index, or README.

Main entry points: [`main.py`](https://github.com/vikaschz/llm-gateway/blob/main/main.py) (Chat completion API), [`key_rotation.py`](https://github.com/vikaschz/llm-gateway/blob/main/key_rotation.py) (Key rotation), [`models.py`](https://github.com/vikaschz/llm-gateway/blob/main/models.py) (Request log model), [`config.py`](https://github.com/vikaschz/llm-gateway/blob/main/config.py) (Key configuration)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_request["Request gateway"]
  node_gateway["Chat completion API<br/>[main.py]"]
  node_cache["Semantic cache<br/>[main.py]"]
  node_tokenizer["Token estimation<br/>[main.py]"]
  node_rate_limit["Rate limiting<br/>[rate_limit.lua]"]
end

subgraph group_routing["Provider routing"]
  node_key_config["Key configuration<br/>[config.py]"]
  node_rotation["Key rotation<br/>[key_rotation.py]"]
  node_select_script["Key selection script"]
  node_true_up_script["Usage true-up script"]
  node_failed_script["Failed-key script"]
end

subgraph group_state["Usage and state"]
  node_usage["Cost and budget tracking<br/>[main.py]"]
  node_request_log["Request log model<br/>[models.py]"]
  node_semantic_model["Cache record model<br/>[models.py]"]
  node_database[("SQLite database<br/>[database.py]")]
end

node_client(("API client"))
node_groq["Groq provider"]
node_redis[("Redis state")]

node_client -->|"submits request"| node_gateway
node_gateway -->|"checks and updates"| node_cache
node_gateway -->|"estimates tokens"| node_tokenizer
node_gateway -->|"uses key config"| node_key_config
node_gateway -->|"registers script"| node_select_script
node_gateway -->|"registers script"| node_true_up_script
node_gateway -->|"registers script"| node_failed_script
node_gateway -->|"forwards request"| node_groq
node_gateway -->|"tracks usage"| node_usage
node_gateway -->|"writes logs"| node_request_log
node_gateway -->|"reads and writes"| node_semantic_model
node_request_log -->|"stored via"| node_database
node_semantic_model -->|"stored via"| node_database
node_rotation -->|"reconciles keys"| node_key_config
node_gateway -->|"runs scripts"| node_redis
node_gateway -.->|"applies limits"| node_rate_limit
node_gateway -.->|"selects and rotates keys"| node_rotation
node_gateway -.->|"tracks budgets"| node_redis

click node_gateway "https://github.com/vikaschz/llm-gateway/blob/main/main.py"
click node_cache "https://github.com/vikaschz/llm-gateway/blob/main/main.py"
click node_tokenizer "https://github.com/vikaschz/llm-gateway/blob/main/main.py"
click node_rate_limit "https://github.com/vikaschz/llm-gateway/blob/main/rate_limit.lua"
click node_key_config "https://github.com/vikaschz/llm-gateway/blob/main/config.py"
click node_rotation "https://github.com/vikaschz/llm-gateway/blob/main/key_rotation.py"
click node_select_script "https://github.com/vikaschz/llm-gateway/blob/main/rotation_select_key.lua"
click node_true_up_script "https://github.com/vikaschz/llm-gateway/blob/main/rotation_true_up.lua"
click node_failed_script "https://github.com/vikaschz/llm-gateway/blob/main/rotation_mark_failed.lua"
click node_usage "https://github.com/vikaschz/llm-gateway/blob/main/main.py"
click node_request_log "https://github.com/vikaschz/llm-gateway/blob/main/models.py"
click node_semantic_model "https://github.com/vikaschz/llm-gateway/blob/main/models.py"
click node_database "https://github.com/vikaschz/llm-gateway/blob/main/database.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_gateway,node_cache,node_tokenizer,node_rate_limit,node_client toneBlue
class node_key_config,node_rotation,node_select_script,node_true_up_script,node_failed_script,node_redis toneAmber
class node_usage,node_request_log,node_semantic_model,node_database toneMint
class node_groq toneIndigo
```

## Components

### Request gateway

- [Chat completion API](https://github.com/vikaschz/llm-gateway/blob/main/main.py): `main.py`
- [Semantic cache](https://github.com/vikaschz/llm-gateway/blob/main/main.py): `main.py`
- [Token estimation](https://github.com/vikaschz/llm-gateway/blob/main/main.py): `main.py`
- [Rate limiting](https://github.com/vikaschz/llm-gateway/blob/main/rate_limit.lua): `rate_limit.lua`

### Provider routing

- [Key configuration](https://github.com/vikaschz/llm-gateway/blob/main/config.py): `config.py`
- [Key rotation](https://github.com/vikaschz/llm-gateway/blob/main/key_rotation.py): `key_rotation.py`
- [Key selection script](https://github.com/vikaschz/llm-gateway/blob/main/rotation_select_key.lua): `rotation_select_key.lua`
- [Usage true-up script](https://github.com/vikaschz/llm-gateway/blob/main/rotation_true_up.lua): `rotation_true_up.lua`
- [Failed-key script](https://github.com/vikaschz/llm-gateway/blob/main/rotation_mark_failed.lua): `rotation_mark_failed.lua`

### Usage and state

- [Cost and budget tracking](https://github.com/vikaschz/llm-gateway/blob/main/main.py): `main.py`
- [Request log model](https://github.com/vikaschz/llm-gateway/blob/main/models.py): `models.py`
- [Cache record model](https://github.com/vikaschz/llm-gateway/blob/main/models.py): `models.py`
- [SQLite database](https://github.com/vikaschz/llm-gateway/blob/main/database.py): `database.py`

### Other components

- **API client**
- **Groq provider**
- **Redis state**

## Connections

- API client → Chat completion API: submits request (evidence: `README.md`)
- Chat completion API → Semantic cache: checks and updates (evidence: `main.py`)
- Chat completion API → Token estimation: estimates tokens (evidence: `main.py`)
- Chat completion API → Key configuration: uses key config (evidence: `main.py`)
- Chat completion API → Key selection script: registers script (evidence: `main.py`)
- Chat completion API → Usage true-up script: registers script (evidence: `main.py`)
- Chat completion API → Failed-key script: registers script (evidence: `main.py`)
- Chat completion API → Groq provider: forwards request (evidence: `main.py`)
- Chat completion API → Cost and budget tracking: tracks usage (evidence: `README.md`)
- Chat completion API → Request log model: writes logs (evidence: `main.py`)
- Chat completion API → Cache record model: reads and writes (evidence: `main.py`)
- Request log model → SQLite database: stored via (evidence: `models.py`)
- Cache record model → SQLite database: stored via (evidence: `models.py`)
- Key rotation → Key configuration: reconciles keys (evidence: `key_rotation.py`)
- Chat completion API → Redis state: runs scripts (evidence: `main.py`)
- Chat completion API → Rate limiting: applies limits
- Chat completion API → Key rotation: selects and rotates keys (evidence: `main.py`)
- Chat completion API → Redis state: tracks budgets
