# ritisha-11/optiroute-ai architecture

> Architecture diagram of the GitHub repository ritisha-11/optiroute-ai, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-12-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ritisha-11/optiroute-ai
- Repository: https://github.com/ritisha-11/optiroute-ai

## Overview

OptiRoute AI is a web interface and FastAPI service for optimizing prompts, reusing semantically similar answers, selecting local models and prompting strategies, validating answers, and estimating energy. The primary workflow runs through the backend pipeline; the UI submits queries and presents answers, processing stages, and session totals. README and the pipeline excerpt establish the domain stages and their orchestration. Several backend modules and the API implementation were not excerpted, so uncertain internal calls are omitted or marked unverified.

Main entry points: [`src/optiroute/pipeline.py`](https://github.com/ritisha-11/optiroute-ai/blob/main/src/optiroute/pipeline.py) (Query orchestrator), [`frontend/src/App.tsx`](https://github.com/ritisha-11/optiroute-ai/blob/main/frontend/src/App.tsx) (App shell), [`src/optiroute/llm.py`](https://github.com/ritisha-11/optiroute-ai/blob/main/src/optiroute/llm.py) (Ollama client), [`frontend/src/api/client.ts`](https://github.com/ritisha-11/optiroute-ai/blob/main/frontend/src/api/client.ts) (API client)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_interface["Web interface"]
  node_app["App shell<br/>[App.tsx]"]
  node_queryform["Query form<br/>[QueryForm.tsx]"]
  node_queryclient["API client<br/>[client.ts]"]
  node_results["Answer and stages<br/>[QueryOutcome.tsx]"]
  node_stageview["Pipeline display<br/>[Pipeline.tsx]"]
end

subgraph group_service["Query service"]
  node_api["FastAPI service<br/>[api.py]"]
  node_pipeline["Query orchestrator<br/>[pipeline.py]"]
  node_config["Runtime settings<br/>[config.py]"]
end

subgraph group_preparation["Prompt and cache"]
  node_optimizer["Prompt optimizer<br/>[optimizer.py]"]
  node_semanticcache[("Semantic cache<br/>[cache.py]")]
  node_embeddings["Sentence embeddings<br/>[embeddings.py]"]
  node_matching["Question matching<br/>[matching.py]"]
end

subgraph group_inference["Inference and quality"]
  node_router["Adaptive router<br/>[router.py]"]
  node_strategy["Prompt strategy<br/>[strategy.py]"]
  node_llm["Ollama client<br/>[llm.py]"]
  node_validator["Answer validator<br/>[validator.py]"]
end

subgraph group_telemetry["Monitoring"]
  node_tokens["Token counter<br/>[tokens.py]"]
  node_energy["Energy estimate<br/>[energy.py]"]
end

node_user(("User"))
node_ollama(("Ollama models"))

node_user -->|"uses"| node_app
node_app -->|"renders"| node_queryform
node_queryform -.->|"submits via"| node_queryclient
node_app -->|"polls status"| node_queryclient
node_queryclient -->|"requests"| node_api
node_api -.->|"dispatches queries"| node_pipeline
node_pipeline -->|"optimizes prompt"| node_optimizer
node_pipeline -->|"looks up and stores"| node_semanticcache
node_pipeline -->|"checks paraphrase"| node_matching
node_pipeline -->|"chooses tier"| node_router
node_pipeline -->|"selects prompt style"| node_strategy
node_pipeline -->|"generates answer"| node_llm
node_llm -->|"sends model request"| node_ollama
node_pipeline -->|"checks answer"| node_validator
node_pipeline -->|"loads counter"| node_tokens
node_pipeline -->|"estimates usage"| node_energy
node_pipeline -->|"builds components"| node_embeddings
node_embeddings -->|"reads settings"| node_config
node_llm -->|"reads settings"| node_config
node_app -->|"shows outcome"| node_results
node_app -->|"shows steps"| node_stageview
node_stageview -.->|"renders stages"| node_results

click node_app "https://github.com/ritisha-11/optiroute-ai/blob/main/frontend/src/App.tsx"
click node_queryform "https://github.com/ritisha-11/optiroute-ai/blob/main/frontend/src/components/QueryForm.tsx"
click node_queryclient "https://github.com/ritisha-11/optiroute-ai/blob/main/frontend/src/api/client.ts"
click node_results "https://github.com/ritisha-11/optiroute-ai/blob/main/frontend/src/components/QueryOutcome.tsx"
click node_stageview "https://github.com/ritisha-11/optiroute-ai/blob/main/frontend/src/components/Pipeline.tsx"
click node_api "https://github.com/ritisha-11/optiroute-ai/blob/main/src/optiroute/api.py"
click node_pipeline "https://github.com/ritisha-11/optiroute-ai/blob/main/src/optiroute/pipeline.py"
click node_optimizer "https://github.com/ritisha-11/optiroute-ai/blob/main/src/optiroute/optimizer.py"
click node_semanticcache "https://github.com/ritisha-11/optiroute-ai/blob/main/src/optiroute/cache.py"
click node_embeddings "https://github.com/ritisha-11/optiroute-ai/blob/main/src/optiroute/embeddings.py"
click node_matching "https://github.com/ritisha-11/optiroute-ai/blob/main/src/optiroute/matching.py"
click node_router "https://github.com/ritisha-11/optiroute-ai/blob/main/src/optiroute/router.py"
click node_strategy "https://github.com/ritisha-11/optiroute-ai/blob/main/src/optiroute/strategy.py"
click node_llm "https://github.com/ritisha-11/optiroute-ai/blob/main/src/optiroute/llm.py"
click node_validator "https://github.com/ritisha-11/optiroute-ai/blob/main/src/optiroute/validator.py"
click node_tokens "https://github.com/ritisha-11/optiroute-ai/blob/main/src/optiroute/tokens.py"
click node_energy "https://github.com/ritisha-11/optiroute-ai/blob/main/src/optiroute/energy.py"
click node_config "https://github.com/ritisha-11/optiroute-ai/blob/main/src/optiroute/config.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_app,node_queryform,node_queryclient,node_results,node_stageview,node_user toneBlue
class node_api,node_pipeline,node_config toneAmber
class node_optimizer,node_semanticcache,node_embeddings,node_matching toneMint
class node_router,node_strategy,node_llm,node_validator toneRose
class node_tokens,node_energy,node_ollama toneIndigo
```

## Components

### Web interface

- [App shell](https://github.com/ritisha-11/optiroute-ai/blob/main/frontend/src/App.tsx): `frontend/src/App.tsx`
- [Query form](https://github.com/ritisha-11/optiroute-ai/blob/main/frontend/src/components/QueryForm.tsx): `frontend/src/components/QueryForm.tsx`
- [API client](https://github.com/ritisha-11/optiroute-ai/blob/main/frontend/src/api/client.ts): `frontend/src/api/client.ts`
- [Answer and stages](https://github.com/ritisha-11/optiroute-ai/blob/main/frontend/src/components/QueryOutcome.tsx): `frontend/src/components/QueryOutcome.tsx`
- [Pipeline display](https://github.com/ritisha-11/optiroute-ai/blob/main/frontend/src/components/Pipeline.tsx): `frontend/src/components/Pipeline.tsx`

### Query service

- [FastAPI service](https://github.com/ritisha-11/optiroute-ai/blob/main/src/optiroute/api.py): `src/optiroute/api.py`
- [Query orchestrator](https://github.com/ritisha-11/optiroute-ai/blob/main/src/optiroute/pipeline.py): `src/optiroute/pipeline.py`
- [Runtime settings](https://github.com/ritisha-11/optiroute-ai/blob/main/src/optiroute/config.py): `src/optiroute/config.py`

### Prompt and cache

- [Prompt optimizer](https://github.com/ritisha-11/optiroute-ai/blob/main/src/optiroute/optimizer.py): `src/optiroute/optimizer.py`
- [Semantic cache](https://github.com/ritisha-11/optiroute-ai/blob/main/src/optiroute/cache.py): `src/optiroute/cache.py`
- [Sentence embeddings](https://github.com/ritisha-11/optiroute-ai/blob/main/src/optiroute/embeddings.py): `src/optiroute/embeddings.py`
- [Question matching](https://github.com/ritisha-11/optiroute-ai/blob/main/src/optiroute/matching.py): `src/optiroute/matching.py`

### Inference and quality

- [Adaptive router](https://github.com/ritisha-11/optiroute-ai/blob/main/src/optiroute/router.py): `src/optiroute/router.py`
- [Prompt strategy](https://github.com/ritisha-11/optiroute-ai/blob/main/src/optiroute/strategy.py): `src/optiroute/strategy.py`
- [Ollama client](https://github.com/ritisha-11/optiroute-ai/blob/main/src/optiroute/llm.py): `src/optiroute/llm.py`
- [Answer validator](https://github.com/ritisha-11/optiroute-ai/blob/main/src/optiroute/validator.py): `src/optiroute/validator.py`

### Monitoring

- [Token counter](https://github.com/ritisha-11/optiroute-ai/blob/main/src/optiroute/tokens.py): `src/optiroute/tokens.py`
- [Energy estimate](https://github.com/ritisha-11/optiroute-ai/blob/main/src/optiroute/energy.py): `src/optiroute/energy.py`

### Other components

- **User**
- **Ollama models**

## Connections

- User → App shell: uses (evidence: `README.md`)
- App shell → Query form: renders (evidence: `frontend/src/App.tsx`)
- Query form → API client: submits via
- App shell → API client: polls status (evidence: `frontend/src/App.tsx`)
- API client → FastAPI service: requests (evidence: `frontend/src/api/client.ts`)
- FastAPI service → Query orchestrator: dispatches queries
- Query orchestrator → Prompt optimizer: optimizes prompt (evidence: `src/optiroute/pipeline.py`)
- Query orchestrator → Semantic cache: looks up and stores (evidence: `src/optiroute/pipeline.py`)
- Query orchestrator → Question matching: checks paraphrase (evidence: `src/optiroute/pipeline.py`)
- Query orchestrator → Adaptive router: chooses tier (evidence: `src/optiroute/pipeline.py`)
- Query orchestrator → Prompt strategy: selects prompt style (evidence: `src/optiroute/pipeline.py`)
- Query orchestrator → Ollama client: generates answer (evidence: `src/optiroute/pipeline.py`)
- Ollama client → Ollama models: sends model request (evidence: `src/optiroute/llm.py`)
- Query orchestrator → Answer validator: checks answer (evidence: `src/optiroute/pipeline.py`)
- Query orchestrator → Token counter: loads counter (evidence: `src/optiroute/pipeline.py`)
- Query orchestrator → Energy estimate: estimates usage (evidence: `src/optiroute/pipeline.py`)
- Query orchestrator → Sentence embeddings: builds components (evidence: `src/optiroute/pipeline.py`)
- Sentence embeddings → Runtime settings: reads settings (evidence: `src/optiroute/embeddings.py`)
- Ollama client → Runtime settings: reads settings (evidence: `src/optiroute/llm.py`)
- App shell → Answer and stages: shows outcome (evidence: `frontend/src/App.tsx`)
- App shell → Pipeline display: shows steps (evidence: `frontend/src/App.tsx`)
- Pipeline display → Answer and stages: renders stages
