# mozilla-ai/otari architecture

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

- Interactive diagram: https://gitdiagram.com/mozilla-ai/otari
- Repository: https://github.com/mozilla-ai/otari

## Overview

Otari is a self-hosted, OpenAI-compatible LLM gateway with standalone, hosted, and hybrid deployment modes. Its principal workflow accepts a client inference request, authenticates it, checks budget and guardrails, dispatches to a model provider (optionally through tools), settles the reservation, and records usage. The repository also includes management APIs and a dashboard for keys, providers, routing, budgets, and usage, plus optional tool and file capabilities. The graph follows documented flows and sampled wiring; unsampled subsystem links are omitted rather than inferred.

Main entry points: [`src/gateway/api/routes/_pipeline.py`](https://github.com/mozilla-ai/otari/blob/main/src/gateway/api/routes/_pipeline.py) (Inference pipeline), [`src/gateway/api/main.py`](https://github.com/mozilla-ai/otari/blob/main/src/gateway/api/main.py) (Management APIs), [`src/gateway/services/_tool_loop.py`](https://github.com/mozilla-ai/otari/blob/main/src/gateway/services/_tool_loop.py) (Tool execution), [`src/gateway/core/database.py`](https://github.com/mozilla-ai/otari/blob/main/src/gateway/core/database.py) (Relational database)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_request["Request handling"]
  node_api["API router<br/>[main.py]"]
  node_pipeline["Inference pipeline<br/>[_pipeline.py]"]
  node_auth["Credential authentication<br/>[deps.py]"]
end

subgraph group_inference["Inference operations"]
  node_budgets["Budget enforcement<br/>[_service.py]"]
  node_guardrails["Inference guardrails<br/>[guardrails.py]"]
  node_routing["Model routing<br/>[_service.py]"]
  node_provider_port["Model provider port"]
  node_providers["Model providers"]
end

subgraph group_management["Management and identity"]
  node_dashboard["Admin dashboard<br/>[App.tsx]"]
  node_management_api["Management APIs<br/>[main.py]"]
  node_oauth["OAuth sign-in<br/>[oauth_service.py]"]
  node_keys["Virtual API keys<br/>[_service.py]"]
end

subgraph group_persistence["State and metering"]
  node_usage["Usage settlement<br/>[_stream_cost.py]"]
  node_log_writer["Usage log writer<br/>[log_writer.py]"]
  node_database[("Relational database<br/>[database.py]")]
  node_uow["Unit of work<br/>[unit_of_work.py]"]
  node_models["Domain models<br/>[tenancy.py]"]
end

subgraph group_extensions["Tools and integrations"]
  node_tool_loop["Tool execution<br/>[_tool_loop.py]"]
  node_search_fetch["Search and fetch<br/>[search_backend.py]"]
  node_code_execution["Code execution<br/>[containers.py]"]
  node_files["File understanding"]
end

node_client(("Client application"))
node_result(("Model response"))
node_external_tools(("Tool services"))

node_client -->|"sends request"| node_api
node_api -.->|"routes inference"| node_pipeline
node_api -->|"registers routes"| node_management_api
node_pipeline -->|"authenticates"| node_auth
node_pipeline -->|"checks budget"| node_budgets
node_pipeline -->|"applies guardrails"| node_guardrails
node_pipeline -->|"dispatches optionally"| node_tool_loop
node_pipeline -->|"dispatches request"| node_provider_port
node_provider_port -.->|"adapts provider calls"| node_providers
node_providers -->|"returns completion"| node_result
node_pipeline -.->|"returns response"| node_result
node_pipeline -->|"settles spend"| node_usage
node_pipeline -->|"records usage"| node_log_writer
node_usage -.->|"persists settlement"| node_database
node_log_writer -.->|"writes usage"| node_database
node_uow -->|"commits transactions"| node_database
node_dashboard -->|"manages gateway"| node_management_api
node_management_api -.->|"manages credentials"| node_keys
node_management_api -.->|"supports sign-in"| node_oauth
node_tool_loop -.->|"dispatches search"| node_search_fetch
node_tool_loop -.->|"dispatches execution"| node_code_execution
node_tool_loop -.->|"calls integrations"| node_external_tools
node_pipeline -.->|"uses file capability"| node_files
node_management_api -.->|"manages domain state"| node_models

click node_api "https://github.com/mozilla-ai/otari/blob/main/src/gateway/api/main.py"
click node_pipeline "https://github.com/mozilla-ai/otari/blob/main/src/gateway/api/routes/_pipeline.py"
click node_auth "https://github.com/mozilla-ai/otari/blob/main/src/gateway/api/deps.py"
click node_budgets "https://github.com/mozilla-ai/otari/blob/main/src/gateway/services/budgets/_service.py"
click node_guardrails "https://github.com/mozilla-ai/otari/blob/main/src/gateway/services/guardrails.py"
click node_routing "https://github.com/mozilla-ai/otari/blob/main/src/gateway/services/routing/_service.py"
click node_provider_port "https://github.com/mozilla-ai/otari/blob/main/src/gateway/ports/model_provider_port.py"
click node_providers "https://github.com/mozilla-ai/otari/blob/main/src/gateway/adapters/model_provider_adapter.py"
click node_usage "https://github.com/mozilla-ai/otari/blob/main/src/gateway/services/inference/_stream_cost.py"
click node_log_writer "https://github.com/mozilla-ai/otari/blob/main/src/gateway/services/log_writer.py"
click node_database "https://github.com/mozilla-ai/otari/blob/main/src/gateway/core/database.py"
click node_uow "https://github.com/mozilla-ai/otari/blob/main/src/gateway/core/unit_of_work.py"
click node_models "https://github.com/mozilla-ai/otari/blob/main/src/gateway/models/tenancy.py"
click node_dashboard "https://github.com/mozilla-ai/otari/blob/main/web/src/app/App.tsx"
click node_management_api "https://github.com/mozilla-ai/otari/blob/main/src/gateway/api/main.py"
click node_oauth "https://github.com/mozilla-ai/otari/blob/main/src/gateway/services/oauth_service.py"
click node_keys "https://github.com/mozilla-ai/otari/blob/main/src/gateway/services/api_keys/_service.py"
click node_tool_loop "https://github.com/mozilla-ai/otari/blob/main/src/gateway/services/_tool_loop.py"
click node_search_fetch "https://github.com/mozilla-ai/otari/blob/main/src/gateway/services/search_backend.py"
click node_code_execution "https://github.com/mozilla-ai/otari/blob/main/src/gateway/services/code_execution/containers.py"
click node_files "https://github.com/mozilla-ai/otari/blob/main/src/gateway/ports/file_storage_port.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_pipeline,node_auth,node_client toneBlue
class node_budgets,node_guardrails,node_routing,node_provider_port,node_providers toneAmber
class node_dashboard,node_management_api,node_oauth,node_keys toneMint
class node_usage,node_log_writer,node_database,node_uow,node_models toneRose
class node_tool_loop,node_search_fetch,node_code_execution,node_files,node_result,node_external_tools toneIndigo
```

## Components

### Request handling

- [API router](https://github.com/mozilla-ai/otari/blob/main/src/gateway/api/main.py): `src/gateway/api/main.py`
- [Inference pipeline](https://github.com/mozilla-ai/otari/blob/main/src/gateway/api/routes/_pipeline.py): `src/gateway/api/routes/_pipeline.py`
- [Credential authentication](https://github.com/mozilla-ai/otari/blob/main/src/gateway/api/deps.py): `src/gateway/api/deps.py`

### Inference operations

- [Budget enforcement](https://github.com/mozilla-ai/otari/blob/main/src/gateway/services/budgets/_service.py): `src/gateway/services/budgets/_service.py`
- [Inference guardrails](https://github.com/mozilla-ai/otari/blob/main/src/gateway/services/guardrails.py): `src/gateway/services/guardrails.py`
- [Model routing](https://github.com/mozilla-ai/otari/blob/main/src/gateway/services/routing/_service.py): `src/gateway/services/routing/_service.py`
- [Model provider port](https://github.com/mozilla-ai/otari/blob/main/src/gateway/ports/model_provider_port.py): `src/gateway/ports/model_provider_port.py`
- [Model providers](https://github.com/mozilla-ai/otari/blob/main/src/gateway/adapters/model_provider_adapter.py): `src/gateway/adapters/model_provider_adapter.py`

### Management and identity

- [Admin dashboard](https://github.com/mozilla-ai/otari/blob/main/web/src/app/App.tsx): `web/src/app/App.tsx`
- [Management APIs](https://github.com/mozilla-ai/otari/blob/main/src/gateway/api/main.py): `src/gateway/api/main.py`
- [OAuth sign-in](https://github.com/mozilla-ai/otari/blob/main/src/gateway/services/oauth_service.py): `src/gateway/services/oauth_service.py`
- [Virtual API keys](https://github.com/mozilla-ai/otari/blob/main/src/gateway/services/api_keys/_service.py): `src/gateway/services/api_keys/_service.py`

### State and metering

- [Usage settlement](https://github.com/mozilla-ai/otari/blob/main/src/gateway/services/inference/_stream_cost.py): `src/gateway/services/inference/_stream_cost.py`
- [Usage log writer](https://github.com/mozilla-ai/otari/blob/main/src/gateway/services/log_writer.py): `src/gateway/services/log_writer.py`
- [Relational database](https://github.com/mozilla-ai/otari/blob/main/src/gateway/core/database.py): `src/gateway/core/database.py`
- [Unit of work](https://github.com/mozilla-ai/otari/blob/main/src/gateway/core/unit_of_work.py): `src/gateway/core/unit_of_work.py`
- [Domain models](https://github.com/mozilla-ai/otari/blob/main/src/gateway/models/tenancy.py): `src/gateway/models/tenancy.py`

### Tools and integrations

- [Tool execution](https://github.com/mozilla-ai/otari/blob/main/src/gateway/services/_tool_loop.py): `src/gateway/services/_tool_loop.py`
- [Search and fetch](https://github.com/mozilla-ai/otari/blob/main/src/gateway/services/search_backend.py): `src/gateway/services/search_backend.py`
- [Code execution](https://github.com/mozilla-ai/otari/blob/main/src/gateway/services/code_execution/containers.py): `src/gateway/services/code_execution/containers.py`
- [File understanding](https://github.com/mozilla-ai/otari/blob/main/src/gateway/ports/file_storage_port.py): `src/gateway/ports/file_storage_port.py`

### Other components

- **Client application**
- **Model response**
- **Tool services**

## Connections

- Client application → API router: sends request (evidence: `README.md`)
- API router → Inference pipeline: routes inference
- API router → Management APIs: registers routes (evidence: `src/gateway/api/main.py`)
- Inference pipeline → Credential authentication: authenticates (evidence: `src/gateway/api/routes/_pipeline.py`)
- Inference pipeline → Budget enforcement: checks budget (evidence: `src/gateway/api/routes/_pipeline.py`)
- Inference pipeline → Inference guardrails: applies guardrails (evidence: `src/gateway/api/routes/_pipeline.py`)
- Inference pipeline → Tool execution: dispatches optionally (evidence: `src/gateway/api/routes/_pipeline.py`)
- Inference pipeline → Model provider port: dispatches request (evidence: `README.md`)
- Model provider port → Model providers: adapts provider calls
- Model providers → Model response: returns completion (evidence: `README.md`)
- Inference pipeline → Model response: returns response
- Inference pipeline → Usage settlement: settles spend (evidence: `src/gateway/api/routes/_pipeline.py`)
- Inference pipeline → Usage log writer: records usage (evidence: `src/gateway/api/routes/_pipeline.py`)
- Usage settlement → Relational database: persists settlement
- Usage log writer → Relational database: writes usage
- Unit of work → Relational database: commits transactions (evidence: `src/gateway/core/unit_of_work.py`)
- Admin dashboard → Management APIs: manages gateway (evidence: `README.md`)
- Management APIs → Virtual API keys: manages credentials
- Management APIs → OAuth sign-in: supports sign-in
- Tool execution → Search and fetch: dispatches search
- Tool execution → Code execution: dispatches execution
- Tool execution → Tool services: calls integrations
- Inference pipeline → File understanding: uses file capability
- Management APIs → Domain models: manages domain state
