# sujpatel/llm-cost-optimization architecture

> Architecture diagram of the GitHub repository sujpatel/llm-cost-optimization, 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/sujpatel/llm-cost-optimization
- Repository: https://github.com/sujpatel/llm-cost-optimization

## Overview

This FastAPI service routes prompts to a benchmark-ranked model within a price cap, compares it with the top-ranked paid reference, and reports estimated savings and quality gap. Its main workflow runs through the API handler, model matching, OpenRouter completion, cost calculation, and query logging; a separate savings endpoint aggregates logged queries. Model prices and task benchmarks are refreshed by scheduled sync jobs. The excerpts show the API and sync call sites; external API relationships are documented or directly visible there.

Main entry points: [`app/main.py`](https://github.com/sujpatel/llm-cost-optimization/blob/main/app/main.py) (FastAPI service), [`app/db.py`](https://github.com/sujpatel/llm-cost-optimization/blob/main/app/db.py) (SQLite catalog), [`app/scheduler.py`](https://github.com/sujpatel/llm-cost-optimization/blob/main/app/scheduler.py) (Refresh scheduler), [`app/sync_models.py`](https://github.com/sujpatel/llm-cost-optimization/blob/main/app/sync_models.py) (Model price sync)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["API workflows"]
  node_api["FastAPI service<br/>[main.py]"]
  node_cost["Savings and gap calculation<br/>[cost.py]"]
  node_querylog["Query logging<br/>[models.py]"]
  node_savings["Savings summary<br/>[main.py]"]
end

subgraph group_routing["Model routing"]
  node_matcher["Model matcher<br/>[matching.py]"]
  node_completion["Completion client<br/>[completion.py]"]
end

subgraph group_catalog["Model catalog"]
  node_catalogdb[("SQLite catalog<br/>[db.py]")]
  node_entities["Catalog and query models<br/>[models.py]"]
end

subgraph group_refresh["Catalog refresh"]
  node_scheduler["Refresh scheduler<br/>[scheduler.py]"]
  node_modelsync["Model price sync<br/>[sync_models.py]"]
  node_benchsync["Benchmark sync<br/>[sync_benchmarks.py]"]
end

node_client(("API client"))
node_router{{"OpenRouter API"}}

node_client -->|"calls endpoints"| node_api
node_api -->|"selects models"| node_matcher
node_matcher -->|"queries catalog"| node_catalogdb
node_api -->|"requests answer"| node_completion
node_completion -->|"sends completion"| node_router
node_api -->|"calculates savings"| node_cost
node_api -->|"records request"| node_querylog
node_querylog -->|"persists via session"| node_catalogdb
node_api -->|"serves summary"| node_savings
node_savings -->|"aggregates queries"| node_catalogdb
node_api -->|"starts at startup"| node_scheduler
node_scheduler -->|"runs refresh"| node_modelsync
node_scheduler -->|"runs refresh"| node_benchsync
node_modelsync -->|"fetches model data"| node_router
node_modelsync -->|"upserts prices"| node_catalogdb
node_benchsync -->|"fetches benchmarks"| node_router
node_benchsync -->|"upserts scores"| node_catalogdb

click node_api "https://github.com/sujpatel/llm-cost-optimization/blob/main/app/main.py"
click node_matcher "https://github.com/sujpatel/llm-cost-optimization/blob/main/app/matching.py"
click node_catalogdb "https://github.com/sujpatel/llm-cost-optimization/blob/main/app/db.py"
click node_entities "https://github.com/sujpatel/llm-cost-optimization/blob/main/app/models.py"
click node_completion "https://github.com/sujpatel/llm-cost-optimization/blob/main/app/completion.py"
click node_cost "https://github.com/sujpatel/llm-cost-optimization/blob/main/app/cost.py"
click node_querylog "https://github.com/sujpatel/llm-cost-optimization/blob/main/app/models.py"
click node_savings "https://github.com/sujpatel/llm-cost-optimization/blob/main/app/main.py"
click node_scheduler "https://github.com/sujpatel/llm-cost-optimization/blob/main/app/scheduler.py"
click node_modelsync "https://github.com/sujpatel/llm-cost-optimization/blob/main/app/sync_models.py"
click node_benchsync "https://github.com/sujpatel/llm-cost-optimization/blob/main/app/sync_benchmarks.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_cost,node_querylog,node_savings,node_client toneBlue
class node_matcher,node_completion toneAmber
class node_catalogdb,node_entities,node_router toneMint
class node_scheduler,node_modelsync,node_benchsync toneRose
```

## Components

### API workflows

- [FastAPI service](https://github.com/sujpatel/llm-cost-optimization/blob/main/app/main.py): `app/main.py`
- [Savings and gap calculation](https://github.com/sujpatel/llm-cost-optimization/blob/main/app/cost.py): `app/cost.py`
- [Query logging](https://github.com/sujpatel/llm-cost-optimization/blob/main/app/models.py): `app/models.py`
- [Savings summary](https://github.com/sujpatel/llm-cost-optimization/blob/main/app/main.py): `app/main.py`

### Model routing

- [Model matcher](https://github.com/sujpatel/llm-cost-optimization/blob/main/app/matching.py): `app/matching.py`
- [Completion client](https://github.com/sujpatel/llm-cost-optimization/blob/main/app/completion.py): `app/completion.py`

### Model catalog

- [SQLite catalog](https://github.com/sujpatel/llm-cost-optimization/blob/main/app/db.py): `app/db.py`
- [Catalog and query models](https://github.com/sujpatel/llm-cost-optimization/blob/main/app/models.py): `app/models.py`

### Catalog refresh

- [Refresh scheduler](https://github.com/sujpatel/llm-cost-optimization/blob/main/app/scheduler.py): `app/scheduler.py`
- [Model price sync](https://github.com/sujpatel/llm-cost-optimization/blob/main/app/sync_models.py): `app/sync_models.py`
- [Benchmark sync](https://github.com/sujpatel/llm-cost-optimization/blob/main/app/sync_benchmarks.py): `app/sync_benchmarks.py`

### Other components

- **API client**
- **OpenRouter API**

## Connections

- API client → FastAPI service: calls endpoints (evidence: `README.md`)
- FastAPI service → Model matcher: selects models (evidence: `app/main.py`)
- Model matcher → SQLite catalog: queries catalog (evidence: `app/matching.py`)
- FastAPI service → Completion client: requests answer (evidence: `app/main.py`)
- Completion client → OpenRouter API: sends completion (evidence: `app/completion.py`)
- FastAPI service → Savings and gap calculation: calculates savings (evidence: `app/main.py`)
- FastAPI service → Query logging: records request (evidence: `app/main.py`)
- Query logging → SQLite catalog: persists via session (evidence: `app/main.py`)
- FastAPI service → Savings summary: serves summary (evidence: `app/main.py`)
- Savings summary → SQLite catalog: aggregates queries (evidence: `app/main.py`)
- FastAPI service → Refresh scheduler: starts at startup (evidence: `app/main.py`)
- Refresh scheduler → Model price sync: runs refresh (evidence: `app/scheduler.py`)
- Refresh scheduler → Benchmark sync: runs refresh (evidence: `app/scheduler.py`)
- Model price sync → OpenRouter API: fetches model data (evidence: `app/sync_models.py`)
- Model price sync → SQLite catalog: upserts prices (evidence: `app/sync_models.py`)
- Benchmark sync → OpenRouter API: fetches benchmarks (evidence: `app/sync_benchmarks.py`)
- Benchmark sync → SQLite catalog: upserts scores (evidence: `app/sync_benchmarks.py`)
