# rachitsethi2455/cost-aware-model-router architecture

> Architecture diagram of the GitHub repository rachitsethi2455/cost-aware-model-router, 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/rachitsethi2455/cost-aware-model-router
- Repository: https://github.com/rachitsethi2455/cost-aware-model-router

## Overview

Adaptive LLM Router accepts a query through its API, extracts interpretable features, chooses a model tier, calls a provider, and checks small-model answers for deterministic escalation before returning answer and cost metadata. The API also exposes a no-inference explanation route and health/statistics endpoints. Separate evaluation tools train and analyze the router and benchmark routing modes. The graph follows the supplied excerpts, SOURCE INDEX references, and README; unsampled API details and provider registration behavior are not expanded beyond those references.

Main entry points: [`src/router/pipeline.py`](https://github.com/rachitsethi2455/cost-aware-model-router/blob/master/src/router/pipeline.py) (Routing pipeline), [`src/router/llm.py`](https://github.com/rachitsethi2455/cost-aware-model-router/blob/master/src/router/llm.py) (LLM client), [`src/router/classifier.py`](https://github.com/rachitsethi2455/cost-aware-model-router/blob/master/src/router/classifier.py) (Learned router), [`src/api/main.py`](https://github.com/rachitsethi2455/cost-aware-model-router/blob/master/src/api/main.py) (HTTP API and demo)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_service["API service"]
  node_api["HTTP API and demo<br/>[main.py]"]
end

subgraph group_routing["Routing and quality"]
  node_pipeline["Routing pipeline<br/>[pipeline.py]"]
  node_features["Query features<br/>[features.py]"]
  node_classifier["Learned router<br/>[classifier.py]"]
  node_heuristic["Heuristic router<br/>[heuristic.py]"]
  node_escalation["Quality escalation<br/>[escalation.py]"]
end

subgraph group_inference["Inference and providers"]
  node_config["Model configuration<br/>[config.py]"]
  node_llm["LLM client<br/>[llm.py]"]
  node_cache[("Response cache<br/>[cache.py]")]
  node_provider_registry["Provider selection<br/>[__init__.py]"]
  node_provider_base["Provider interface<br/>[base.py]"]
  node_anthropic["Anthropic provider"]
  node_openai_compat["OpenAI-compatible provider<br/>[openai_compat.py]"]
  node_call_result["Call result and costs<br/>[types.py]"]
end

subgraph group_evaluation["Router evaluation"]
  node_train["Router training<br/>[train_router.py]"]
  node_analyze["Router analysis<br/>[analyze_router.py]"]
  node_benchmark["Pipeline benchmark<br/>[run_benchmark.py]"]
  node_judge["Answer judge<br/>[judge.py]"]
end

node_user(("API caller"))
node_model_services{{"Model services"}}

node_user -->|"submits query"| node_api
node_api -->|"runs request"| node_pipeline
node_api -->|"explains route"| node_classifier
node_api -->|"explains route"| node_heuristic
node_pipeline -->|"extracts features"| node_features
node_pipeline -->|"selects tier"| node_classifier
node_pipeline -->|"selects tier"| node_heuristic
node_classifier -->|"extracts features"| node_features
node_heuristic -->|"scores features"| node_features
node_pipeline -->|"calls model"| node_llm
node_pipeline -->|"checks answer"| node_escalation
node_pipeline -->|"selects tier spec"| node_config
node_llm -->|"reads and writes"| node_cache
node_llm -->|"uses model settings"| node_config
node_llm -->|"selects provider"| node_provider_registry
node_provider_registry -->|"registers providers"| node_provider_base
node_provider_registry -->|"registers provider"| node_anthropic
node_provider_registry -->|"registers provider"| node_openai_compat
node_anthropic -.->|"sends inference"| node_model_services
node_openai_compat -->|"sends inference"| node_model_services
node_llm -->|"creates result"| node_call_result
node_train -->|"trains model"| node_classifier
node_analyze -->|"evaluates classifier"| node_classifier
node_analyze -->|"evaluates rules"| node_heuristic
node_analyze -->|"extracts inputs"| node_features
node_benchmark -->|"runs routing arms"| node_pipeline
node_benchmark -->|"grades answers"| node_judge
node_judge -->|"requests grading call"| node_llm

click node_api "https://github.com/rachitsethi2455/cost-aware-model-router/blob/master/src/api/main.py"
click node_pipeline "https://github.com/rachitsethi2455/cost-aware-model-router/blob/master/src/router/pipeline.py"
click node_features "https://github.com/rachitsethi2455/cost-aware-model-router/blob/master/src/router/features.py"
click node_classifier "https://github.com/rachitsethi2455/cost-aware-model-router/blob/master/src/router/classifier.py"
click node_heuristic "https://github.com/rachitsethi2455/cost-aware-model-router/blob/master/src/router/heuristic.py"
click node_escalation "https://github.com/rachitsethi2455/cost-aware-model-router/blob/master/src/router/escalation.py"
click node_config "https://github.com/rachitsethi2455/cost-aware-model-router/blob/master/src/router/config.py"
click node_llm "https://github.com/rachitsethi2455/cost-aware-model-router/blob/master/src/router/llm.py"
click node_cache "https://github.com/rachitsethi2455/cost-aware-model-router/blob/master/src/router/cache.py"
click node_provider_registry "https://github.com/rachitsethi2455/cost-aware-model-router/blob/master/src/router/providers/__init__.py"
click node_provider_base "https://github.com/rachitsethi2455/cost-aware-model-router/blob/master/src/router/providers/base.py"
click node_anthropic "https://github.com/rachitsethi2455/cost-aware-model-router/blob/master/src/router/providers/anthropic_provider.py"
click node_openai_compat "https://github.com/rachitsethi2455/cost-aware-model-router/blob/master/src/router/providers/openai_compat.py"
click node_call_result "https://github.com/rachitsethi2455/cost-aware-model-router/blob/master/src/router/types.py"
click node_train "https://github.com/rachitsethi2455/cost-aware-model-router/blob/master/evals/train_router.py"
click node_analyze "https://github.com/rachitsethi2455/cost-aware-model-router/blob/master/evals/analyze_router.py"
click node_benchmark "https://github.com/rachitsethi2455/cost-aware-model-router/blob/master/evals/run_benchmark.py"
click node_judge "https://github.com/rachitsethi2455/cost-aware-model-router/blob/master/evals/judge.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 toneBlue
class node_pipeline,node_features,node_classifier,node_heuristic,node_escalation toneAmber
class node_config,node_llm,node_cache,node_provider_registry,node_provider_base,node_anthropic,node_openai_compat,node_call_result,node_user toneMint
class node_train,node_analyze,node_benchmark,node_judge toneRose
class node_model_services toneIndigo
```

## Components

### API service

- [HTTP API and demo](https://github.com/rachitsethi2455/cost-aware-model-router/blob/master/src/api/main.py): `src/api/main.py`

### Routing and quality

- [Routing pipeline](https://github.com/rachitsethi2455/cost-aware-model-router/blob/master/src/router/pipeline.py): `src/router/pipeline.py`
- [Query features](https://github.com/rachitsethi2455/cost-aware-model-router/blob/master/src/router/features.py): `src/router/features.py`
- [Learned router](https://github.com/rachitsethi2455/cost-aware-model-router/blob/master/src/router/classifier.py): `src/router/classifier.py`
- [Heuristic router](https://github.com/rachitsethi2455/cost-aware-model-router/blob/master/src/router/heuristic.py): `src/router/heuristic.py`
- [Quality escalation](https://github.com/rachitsethi2455/cost-aware-model-router/blob/master/src/router/escalation.py): `src/router/escalation.py`

### Inference and providers

- [Model configuration](https://github.com/rachitsethi2455/cost-aware-model-router/blob/master/src/router/config.py): `src/router/config.py`
- [LLM client](https://github.com/rachitsethi2455/cost-aware-model-router/blob/master/src/router/llm.py): `src/router/llm.py`
- [Response cache](https://github.com/rachitsethi2455/cost-aware-model-router/blob/master/src/router/cache.py): `src/router/cache.py`
- [Provider selection](https://github.com/rachitsethi2455/cost-aware-model-router/blob/master/src/router/providers/__init__.py): `src/router/providers/__init__.py`
- [Provider interface](https://github.com/rachitsethi2455/cost-aware-model-router/blob/master/src/router/providers/base.py): `src/router/providers/base.py`
- [Anthropic provider](https://github.com/rachitsethi2455/cost-aware-model-router/blob/master/src/router/providers/anthropic_provider.py): `src/router/providers/anthropic_provider.py`
- [OpenAI-compatible provider](https://github.com/rachitsethi2455/cost-aware-model-router/blob/master/src/router/providers/openai_compat.py): `src/router/providers/openai_compat.py`
- [Call result and costs](https://github.com/rachitsethi2455/cost-aware-model-router/blob/master/src/router/types.py): `src/router/types.py`

### Router evaluation

- [Router training](https://github.com/rachitsethi2455/cost-aware-model-router/blob/master/evals/train_router.py): `evals/train_router.py`
- [Router analysis](https://github.com/rachitsethi2455/cost-aware-model-router/blob/master/evals/analyze_router.py): `evals/analyze_router.py`
- [Pipeline benchmark](https://github.com/rachitsethi2455/cost-aware-model-router/blob/master/evals/run_benchmark.py): `evals/run_benchmark.py`
- [Answer judge](https://github.com/rachitsethi2455/cost-aware-model-router/blob/master/evals/judge.py): `evals/judge.py`

### Other components

- **API caller**
- **Model services**

## Connections

- API caller → HTTP API and demo: submits query (evidence: `src/api/main.py`)
- HTTP API and demo → Routing pipeline: runs request (evidence: `src/api/main.py`)
- HTTP API and demo → Learned router: explains route (evidence: `src/api/main.py`)
- HTTP API and demo → Heuristic router: explains route (evidence: `src/api/main.py`)
- Routing pipeline → Query features: extracts features (evidence: `src/router/pipeline.py`)
- Routing pipeline → Learned router: selects tier (evidence: `src/router/pipeline.py`)
- Routing pipeline → Heuristic router: selects tier (evidence: `src/router/pipeline.py`)
- Learned router → Query features: extracts features (evidence: `src/router/classifier.py`)
- Heuristic router → Query features: scores features (evidence: `src/router/heuristic.py`)
- Routing pipeline → LLM client: calls model (evidence: `src/router/pipeline.py`)
- Routing pipeline → Quality escalation: checks answer (evidence: `src/router/pipeline.py`)
- Routing pipeline → Model configuration: selects tier spec (evidence: `src/router/pipeline.py`)
- LLM client → Response cache: reads and writes (evidence: `src/router/llm.py`)
- LLM client → Model configuration: uses model settings (evidence: `src/router/llm.py`)
- LLM client → Provider selection: selects provider (evidence: `src/router/llm.py`)
- Provider selection → Provider interface: registers providers (evidence: `src/router/providers/__init__.py`)
- Provider selection → Anthropic provider: registers provider (evidence: `src/router/providers/__init__.py`)
- Provider selection → OpenAI-compatible provider: registers provider (evidence: `src/router/providers/__init__.py`)
- Anthropic provider → Model services: sends inference
- OpenAI-compatible provider → Model services: sends inference (evidence: `src/router/providers/openai_compat.py`)
- LLM client → Call result and costs: creates result (evidence: `src/router/llm.py`)
- Router training → Learned router: trains model (evidence: `evals/train_router.py`)
- Router analysis → Learned router: evaluates classifier (evidence: `evals/analyze_router.py`)
- Router analysis → Heuristic router: evaluates rules (evidence: `evals/analyze_router.py`)
- Router analysis → Query features: extracts inputs (evidence: `evals/analyze_router.py`)
- Pipeline benchmark → Routing pipeline: runs routing arms (evidence: `evals/run_benchmark.py`)
- Pipeline benchmark → Answer judge: grades answers (evidence: `evals/run_benchmark.py`)
- Answer judge → LLM client: requests grading call (evidence: `evals/judge.py`)
