# goabiaryan/relayserve architecture

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

- Interactive diagram: https://gitdiagram.com/goabiaryan/relayserve
- Repository: https://github.com/goabiaryan/relayserve

## Overview

RelayServe is an adaptive LLM inference server. The principal workflow is an OpenAI-compatible chat request entering the HTTP server, being handled by the application’s queued request processor, routed to a configured backend or through device selection to local/llama.cpp execution, and returned as a response. The application also tracks metrics, plans layer placement, and manages KV-prefix bookkeeping. README documents streaming, request IDs, and multi-backend use; HTTP-server internals and several runtime modules were not excerpted, so their links rely on the source index or README rather than direct inspection.

Main entry points: [`relayserve/internal/server/app.py`](https://github.com/goabiaryan/relayserve/blob/main/relayserve/internal/server/app.py) (Request application), [`router.py`](https://github.com/goabiaryan/relayserve/blob/main/router.py) (Backend router), [`relayserve/internal/server/http_server.py`](https://github.com/goabiaryan/relayserve/blob/main/relayserve/internal/server/http_server.py) (HTTP API), [`relayserve/internal/device/registry.py`](https://github.com/goabiaryan/relayserve/blob/main/relayserve/internal/device/registry.py) (Device registry)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["API and startup"]
  node_cli["CLI startup<br/>[cli.py]"]
  node_settings["Runtime settings<br/>[settings.py]"]
  node_http["HTTP API<br/>[http_server.py]"]
end

subgraph group_orchestration["Request orchestration"]
  node_app["Request application<br/>[app.py]"]
  node_queue["Request queue<br/>[queue.py]"]
  node_kv["KV cache manager<br/>[manager.py]"]
  node_metrics["Metrics collector<br/>[collector.py]"]
end

subgraph group_devices["Device adaptation"]
  node_scheduler["Device scheduler<br/>[scheduler.py]"]
  node_probe["Device probe<br/>[probe.py]"]
  node_registry["Device registry<br/>[registry.py]"]
  node_shard["Shard planner<br/>[plan.py]"]
end

subgraph group_inference["Inference backends"]
  node_router["Backend router<br/>[router.py]"]
  node_interface["Backend interface"]
  node_local["Local backend<br/>[local_backend.py]"]
  node_modal["Modal backend<br/>[modal_backend.py]"]
  node_vllm["vLLM backend<br/>[vllm_backend.py]"]
  node_llama["llama.cpp client<br/>[runner.py]"]
  node_runner["Device runner<br/>[runner.py]"]
end

node_user(("Client"))
node_backend_services(("Configured inference services"))
node_llama_services(("llama.cpp endpoints"))

node_user -->|"sends chat"| node_http
node_cli -->|"loads settings"| node_settings
node_cli -->|"builds app"| node_app
node_cli -->|"starts server"| node_http
node_http -->|"dispatches requests"| node_app
node_app -->|"uses configuration"| node_settings
node_app -->|"enqueues requests"| node_queue
node_app -->|"selects device"| node_scheduler
node_app -->|"probes devices"| node_probe
node_app -->|"lists devices"| node_registry
node_probe -->|"creates devices"| node_registry
node_scheduler -->|"reads devices"| node_registry
node_app -->|"plans placement"| node_shard
node_shard -->|"reads devices"| node_registry
node_app -->|"tracks KV prefixes"| node_kv
node_app -->|"records metrics"| node_metrics
node_app -->|"selects backend"| node_router
node_router -->|"builds backend"| node_local
node_router -->|"builds backend"| node_modal
node_router -->|"builds backend"| node_vllm
node_local -->|"implements"| node_interface
node_modal -->|"implements"| node_interface
node_vllm -->|"implements"| node_interface
node_local -->|"sends inference"| node_backend_services
node_modal -->|"sends inference"| node_backend_services
node_vllm -->|"sends inference"| node_backend_services
node_app -->|"requests completion"| node_llama
node_app -->|"runs fallback"| node_runner
node_llama -->|"sends completion"| node_llama_services
node_http -->|"returns response"| node_user

click node_cli "https://github.com/goabiaryan/relayserve/blob/main/relayserve/cli.py"
click node_settings "https://github.com/goabiaryan/relayserve/blob/main/relayserve/internal/config/settings.py"
click node_http "https://github.com/goabiaryan/relayserve/blob/main/relayserve/internal/server/http_server.py"
click node_app "https://github.com/goabiaryan/relayserve/blob/main/relayserve/internal/server/app.py"
click node_queue "https://github.com/goabiaryan/relayserve/blob/main/relayserve/internal/queue/queue.py"
click node_scheduler "https://github.com/goabiaryan/relayserve/blob/main/relayserve/internal/scheduler/scheduler.py"
click node_probe "https://github.com/goabiaryan/relayserve/blob/main/relayserve/internal/profile/probe.py"
click node_registry "https://github.com/goabiaryan/relayserve/blob/main/relayserve/internal/device/registry.py"
click node_shard "https://github.com/goabiaryan/relayserve/blob/main/relayserve/internal/shard/plan.py"
click node_kv "https://github.com/goabiaryan/relayserve/blob/main/relayserve/internal/kv/manager.py"
click node_metrics "https://github.com/goabiaryan/relayserve/blob/main/relayserve/internal/metrics/collector.py"
click node_router "https://github.com/goabiaryan/relayserve/blob/main/router.py"
click node_interface "https://github.com/goabiaryan/relayserve/blob/main/backends/backend_interface.py"
click node_local "https://github.com/goabiaryan/relayserve/blob/main/backends/local_backend.py"
click node_modal "https://github.com/goabiaryan/relayserve/blob/main/backends/modal_backend.py"
click node_vllm "https://github.com/goabiaryan/relayserve/blob/main/backends/vllm_backend.py"
click node_llama "https://github.com/goabiaryan/relayserve/blob/main/relayserve/internal/runner/runner.py"
click node_runner "https://github.com/goabiaryan/relayserve/blob/main/relayserve/internal/runner/runner.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_cli,node_settings,node_http,node_user toneBlue
class node_app,node_queue,node_kv,node_metrics toneAmber
class node_scheduler,node_probe,node_registry,node_shard toneMint
class node_router,node_interface,node_local,node_modal,node_vllm,node_llama,node_runner toneRose
class node_backend_services,node_llama_services toneIndigo
```

## Components

### API and startup

- [CLI startup](https://github.com/goabiaryan/relayserve/blob/main/relayserve/cli.py): `relayserve/cli.py`
- [Runtime settings](https://github.com/goabiaryan/relayserve/blob/main/relayserve/internal/config/settings.py): `relayserve/internal/config/settings.py`
- [HTTP API](https://github.com/goabiaryan/relayserve/blob/main/relayserve/internal/server/http_server.py): `relayserve/internal/server/http_server.py`

### Request orchestration

- [Request application](https://github.com/goabiaryan/relayserve/blob/main/relayserve/internal/server/app.py): `relayserve/internal/server/app.py`
- [Request queue](https://github.com/goabiaryan/relayserve/blob/main/relayserve/internal/queue/queue.py): `relayserve/internal/queue/queue.py`
- [KV cache manager](https://github.com/goabiaryan/relayserve/blob/main/relayserve/internal/kv/manager.py): `relayserve/internal/kv/manager.py`
- [Metrics collector](https://github.com/goabiaryan/relayserve/blob/main/relayserve/internal/metrics/collector.py): `relayserve/internal/metrics/collector.py`

### Device adaptation

- [Device scheduler](https://github.com/goabiaryan/relayserve/blob/main/relayserve/internal/scheduler/scheduler.py): `relayserve/internal/scheduler/scheduler.py`
- [Device probe](https://github.com/goabiaryan/relayserve/blob/main/relayserve/internal/profile/probe.py): `relayserve/internal/profile/probe.py`
- [Device registry](https://github.com/goabiaryan/relayserve/blob/main/relayserve/internal/device/registry.py): `relayserve/internal/device/registry.py`
- [Shard planner](https://github.com/goabiaryan/relayserve/blob/main/relayserve/internal/shard/plan.py): `relayserve/internal/shard/plan.py`

### Inference backends

- [Backend router](https://github.com/goabiaryan/relayserve/blob/main/router.py): `router.py`
- [Backend interface](https://github.com/goabiaryan/relayserve/blob/main/backends/backend_interface.py): `backends/backend_interface.py`
- [Local backend](https://github.com/goabiaryan/relayserve/blob/main/backends/local_backend.py): `backends/local_backend.py`
- [Modal backend](https://github.com/goabiaryan/relayserve/blob/main/backends/modal_backend.py): `backends/modal_backend.py`
- [vLLM backend](https://github.com/goabiaryan/relayserve/blob/main/backends/vllm_backend.py): `backends/vllm_backend.py`
- [llama.cpp client](https://github.com/goabiaryan/relayserve/blob/main/relayserve/internal/runner/runner.py): `relayserve/internal/runner/runner.py`
- [Device runner](https://github.com/goabiaryan/relayserve/blob/main/relayserve/internal/runner/runner.py): `relayserve/internal/runner/runner.py`

### Other components

- **Client**
- **Configured inference services**
- **llama.cpp endpoints**

## Connections

- Client → HTTP API: sends chat (evidence: `README.md`)
- CLI startup → Runtime settings: loads settings (evidence: `relayserve/cli.py`)
- CLI startup → Request application: builds app (evidence: `relayserve/cli.py`)
- CLI startup → HTTP API: starts server (evidence: `relayserve/cli.py`)
- HTTP API → Request application: dispatches requests (evidence: `relayserve/internal/server/http_server.py`)
- Request application → Runtime settings: uses configuration (evidence: `relayserve/internal/server/app.py`)
- Request application → Request queue: enqueues requests (evidence: `relayserve/internal/server/app.py`)
- Request application → Device scheduler: selects device (evidence: `relayserve/internal/server/app.py`)
- Request application → Device probe: probes devices (evidence: `relayserve/internal/server/app.py`)
- Request application → Device registry: lists devices (evidence: `relayserve/internal/server/app.py`)
- Device probe → Device registry: creates devices (evidence: `relayserve/internal/profile/probe.py`)
- Device scheduler → Device registry: reads devices (evidence: `relayserve/internal/scheduler/scheduler.py`)
- Request application → Shard planner: plans placement (evidence: `relayserve/internal/server/app.py`)
- Shard planner → Device registry: reads devices (evidence: `relayserve/internal/shard/plan.py`)
- Request application → KV cache manager: tracks KV prefixes (evidence: `relayserve/internal/server/app.py`)
- Request application → Metrics collector: records metrics (evidence: `relayserve/internal/server/app.py`)
- Request application → Backend router: selects backend (evidence: `relayserve/internal/server/app.py`)
- Backend router → Local backend: builds backend (evidence: `router.py`)
- Backend router → Modal backend: builds backend (evidence: `router.py`)
- Backend router → vLLM backend: builds backend (evidence: `router.py`)
- Local backend → Backend interface: implements (evidence: `backends/local_backend.py`)
- Modal backend → Backend interface: implements (evidence: `backends/modal_backend.py`)
- vLLM backend → Backend interface: implements (evidence: `backends/vllm_backend.py`)
- Local backend → Configured inference services: sends inference (evidence: `backends/local_backend.py`)
- Modal backend → Configured inference services: sends inference (evidence: `backends/modal_backend.py`)
- vLLM backend → Configured inference services: sends inference (evidence: `backends/vllm_backend.py`)
- Request application → llama.cpp client: requests completion (evidence: `relayserve/internal/server/app.py`)
- Request application → Device runner: runs fallback (evidence: `relayserve/internal/server/app.py`)
- llama.cpp client → llama.cpp endpoints: sends completion (evidence: `relayserve/internal/runner/runner.py`)
- HTTP API → Client: returns response (evidence: `README.md`)
