# amogh-nagri-11/fleet-llm-inference architecture

> Architecture diagram of the GitHub repository amogh-nagri-11/fleet-llm-inference, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/amogh-nagri-11/fleet-llm-inference
- Repository: https://github.com/amogh-nagri-11/fleet-llm-inference

## Overview

#### Purpose and entry points
Fleet is an agent-context and inference orchestration runtime above Ollama. Agents call the thin HTTP SDK at `client/fleet_client.py` or the FastAPI API started from `gateway/main.py`; request endpoints and lifecycle wiring are in `gateway/routes.py`. It manages workflow context, routes only to eligible model workers, and supports synchronous and Redis-backed queued inference.

#### Gateway request boundary
`gateway/routes.py` is the public `/api/v1` boundary for generate, chat, queued generation, health, workers, and queue depth. Middleware/auth and Prometheus-facing metrics are implemented in `gateway/middleware.py` and `gateway/metrics.py`. POST requests require an API key; optional agent/workflow/request IDs establish workload identity across context recording, routing, and metrics.

#### Context lifecycle: live recording vs. unwired optimization
`context/manager.py` records agent-aware requests and token counts, backed by context models/store in `context/models.py` and `context/store.py`. The live routing path uses the workflow’s accumulated token count to exclude workers lacking context capacity.  
`context/selection.py`, `context/artifacts.py`, and `context/compression.py` implement budgeted selection, large-content externalization, and compression, but are not yet invoked by HTTP routes; therefore they do not alter prompts sent to Ollama today.

#### Routing and worker execution
`router/load_balancer.py` applies context-capacity eligibility before its configured scheduling policy (round-robin, least-latency, or queue-depth). `router/circuit_breaker.py` isolates failing workers, while health behavior is represented by `router/health_checker.py`. Selected workers are contacted through `workers/ollama_client.py`, the implemented inference backend. Workers are configured uniformly; there is no per-worker model/capability registry.

#### Durable asynchronous inference and recovery
Queued requests enter Redis Streams through `job_queue/streams.py`. `workers/worker_pool.py` consumes jobs via consumer groups, dispatches through the same load balancer, and may run in independent gateway replicas sharing the queue. Retry/recovery uses `job_queue/retry.py`; permanently failed jobs go to `job_queue/dead_letter.py`. This subsystem is the reliability boundary for worker/gateway crashes and horizontal queue consumers.

#### Memory subsystem: initialized but outside live generation
`memory/manager.py` coordinates durable working/episodic memory; `memory/store.py` persists it in PostgreSQL and `memory/retrieval.py` performs ranking/budgeted retrieval. It is connected during application lifespan but currently has no route-driven integration into prompt assembly. It is thus a separable future stage between context gathering and inference routing.

#### Runtime, infrastructure, and operations
The runtime is Python/FastAPI/uvicorn with async HTTP and database access; settings are environment-driven in `config/settings.py`. Required external services are Ollama for model execution, Redis for Streams queueing, and PostgreSQL for memory. Local/container deployment is defined in `docker-compose.yml` and `docker-compose.prod.yml`; Kubernetes deployment/autoscaling resources are in `k8s/30-gateway.yaml` and `k8s/40-keda-scaledobject.yaml`. Prometheus and Grafana configuration lives under `observability/prometheus/prometheus.yml` and `observability/grafana/`; Kubernetes currently lacks PostgreSQL, so its memory path is incomplete.

