# sloxphrite73/sakura-llmgateway architecture

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

- Interactive diagram: https://gitdiagram.com/sloxphrite73/sakura-llmgateway
- Repository: https://github.com/sloxphrite73/sakura-llmgateway

## Overview

Sakura LLM Gateway is a local-first API gateway with an Android host option. Clients send OpenAI or Anthropic requests to the gateway; it resolves models, selects and rotates provider keys, translates protocols when needed, and returns upstream responses. A separate web console manages providers, models, keys, strategy, settings, catalog, and statistics. The graph includes documented persistence, metrics, and Android hosting capabilities. Source excerpts are partial, so some subsystem connections are omitted where their invoking code was not shown.

Main entry points: [`llm-gateway/src/state.rs`](https://github.com/sloxphrite73/sakura-llmgateway/blob/main/llm-gateway/src/state.rs) (Runtime state), [`llm-gateway/src/main.rs`](https://github.com/sloxphrite73/sakura-llmgateway/blob/main/llm-gateway/src/main.rs) (Gateway server), [`llm-gateway/src/admin.rs`](https://github.com/sloxphrite73/sakura-llmgateway/blob/main/llm-gateway/src/admin.rs) (Admin API), [`llm-gateway/src/proxy.rs`](https://github.com/sloxphrite73/sakura-llmgateway/blob/main/llm-gateway/src/proxy.rs) (API proxy)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_hosting["Hosting and interfaces"]
  node_android_activity["Android app<br/>[MainActivity.kt]"]
  node_android_service["Gateway service<br/>[GatewayService.kt]"]
  node_gateway["Gateway server<br/>[main.rs]"]
  node_console_ui["Web console UI<br/>[ui.rs]"]
  node_console_assets["Console assets<br/>[index.html]"]
end

subgraph group_inference["Inference path"]
  node_proxy["API proxy<br/>[proxy.rs]"]
  node_protocol["Protocol translation<br/>[protocol.rs]"]
  node_model_routing["Model routing<br/>[strategy.rs]"]
  node_provider_keys["Provider keys<br/>[config.rs]"]
end

subgraph group_management["Management console"]
  node_admin_api["Admin API<br/>[admin.rs]"]
  node_free_catalog["Free provider catalog<br/>[free_catalog.rs]"]
end

subgraph group_runtime["State and telemetry"]
  node_config["Gateway configuration<br/>[config.rs]"]
  node_runtime_state["Runtime state<br/>[state.rs]"]
  node_balance["Balance polling<br/>[balance.rs]"]
  node_stats["Statistics and metrics<br/>[stats.rs]"]
  node_config_file[("gateway.json<br/>[config.rs]")]
end

node_client(("API client"))
node_upstream(("LLM providers"))

node_client -->|"sends requests"| node_gateway
node_gateway -->|"routes API"| node_proxy
node_gateway -->|"routes admin"| node_admin_api
node_gateway -->|"routes catalog"| node_free_catalog
node_gateway -->|"serves console"| node_console_ui
node_console_ui -->|"serves assets"| node_console_assets
node_proxy -->|"selects candidates"| node_model_routing
node_proxy -->|"translates messages"| node_protocol
node_proxy -->|"records outcomes"| node_runtime_state
node_proxy -->|"forwards requests"| node_upstream
node_model_routing -->|"resolves models"| node_config
node_model_routing -.->|"selects keys"| node_provider_keys
node_admin_api -->|"updates runtime"| node_runtime_state
node_admin_api -->|"manages settings"| node_config
node_admin_api -->|"adopts statistics"| node_stats
node_admin_api -->|"manages strategy"| node_model_routing
node_free_catalog -->|"caches catalog"| node_runtime_state
node_free_catalog -->|"uses admin errors"| node_admin_api
node_runtime_state -->|"reads configuration"| node_config
node_runtime_state -->|"tracks metrics"| node_stats
node_balance -->|"updates balances"| node_runtime_state
node_runtime_state -.->|"persists settings"| node_config_file
node_runtime_state -.->|"persists statistics"| node_stats
node_android_activity -->|"starts service"| node_android_service
node_android_service -.->|"hosts process"| node_gateway

click node_android_activity "https://github.com/sloxphrite73/sakura-llmgateway/blob/main/android/app/src/main/java/dev/sakura/llmgateway/MainActivity.kt"
click node_android_service "https://github.com/sloxphrite73/sakura-llmgateway/blob/main/android/app/src/main/java/dev/sakura/llmgateway/GatewayService.kt"
click node_gateway "https://github.com/sloxphrite73/sakura-llmgateway/blob/main/llm-gateway/src/main.rs"
click node_console_ui "https://github.com/sloxphrite73/sakura-llmgateway/blob/main/llm-gateway/src/ui.rs"
click node_console_assets "https://github.com/sloxphrite73/sakura-llmgateway/blob/main/llm-gateway/static/index.html"
click node_proxy "https://github.com/sloxphrite73/sakura-llmgateway/blob/main/llm-gateway/src/proxy.rs"
click node_protocol "https://github.com/sloxphrite73/sakura-llmgateway/blob/main/llm-gateway/src/protocol.rs"
click node_model_routing "https://github.com/sloxphrite73/sakura-llmgateway/blob/main/llm-gateway/src/strategy.rs"
click node_provider_keys "https://github.com/sloxphrite73/sakura-llmgateway/blob/main/llm-gateway/src/config.rs"
click node_admin_api "https://github.com/sloxphrite73/sakura-llmgateway/blob/main/llm-gateway/src/admin.rs"
click node_free_catalog "https://github.com/sloxphrite73/sakura-llmgateway/blob/main/llm-gateway/src/free_catalog.rs"
click node_config "https://github.com/sloxphrite73/sakura-llmgateway/blob/main/llm-gateway/src/config.rs"
click node_runtime_state "https://github.com/sloxphrite73/sakura-llmgateway/blob/main/llm-gateway/src/state.rs"
click node_balance "https://github.com/sloxphrite73/sakura-llmgateway/blob/main/llm-gateway/src/balance.rs"
click node_stats "https://github.com/sloxphrite73/sakura-llmgateway/blob/main/llm-gateway/src/stats.rs"
click node_config_file "https://github.com/sloxphrite73/sakura-llmgateway/blob/main/llm-gateway/src/config.rs"

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_android_activity,node_android_service,node_gateway,node_console_ui,node_console_assets,node_client toneBlue
class node_proxy,node_protocol,node_model_routing,node_provider_keys toneAmber
class node_admin_api,node_free_catalog toneMint
class node_config,node_runtime_state,node_balance,node_stats,node_config_file toneRose
class node_upstream toneIndigo
```

## Components

### Hosting and interfaces

- [Android app](https://github.com/sloxphrite73/sakura-llmgateway/blob/main/android/app/src/main/java/dev/sakura/llmgateway/MainActivity.kt): `android/app/src/main/java/dev/sakura/llmgateway/MainActivity.kt`
- [Gateway service](https://github.com/sloxphrite73/sakura-llmgateway/blob/main/android/app/src/main/java/dev/sakura/llmgateway/GatewayService.kt): `android/app/src/main/java/dev/sakura/llmgateway/GatewayService.kt`
- [Gateway server](https://github.com/sloxphrite73/sakura-llmgateway/blob/main/llm-gateway/src/main.rs): `llm-gateway/src/main.rs`
- [Web console UI](https://github.com/sloxphrite73/sakura-llmgateway/blob/main/llm-gateway/src/ui.rs): `llm-gateway/src/ui.rs`
- [Console assets](https://github.com/sloxphrite73/sakura-llmgateway/blob/main/llm-gateway/static/index.html): `llm-gateway/static/index.html`

### Inference path

- [API proxy](https://github.com/sloxphrite73/sakura-llmgateway/blob/main/llm-gateway/src/proxy.rs): `llm-gateway/src/proxy.rs`
- [Protocol translation](https://github.com/sloxphrite73/sakura-llmgateway/blob/main/llm-gateway/src/protocol.rs): `llm-gateway/src/protocol.rs`
- [Model routing](https://github.com/sloxphrite73/sakura-llmgateway/blob/main/llm-gateway/src/strategy.rs): `llm-gateway/src/strategy.rs`
- [Provider keys](https://github.com/sloxphrite73/sakura-llmgateway/blob/main/llm-gateway/src/config.rs): `llm-gateway/src/config.rs`

### Management console

- [Admin API](https://github.com/sloxphrite73/sakura-llmgateway/blob/main/llm-gateway/src/admin.rs): `llm-gateway/src/admin.rs`
- [Free provider catalog](https://github.com/sloxphrite73/sakura-llmgateway/blob/main/llm-gateway/src/free_catalog.rs): `llm-gateway/src/free_catalog.rs`

### State and telemetry

- [Gateway configuration](https://github.com/sloxphrite73/sakura-llmgateway/blob/main/llm-gateway/src/config.rs): `llm-gateway/src/config.rs`
- [Runtime state](https://github.com/sloxphrite73/sakura-llmgateway/blob/main/llm-gateway/src/state.rs): `llm-gateway/src/state.rs`
- [Balance polling](https://github.com/sloxphrite73/sakura-llmgateway/blob/main/llm-gateway/src/balance.rs): `llm-gateway/src/balance.rs`
- [Statistics and metrics](https://github.com/sloxphrite73/sakura-llmgateway/blob/main/llm-gateway/src/stats.rs): `llm-gateway/src/stats.rs`
- [gateway.json](https://github.com/sloxphrite73/sakura-llmgateway/blob/main/llm-gateway/src/config.rs): `llm-gateway/src/config.rs`

### Other components

- **API client**
- **LLM providers**

## Connections

- API client → Gateway server: sends requests (evidence: `README.md`)
- Gateway server → API proxy: routes API (evidence: `llm-gateway/src/main.rs`)
- Gateway server → Admin API: routes admin (evidence: `llm-gateway/src/main.rs`)
- Gateway server → Free provider catalog: routes catalog (evidence: `llm-gateway/src/main.rs`)
- Gateway server → Web console UI: serves console (evidence: `llm-gateway/src/main.rs`)
- Web console UI → Console assets: serves assets
- API proxy → Model routing: selects candidates (evidence: `llm-gateway/src/proxy.rs`)
- API proxy → Protocol translation: translates messages (evidence: `llm-gateway/src/proxy.rs`)
- API proxy → Runtime state: records outcomes (evidence: `llm-gateway/src/proxy.rs`)
- API proxy → LLM providers: forwards requests (evidence: `README.md`)
- Model routing → Gateway configuration: resolves models (evidence: `llm-gateway/src/strategy.rs`)
- Model routing → Provider keys: selects keys (evidence: `llm-gateway/src/strategy.rs`)
- Admin API → Runtime state: updates runtime (evidence: `llm-gateway/src/admin.rs`)
- Admin API → Gateway configuration: manages settings (evidence: `llm-gateway/src/admin.rs`)
- Admin API → Statistics and metrics: adopts statistics (evidence: `llm-gateway/src/admin.rs`)
- Admin API → Model routing: manages strategy (evidence: `llm-gateway/src/admin.rs`)
- Free provider catalog → Runtime state: caches catalog (evidence: `llm-gateway/src/free_catalog.rs`)
- Free provider catalog → Admin API: uses admin errors (evidence: `llm-gateway/src/free_catalog.rs`)
- Runtime state → Gateway configuration: reads configuration (evidence: `llm-gateway/src/state.rs`)
- Runtime state → Statistics and metrics: tracks metrics (evidence: `llm-gateway/src/state.rs`)
- Balance polling → Runtime state: updates balances (evidence: `llm-gateway/src/balance.rs`)
- Runtime state → gateway.json: persists settings (evidence: `llm-gateway/src/state.rs`)
- Runtime state → Statistics and metrics: persists statistics (evidence: `llm-gateway/src/state.rs`)
- Android app → Gateway service: starts service (evidence: `android/app/src/main/java/dev/sakura/llmgateway/MainActivity.kt`)
- Gateway service → Gateway server: hosts process (evidence: `README.md`)
