How amogh-nagri-11/fleet-llm-inference works
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: routes.py /api/v1 routes · load_balancer.py Load balancer · streams.py Redis Streams · worker_pool.py Queue worker pool
Components 20
Gateway API
- /api/v1 routes · Generate, chat, queued generation, health, workers, and queue depth.
gateway/routes.py - Auth & middleware · POST requests require an API key.
gateway/middleware.py - Metrics
gateway/metrics.py
Context
- Context manager · Records workload identity and accumulated token counts.
context/manager.py - Context optimization · Implemented but not invoked by HTTP routes.
context/selection.py
Routing & execution
- Load balancer · Filters context-capacity-ineligible workers before scheduling.
router/load_balancer.py - Worker resilience
router/circuit_breaker.py - Ollama client
workers/ollama_client.py
Durable async
- Redis Streams · Shared queue for gateway replicas and consumers.
job_queue/streams.py - Queue worker pool · Consumes jobs and dispatches through the shared load balancer.
workers/worker_pool.py - Retry & dead letter · Retries recoverable jobs and retains permanent failures.
job_queue/retry.py
Memory
Initialized but not used in live prompt assembly.
- Memory manager · Lifespan-initialized, outside live generation.
memory/manager.py - Memory store & retrieval · Persists memory and performs ranked budgeted retrieval.
memory/store.py
Runtime & operations
- Gateway runtime · Environment-driven async application lifecycle.
gateway/main.py - Deployment & autoscaling · Kubernetes deployment lacks PostgreSQL for the memory path.
k8s/30-gateway.yaml - Prometheus & Grafana
observability/prometheus/prometheus.yml
Other components
- Fleet HTTP SDK
client/fleet_client.py - Ollama workers · Uniform worker configuration; no capability registry.
- Redis
- PostgreSQL
Connections 22
- 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