# mayureshmohnalkar22/llm-cost-autopilot architecture

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

- Interactive diagram: https://gitdiagram.com/mayureshmohnalkar22/llm-cost-autopilot
- Repository: https://github.com/mayureshmohnalkar22/llm-cost-autopilot

## Overview

LLM Cost Autopilot is a local Ollama gateway that routes prompts across three sub-3B models, checks outputs, and escalates failures. The main workflow is API submission → complexity-based tier selection → inference → deterministic or sampled judge verification → durable result and attempts. A background worker handles pending verification, while persisted escalation history can raise future routing floors. The dashboard reads gateway results and resource metrics; a separate benchmark compares routing against the largest-model baseline. Edges reflect shown calls or README-documented behavior; unsampled benchmark implementation details are not inferred.

Main entry points: [`app/main.py`](https://github.com/mayureshmohnalkar22/llm-cost-autopilot/blob/main/app/main.py) (FastAPI Gateway), [`app/engine.py`](https://github.com/mayureshmohnalkar22/llm-cost-autopilot/blob/main/app/engine.py) (Request Engine), [`app/provider.py`](https://github.com/mayureshmohnalkar22/llm-cost-autopilot/blob/main/app/provider.py) (Ollama Provider), [`app/database.py`](https://github.com/mayureshmohnalkar22/llm-cost-autopilot/blob/main/app/database.py) (SQLite Store and Queue)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_gateway["Gateway and Routing"]
  node_api["FastAPI Gateway<br/>[main.py]"]
  node_settings["Runtime Settings<br/>[config.py]"]
  node_classifier["Complexity Classifier<br/>[routing.py]"]
  node_engine["Request Engine<br/>[engine.py]"]
  node_model_config["Model Definitions<br/>[models.json]"]
end

subgraph group_quality["Inference and Quality"]
  node_worker["Verification Worker<br/>[engine.py]"]
  node_ollama["Ollama Provider<br/>[provider.py]"]
  node_evaluation["Response Checks<br/>[evaluation.py]"]
end

subgraph group_state["Persistence and Metrics"]
  node_store[("SQLite Store and Queue<br/>[database.py]")]
  node_monitoring["Host Monitoring<br/>[monitoring.py]"]
end

subgraph group_clients["User Interfaces"]
  node_dashboard["Streamlit Dashboard<br/>[app.py]"]
end

subgraph group_analysis["Benchmark and Training"]
  node_benchmark["Paired Benchmark<br/>[benchmark.py]"]
  node_trainer["Classifier Training"]
end

node_user(("User"))
node_ollama_service["Ollama Service"]

node_user -->|"submits prompt"| node_api
node_api -->|"loads settings"| node_settings
node_api -->|"submits request"| node_engine
node_settings -->|"loads models"| node_model_config
node_engine -->|"predicts tier"| node_classifier
node_engine -->|"reads floor; records request"| node_store
node_engine -->|"generates response"| node_ollama
node_engine -->|"checks response"| node_evaluation
node_api -->|"starts worker"| node_worker
node_worker -->|"claims pending work"| node_store
node_worker -->|"checks and judges"| node_evaluation
node_worker -->|"judges or escalates"| node_ollama
node_ollama -->|"calls inference API"| node_ollama_service
node_ollama -->|"samples resources"| node_monitoring
node_api -->|"reads requests and metrics"| node_store
node_api -->|"serves resource snapshot"| node_monitoring
node_dashboard -->|"requests results and metrics"| node_api
node_benchmark -->|"compares routing policies"| node_api
node_trainer -.->|"provides optional model artifact"| node_classifier

click node_api "https://github.com/mayureshmohnalkar22/llm-cost-autopilot/blob/main/app/main.py"
click node_settings "https://github.com/mayureshmohnalkar22/llm-cost-autopilot/blob/main/app/config.py"
click node_classifier "https://github.com/mayureshmohnalkar22/llm-cost-autopilot/blob/main/app/routing.py"
click node_engine "https://github.com/mayureshmohnalkar22/llm-cost-autopilot/blob/main/app/engine.py"
click node_worker "https://github.com/mayureshmohnalkar22/llm-cost-autopilot/blob/main/app/engine.py"
click node_ollama "https://github.com/mayureshmohnalkar22/llm-cost-autopilot/blob/main/app/provider.py"
click node_evaluation "https://github.com/mayureshmohnalkar22/llm-cost-autopilot/blob/main/app/evaluation.py"
click node_store "https://github.com/mayureshmohnalkar22/llm-cost-autopilot/blob/main/app/database.py"
click node_monitoring "https://github.com/mayureshmohnalkar22/llm-cost-autopilot/blob/main/app/monitoring.py"
click node_dashboard "https://github.com/mayureshmohnalkar22/llm-cost-autopilot/blob/main/dashboard/app.py"
click node_benchmark "https://github.com/mayureshmohnalkar22/llm-cost-autopilot/blob/main/scripts/benchmark.py"
click node_trainer "https://github.com/mayureshmohnalkar22/llm-cost-autopilot/blob/main/scripts/train_classifier.py"
click node_model_config "https://github.com/mayureshmohnalkar22/llm-cost-autopilot/blob/main/config/models.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_api,node_settings,node_classifier,node_engine,node_model_config,node_user toneBlue
class node_worker,node_ollama,node_evaluation toneAmber
class node_store,node_monitoring toneMint
class node_dashboard toneRose
class node_benchmark,node_trainer,node_ollama_service toneIndigo
```

## Components

### Gateway and Routing

- [FastAPI Gateway](https://github.com/mayureshmohnalkar22/llm-cost-autopilot/blob/main/app/main.py): `app/main.py`
- [Runtime Settings](https://github.com/mayureshmohnalkar22/llm-cost-autopilot/blob/main/app/config.py): `app/config.py`
- [Complexity Classifier](https://github.com/mayureshmohnalkar22/llm-cost-autopilot/blob/main/app/routing.py): `app/routing.py`
- [Request Engine](https://github.com/mayureshmohnalkar22/llm-cost-autopilot/blob/main/app/engine.py): `app/engine.py`
- [Model Definitions](https://github.com/mayureshmohnalkar22/llm-cost-autopilot/blob/main/config/models.json): `config/models.json`

### Inference and Quality

- [Verification Worker](https://github.com/mayureshmohnalkar22/llm-cost-autopilot/blob/main/app/engine.py): `app/engine.py`
- [Ollama Provider](https://github.com/mayureshmohnalkar22/llm-cost-autopilot/blob/main/app/provider.py): `app/provider.py`
- [Response Checks](https://github.com/mayureshmohnalkar22/llm-cost-autopilot/blob/main/app/evaluation.py): `app/evaluation.py`

### Persistence and Metrics

- [SQLite Store and Queue](https://github.com/mayureshmohnalkar22/llm-cost-autopilot/blob/main/app/database.py): `app/database.py`
- [Host Monitoring](https://github.com/mayureshmohnalkar22/llm-cost-autopilot/blob/main/app/monitoring.py): `app/monitoring.py`

### User Interfaces

- [Streamlit Dashboard](https://github.com/mayureshmohnalkar22/llm-cost-autopilot/blob/main/dashboard/app.py): `dashboard/app.py`

### Benchmark and Training

- [Paired Benchmark](https://github.com/mayureshmohnalkar22/llm-cost-autopilot/blob/main/scripts/benchmark.py): `scripts/benchmark.py`
- [Classifier Training](https://github.com/mayureshmohnalkar22/llm-cost-autopilot/blob/main/scripts/train_classifier.py): `scripts/train_classifier.py`

### Other components

- **User**
- **Ollama Service**

## Connections

- User → FastAPI Gateway: submits prompt (evidence: `README.md`)
- FastAPI Gateway → Runtime Settings: loads settings (evidence: `app/main.py`)
- FastAPI Gateway → Request Engine: submits request (evidence: `app/main.py`)
- Runtime Settings → Model Definitions: loads models (evidence: `app/config.py`)
- Request Engine → Complexity Classifier: predicts tier (evidence: `app/engine.py`)
- Request Engine → SQLite Store and Queue: reads floor; records request (evidence: `app/engine.py`)
- Request Engine → Ollama Provider: generates response (evidence: `app/engine.py`)
- Request Engine → Response Checks: checks response (evidence: `app/engine.py`)
- FastAPI Gateway → Verification Worker: starts worker (evidence: `app/main.py`)
- Verification Worker → SQLite Store and Queue: claims pending work (evidence: `app/engine.py`)
- Verification Worker → Response Checks: checks and judges (evidence: `app/engine.py`)
- Verification Worker → Ollama Provider: judges or escalates (evidence: `app/engine.py`)
- Ollama Provider → Ollama Service: calls inference API (evidence: `README.md`)
- Ollama Provider → Host Monitoring: samples resources (evidence: `app/provider.py`)
- FastAPI Gateway → SQLite Store and Queue: reads requests and metrics (evidence: `app/main.py`)
- FastAPI Gateway → Host Monitoring: serves resource snapshot (evidence: `app/main.py`)
- Streamlit Dashboard → FastAPI Gateway: requests results and metrics (evidence: `dashboard/app.py`)
- Paired Benchmark → FastAPI Gateway: compares routing policies (evidence: `README.md`)
- Classifier Training → Complexity Classifier: provides optional model artifact (evidence: `README.md`)
