How sujpatel/llm-cost-optimization works
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: main.py FastAPI service · db.py SQLite catalog · scheduler.py Refresh scheduler · sync_models.py Model price sync
Components 13
API workflows
- FastAPI service
app/main.py - Savings and gap calculation
app/cost.py - Query logging
app/models.py - Savings summary
app/main.py
Model routing
- Model matcher
app/matching.py - Completion client
app/completion.py
Model catalog
- SQLite catalog
app/db.py - Catalog and query models
app/models.py
Catalog refresh
- Refresh scheduler
app/scheduler.py - Model price sync
app/sync_models.py - Benchmark sync
app/sync_benchmarks.py
Other components
- API client
- OpenRouter API
Connections 17
- API client → FastAPI service: calls endpoints
README.md - FastAPI service → Model matcher: selects models
app/main.py - Model matcher → SQLite catalog: queries catalog
app/matching.py - FastAPI service → Completion client: requests answer
app/main.py - Completion client → OpenRouter API: sends completion
app/completion.py - FastAPI service → Savings and gap calculation: calculates savings
app/main.py - FastAPI service → Query logging: records request
app/main.py - Query logging → SQLite catalog: persists via session
app/main.py - FastAPI service → Savings summary: serves summary
app/main.py - Savings summary → SQLite catalog: aggregates queries
app/main.py - FastAPI service → Refresh scheduler: starts at startup
app/main.py - Refresh scheduler → Model price sync: runs refresh
app/scheduler.py - Refresh scheduler → Benchmark sync: runs refresh
app/scheduler.py - Model price sync → OpenRouter API: fetches model data
app/sync_models.py - Model price sync → SQLite catalog: upserts prices
app/sync_models.py - Benchmark sync → OpenRouter API: fetches benchmarks
app/sync_benchmarks.py - Benchmark sync → SQLite catalog: upserts scores
app/sync_benchmarks.py