Main entry points: [`gateway/routes.py`](https://github.com/amogh-nagri-11/fleet-llm-inference/blob/main/gateway/routes.py) (/api/v1 routes), [`router/load_balancer.py`](https://github.com/amogh-nagri-11/fleet-llm-inference/blob/main/router/load_balancer.py) (Load balancer), [`job_queue/streams.py`](https://github.com/amogh-nagri-11/fleet-llm-inference/blob/main/job_queue/streams.py) (Redis Streams), [`workers/worker_pool.py`](https://github.com/amogh-nagri-11/fleet-llm-inference/blob/main/workers/worker_pool.py) (Queue worker pool)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_gateway["Gateway API"]
  node_gateway_routes["/api/v1 routes<br/>FastAPI boundary<br/>[routes.py]"]
  node_gateway_middleware["Auth &amp; middleware<br/>request policy<br/>[middleware.py]"]
  node_gateway_metrics["Metrics<br/>[metrics.py]"]
end

subgraph group_context["Context"]
  node_context_manager["Context manager<br/>workflow recorder<br/>[manager.py]"]
  node_context_optimization["Context optimization<br/>selection/compression<br/>[selection.py]"]
end

subgraph group_routing["Routing &amp; execution"]
  node_load_balancer["Load balancer<br/>eligibility scheduler<br/>[load_balancer.py]"]
  node_worker_health["Worker resilience<br/>health and circuit breaking<br/>[circuit_breaker.py]"]
  node_ollama_client["Ollama client<br/>inference backend<br/>[ollama_client.py]"]
end

subgraph group_async["Durable async"]
  node_streams["Redis Streams<br/>durable job queue<br/>[streams.py]"]
  node_worker_pool["Queue worker pool<br/>consumer-group executor<br/>[worker_pool.py]"]
  node_retry_dlq["Retry &amp; dead letter<br/>failure recovery<br/>[retry.py]"]
end

subgraph group_memory["Memory"]
  node_memory_manager["Memory manager<br/>memory coordinator<br/>[manager.py]"]
  node_memory_store["Memory store &amp; retrieval<br/>durable retrieval<br/>[store.py]"]
end

subgraph group_ops["Runtime &amp; operations"]
  node_runtime["Gateway runtime<br/>FastAPI / uvicorn<br/>[main.py]"]
  node_deployment["Deployment &amp; autoscaling<br/>Compose / Kubernetes<br/>[30-gateway.yaml]"]
  node_observability["Prometheus &amp; Grafana<br/>operations monitoring<br/>[prometheus.yml]"]
end

node_sdk["Fleet HTTP SDK<br/>Python client<br/>[fleet_client.py]"]
node_ollama{{"Ollama workers<br/>model runtime"}}
node_redis[("Redis<br/>external datastore")]
node_postgres[("PostgreSQL<br/>external datastore")]

node_sdk -->|"HTTP requests"| node_gateway_routes
node_runtime -->|"starts"| node_gateway_routes
node_gateway_middleware -->|"protects"| node_gateway_routes
node_gateway_routes -->|"records request metrics"| node_gateway_metrics
node_gateway_routes -->|"records workflow context"| node_context_manager
node_context_manager -->|"accumulated tokens"| node_load_balancer
node_gateway_routes -->|"synchronous inference"| node_load_balancer
node_load_balancer -->|"considers availability"| node_worker_health
node_load_balancer -->|"selects worker"| node_ollama_client
node_ollama_client -->|"inference HTTP"| node_ollama
node_gateway_routes -->|"enqueue async request"| node_streams
node_streams -->|"stored in"| node_redis
node_streams -->|"consumer group jobs"| node_worker_pool
node_worker_pool -->|"dispatches via"| node_load_balancer
node_worker_pool -->|"failure handling"| node_retry_dlq
node_runtime -->|"initializes"| node_memory_manager
node_memory_manager -->|"coordinates"| node_memory_store
node_memory_store -->|"persists memory"| node_postgres
node_context_optimization -.->|"not wired into live path"| node_load_balancer
node_memory_manager -.->|"future prompt stage"| node_load_balancer
node_gateway_metrics -->|"scraped and visualized"| node_observability
node_deployment -->|"deploys gateway replicas"| node_runtime

click node_sdk "https://github.com/amogh-nagri-11/fleet-llm-inference/blob/main/client/fleet_client.py"
click node_gateway_routes "https://github.com/amogh-nagri-11/fleet-llm-inference/blob/main/gateway/routes.py"
click node_gateway_middleware "https://github.com/amogh-nagri-11/fleet-llm-inference/blob/main/gateway/middleware.py"
click node_gateway_metrics "https://github.com/amogh-nagri-11/fleet-llm-inference/blob/main/gateway/metrics.py"
click node_context_manager "https://github.com/amogh-nagri-11/fleet-llm-inference/blob/main/context/manager.py"
click node_context_optimization "https://github.com/amogh-nagri-11/fleet-llm-inference/blob/main/context/selection.py"
click node_load_balancer "https://github.com/amogh-nagri-11/fleet-llm-inference/blob/main/router/load_balancer.py"
click node_worker_health "https://github.com/amogh-nagri-11/fleet-llm-inference/blob/main/router/circuit_breaker.py"
click node_ollama_client "https://github.com/amogh-nagri-11/fleet-llm-inference/blob/main/workers/ollama_client.py"
click node_streams "https://github.com/amogh-nagri-11/fleet-llm-inference/blob/main/job_queue/streams.py"
click node_worker_pool "https://github.com/amogh-nagri-11/fleet-llm-inference/blob/main/workers/worker_pool.py"
click node_retry_dlq "https://github.com/amogh-nagri-11/fleet-llm-inference/blob/main/job_queue/retry.py"
click node_memory_manager "https://github.com/amogh-nagri-11/fleet-llm-inference/blob/main/memory/manager.py"
click node_memory_store "https://github.com/amogh-nagri-11/fleet-llm-inference/blob/main/memory/store.py"
click node_runtime "https://github.com/amogh-nagri-11/fleet-llm-inference/blob/main/gateway/main.py"
click node_deployment "https://github.com/amogh-nagri-11/fleet-llm-inference/blob/main/k8s/30-gateway.yaml"
click node_observability "https://github.com/amogh-nagri-11/fleet-llm-inference/blob/main/observability/prometheus/prometheus.yml"

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_gateway_routes,node_gateway_middleware,node_gateway_metrics toneBlue
class node_context_manager,node_context_optimization toneAmber
class node_load_balancer,node_worker_health,node_ollama_client toneMint
class node_streams,node_worker_pool,node_retry_dlq toneRose
class node_memory_manager,node_memory_store toneIndigo
class node_runtime,node_deployment,node_observability toneTeal
class node_sdk,node_ollama,node_redis,node_postgres toneNeutral
```

## Components

### Gateway API

- [/api/v1 routes](https://github.com/amogh-nagri-11/fleet-llm-inference/blob/main/gateway/routes.py): FastAPI boundary · Generate, chat, queued generation, health, workers, and queue depth. · `gateway/routes.py`
- [Auth & middleware](https://github.com/amogh-nagri-11/fleet-llm-inference/blob/main/gateway/middleware.py): request policy · POST requests require an API key. · `gateway/middleware.py`
- [Metrics](https://github.com/amogh-nagri-11/fleet-llm-inference/blob/main/gateway/metrics.py): Prometheus metrics · `gateway/metrics.py`

### Context

- [Context manager](https://github.com/amogh-nagri-11/fleet-llm-inference/blob/main/context/manager.py): workflow recorder · Records workload identity and accumulated token counts. · `context/manager.py`
- [Context optimization](https://github.com/amogh-nagri-11/fleet-llm-inference/blob/main/context/selection.py): selection/compression · Implemented but not invoked by HTTP routes. · `context/selection.py`

### Routing & execution

- [Load balancer](https://github.com/amogh-nagri-11/fleet-llm-inference/blob/main/router/load_balancer.py): eligibility scheduler · Filters context-capacity-ineligible workers before scheduling. · `router/load_balancer.py`
- [Worker resilience](https://github.com/amogh-nagri-11/fleet-llm-inference/blob/main/router/circuit_breaker.py): health and circuit breaking · `router/circuit_breaker.py`
- [Ollama client](https://github.com/amogh-nagri-11/fleet-llm-inference/blob/main/workers/ollama_client.py): inference backend · `workers/ollama_client.py`

### Durable async

- [Redis Streams](https://github.com/amogh-nagri-11/fleet-llm-inference/blob/main/job_queue/streams.py): durable job queue · Shared queue for gateway replicas and consumers. · `job_queue/streams.py`
- [Queue worker pool](https://github.com/amogh-nagri-11/fleet-llm-inference/blob/main/workers/worker_pool.py): consumer-group executor · Consumes jobs and dispatches through the shared load balancer. · `workers/worker_pool.py`
- [Retry & dead letter](https://github.com/amogh-nagri-11/fleet-llm-inference/blob/main/job_queue/retry.py): failure recovery · Retries recoverable jobs and retains permanent failures. · `job_queue/retry.py`

### Memory

Initialized but not used in live prompt assembly.

- [Memory manager](https://github.com/amogh-nagri-11/fleet-llm-inference/blob/main/memory/manager.py): memory coordinator · Lifespan-initialized, outside live generation. · `memory/manager.py`
- [Memory store & retrieval](https://github.com/amogh-nagri-11/fleet-llm-inference/blob/main/memory/store.py): durable retrieval · Persists memory and performs ranked budgeted retrieval. · `memory/store.py`

### Runtime & operations

- [Gateway runtime](https://github.com/amogh-nagri-11/fleet-llm-inference/blob/main/gateway/main.py): FastAPI / uvicorn · Environment-driven async application lifecycle. · `gateway/main.py`
- [Deployment & autoscaling](https://github.com/amogh-nagri-11/fleet-llm-inference/blob/main/k8s/30-gateway.yaml): Compose / Kubernetes · Kubernetes deployment lacks PostgreSQL for the memory path. · `k8s/30-gateway.yaml`
- [Prometheus & Grafana](https://github.com/amogh-nagri-11/fleet-llm-inference/blob/main/observability/prometheus/prometheus.yml): operations monitoring · `observability/prometheus/prometheus.yml`

### Other components

- [Fleet HTTP SDK](https://github.com/amogh-nagri-11/fleet-llm-inference/blob/main/client/fleet_client.py): Python client · `client/fleet_client.py`
- **Ollama workers**: model runtime · Uniform worker configuration; no capability registry.
- **Redis**: external datastore
- **PostgreSQL**: external datastore

## Connections

- Fleet HTTP SDK → /api/v1 routes: HTTP requests
- Gateway runtime → /api/v1 routes: starts
- Auth & middleware → /api/v1 routes: protects
- /api/v1 routes → Metrics: records request metrics
- /api/v1 routes → Context manager: records workflow context
- Context manager → Load balancer: accumulated tokens · Used for context-capacity eligibility.
- /api/v1 routes → Load balancer: synchronous inference
- Load balancer → Worker resilience: considers availability
- Load balancer → Ollama client: selects worker
- Ollama client → Ollama workers: inference HTTP
- /api/v1 routes → Redis Streams: enqueue async request
- Redis Streams → Redis: stored in
- Redis Streams → Queue worker pool: consumer group jobs
- Queue worker pool → Load balancer: dispatches via
- Queue worker pool → Retry & dead letter: failure handling
- Gateway runtime → Memory manager: initializes
- Memory manager → Memory store & retrieval: coordinates
- Memory store & retrieval → PostgreSQL: persists memory
- Context optimization → Load balancer: not wired into live path
- Memory manager → Load balancer: future prompt stage
- Metrics → Prometheus & Grafana: scraped and visualized
- Deployment & autoscaling → Gateway runtime: deploys gateway replicas
