# scaleapi/llm-engine architecture

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

- Interactive diagram: https://gitdiagram.com/scaleapi/llm-engine
- Repository: https://github.com/scaleapi/llm-engine

## Overview

#### Purpose and entry points
LLM Engine is a Python client plus a self-hostable Kubernetes control plane for fine-tuning, deploying, and serving LLMs. Hosted users enter through the Python SDK (`clients/python/llmengine/api_engine.py`, `clients/python/llmengine/completion.py`). Self-hosted control-plane processes enter through FastAPI and operational scripts: `model-engine/model_engine_server/entrypoints/start_fastapi_server.py`, `model-engine/model_engine_server/entrypoints/start_batch_job_orchestration.py`, and `model-engine/model_engine_server/entrypoints/init_database.py`.

#### API boundary and application composition
The FastAPI app exposes versioned REST resources for LLMs, endpoints, bundles, files, jobs, tasks, and triggers. API handlers translate HTTP DTOs into domain use cases; dependency wiring chooses live or fake/configured implementations. OpenAI-compatible completion/chat/batch routes are separated under v2.
Paths: `model-engine/model_engine_server/api/app.py`, `model-engine/model_engine_server/api/model_endpoints_v1.py`, `model-engine/model_engine_server/api/v2/chat_completion.py`.

#### Domain orchestration layer
Domain entities model endpoints, model bundles, LLMs/fine-tunes, files, and batch jobs. Use cases coordinate authorization, persistence, endpoint lifecycle, inference, streaming, fine-tuning, and scheduled triggers behind gateway interfaces. This is the principal control boundary: domain code depends on abstract gateways/repositories, while infrastructure supplies cloud/Kubernetes implementations.
Paths: `model-engine/model_engine_server/domain/use_cases/model_endpoint_use_cases.py`, `model-engine/model_engine_server/domain/use_cases/sync_inference_use_cases.py`, `model-engine/model_engine_server/domain/gateways/sync_model_endpoint_inference_gateway.py`.

#### Endpoint provisioning and lifecycle
Endpoint creation resolves model/bundle metadata and provisions Kubernetes-backed serving resources through a live infrastructure gateway. Resource delegates support direct Kubernetes endpoints and queue-backed endpoint variants; cache/image-cache services support faster startup. The Helm deployment includes dedicated endpoint-builder and cacher workloads.
Paths: `model-engine/model_engine_server/infra/services/live_endpoint_builder_service.py`, `model-engine/model_engine_server/infra/gateways/live_model_endpoint_infra_gateway.py`, `model-engine/model_engine_server/infra/gateways/resources/k8s_endpoint_resource_delegate.py`.

#### Inference execution paths
Synchronous, streaming, and asynchronous inference are distinct paths from API/domain gateways to deployed endpoint services. Serving images and wrappers support vLLM, SGLang, TensorRT-LLM/Triton, generic HTTP forwarding, and Celery forwarding. Async work is queued through Celery; batch inference has vLLM/Ray-oriented execution assets.
Paths: `model-engine/model_engine_server/inference/sync_inference/fastapi_server.py`, `model-engine/model_engine_server/inference/vllm/vllm_server.py`, `model-engine/model_engine_server/inference/async_inference/tasks.py`.

#### Persistence, queues, and external integrations
PostgreSQL stores control-plane records and schema migrations. Redis supports caches, feature flags, and autoscaling metrics; Celery provides task-queue execution. Artifact/file abstractions select S3 or GCS implementations; container registries include ECR, GAR, and ACR. Datadog provides monitoring, while AWS/GCP secret and role adapters isolate cloud specifics.
Paths: `model-engine/model_engine_server/db/base.py`, `model-engine/model_engine_server/infra/gateways/s3_llm_artifact_gateway.py`, `model-engine/model_engine_server/infra/gateways/gcs_llm_artifact_gateway.py`.

#### Deployment and operations
Helm is the self-hosting integration boundary: it deploys gateway/control-plane workloads, endpoint builder, cache, Celery autoscaler, database initialization/migration jobs, RBAC, autoscaling, and optional Istio routing. A separate startup watcher observes Kubernetes startup behavior. Dockerfiles package control-plane and inference runtimes.
Paths: `charts/model-engine/templates/gateway_deployment.yaml`, `charts/model-engine/templates/endpoint_builder_deployment.yaml`, `charts/k8s-startup-watcher/templates/deployment.yaml`.

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Client access"]
  node_python_sdk["Python SDK<br/>client library<br/>[api_engine.py]"]
  node_completion_sdk["Completion API<br/>SDK module<br/>[completion.py]"]
end

subgraph group_control["Control plane"]
  node_fastapi_app["FastAPI app<br/>REST API<br/>[app.py]"]
  node_v1_resources["v1 resources<br/>API handlers"]
  node_openai_v2["OpenAI-compatible v2<br/>API handlers<br/>[chat_completion.py]"]
  node_domain_use_cases["Domain use cases<br/>application layer"]
  node_sync_inference_use_cases["Sync inference use cases<br/>application layer"]
  node_inference_gateway_contract["Inference gateway contract<br/>domain gateway"]
  node_live_endpoint_gateway["Live endpoint gateway<br/>infrastructure gateway"]
end

subgraph group_serving["Serving and execution"]
  node_endpoint_builder["Endpoint builder<br/>provisioning service"]
  node_k8s_endpoint_delegate["Kubernetes endpoint delegate<br/>resource delegate"]
  node_sync_inference_server["Sync inference server<br/>serving runtime<br/>[fastapi_server.py]"]
  node_vllm_server["vLLM server<br/>serving runtime<br/>[vllm_server.py]"]
  node_async_tasks["Async inference tasks<br/>Celery worker tasks<br/>[tasks.py]"]
end

subgraph group_platform["Platform integrations"]
  node_postgres[("PostgreSQL<br/>control-plane database<br/>[base.py]")]
  node_artifact_storage["Artifact storage<br/>object storage gateway"]
  node_gcs_artifact_storage["GCS artifact storage<br/>object storage gateway"]
end

subgraph group_operations["Deployment"]
  node_helm_gateway{{"Gateway deployment<br/>Helm workload"}}
  node_helm_endpoint_builder{{"Endpoint builder deployment<br/>Helm workload"}}
  node_startup_watcher["Kubernetes startup watcher<br/>operational workload<br/>[deployment.yaml]"]
end

node_python_sdk -->|"uses"| node_completion_sdk
node_completion_sdk -->|"calls"| node_fastapi_app
node_fastapi_app -->|"routes v1"| node_v1_resources
node_fastapi_app -->|"routes v2"| node_openai_v2
node_v1_resources -->|"translates DTOs"| node_domain_use_cases
node_openai_v2 -->|"invokes"| node_sync_inference_use_cases
node_domain_use_cases -->|"coordinates inference"| node_sync_inference_use_cases
node_domain_use_cases -.->|"uses gateway"| node_live_endpoint_gateway
node_domain_use_cases -->|"persists records"| node_postgres
node_domain_use_cases -->|"stores artifacts"| node_artifact_storage
node_domain_use_cases -.->|"alternative backend"| node_gcs_artifact_storage
node_sync_inference_use_cases -.->|"uses contract"| node_inference_gateway_contract
node_inference_gateway_contract -.->|"implemented by"| node_sync_inference_server
node_live_endpoint_gateway -->|"provisions through"| node_endpoint_builder
node_endpoint_builder -->|"creates resources"| node_k8s_endpoint_delegate
node_k8s_endpoint_delegate -->|"deploys endpoint"| node_sync_inference_server
node_sync_inference_server -->|"serves models with"| node_vllm_server
node_domain_use_cases -->|"queues async work"| node_async_tasks
node_helm_gateway -->|"deploys"| node_fastapi_app
node_helm_endpoint_builder -->|"deploys"| node_endpoint_builder
node_startup_watcher -.->|"watches startup"| node_helm_gateway

click node_python_sdk "https://github.com/scaleapi/llm-engine/blob/main/clients/python/llmengine/api_engine.py"
click node_completion_sdk "https://github.com/scaleapi/llm-engine/blob/main/clients/python/llmengine/completion.py"
click node_fastapi_app "https://github.com/scaleapi/llm-engine/blob/main/model-engine/model_engine_server/api/app.py"
click node_v1_resources "https://github.com/scaleapi/llm-engine/blob/main/model-engine/model_engine_server/api/model_endpoints_v1.py"
click node_openai_v2 "https://github.com/scaleapi/llm-engine/blob/main/model-engine/model_engine_server/api/v2/chat_completion.py"
click node_domain_use_cases "https://github.com/scaleapi/llm-engine/blob/main/model-engine/model_engine_server/domain/use_cases/model_endpoint_use_cases.py"
click node_sync_inference_use_cases "https://github.com/scaleapi/llm-engine/blob/main/model-engine/model_engine_server/domain/use_cases/sync_inference_use_cases.py"
click node_inference_gateway_contract "https://github.com/scaleapi/llm-engine/blob/main/model-engine/model_engine_server/domain/gateways/sync_model_endpoint_inference_gateway.py"
click node_live_endpoint_gateway "https://github.com/scaleapi/llm-engine/blob/main/model-engine/model_engine_server/infra/gateways/live_model_endpoint_infra_gateway.py"
click node_endpoint_builder "https://github.com/scaleapi/llm-engine/blob/main/model-engine/model_engine_server/infra/services/live_endpoint_builder_service.py"
click node_k8s_endpoint_delegate "https://github.com/scaleapi/llm-engine/blob/main/model-engine/model_engine_server/infra/gateways/resources/k8s_endpoint_resource_delegate.py"
click node_sync_inference_server "https://github.com/scaleapi/llm-engine/blob/main/model-engine/model_engine_server/inference/sync_inference/fastapi_server.py"
click node_vllm_server "https://github.com/scaleapi/llm-engine/blob/main/model-engine/model_engine_server/inference/vllm/vllm_server.py"
click node_async_tasks "https://github.com/scaleapi/llm-engine/blob/main/model-engine/model_engine_server/inference/async_inference/tasks.py"
click node_postgres "https://github.com/scaleapi/llm-engine/blob/main/model-engine/model_engine_server/db/base.py"
click node_artifact_storage "https://github.com/scaleapi/llm-engine/blob/main/model-engine/model_engine_server/infra/gateways/s3_llm_artifact_gateway.py"
click node_gcs_artifact_storage "https://github.com/scaleapi/llm-engine/blob/main/model-engine/model_engine_server/infra/gateways/gcs_llm_artifact_gateway.py"
click node_helm_gateway "https://github.com/scaleapi/llm-engine/blob/main/charts/model-engine/templates/gateway_deployment.yaml"
click node_helm_endpoint_builder "https://github.com/scaleapi/llm-engine/blob/main/charts/model-engine/templates/endpoint_builder_deployment.yaml"
click node_startup_watcher "https://github.com/scaleapi/llm-engine/blob/main/charts/k8s-startup-watcher/templates/deployment.yaml"

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_python_sdk,node_completion_sdk toneBlue
class node_fastapi_app,node_v1_resources,node_openai_v2,node_domain_use_cases,node_sync_inference_use_cases,node_inference_gateway_contract,node_live_endpoint_gateway toneAmber
class node_endpoint_builder,node_k8s_endpoint_delegate,node_sync_inference_server,node_vllm_server,node_async_tasks toneMint
class node_postgres,node_artifact_storage,node_gcs_artifact_storage toneRose
class node_helm_gateway,node_helm_endpoint_builder,node_startup_watcher toneIndigo
```

## Components

### Client access

- [Python SDK](https://github.com/scaleapi/llm-engine/blob/main/clients/python/llmengine/api_engine.py): client library · Hosted client entry point for completions. · `clients/python/llmengine/api_engine.py`
- [Completion API](https://github.com/scaleapi/llm-engine/blob/main/clients/python/llmengine/completion.py): SDK module · `clients/python/llmengine/completion.py`

### Control plane

- [FastAPI app](https://github.com/scaleapi/llm-engine/blob/main/model-engine/model_engine_server/api/app.py): REST API · Versioned control-plane HTTP boundary. · `model-engine/model_engine_server/api/app.py`
- [v1 resources](https://github.com/scaleapi/llm-engine/blob/main/model-engine/model_engine_server/api/model_endpoints_v1.py): API handlers · LLM, endpoint, bundle, file, job, task, and trigger routes. · `model-engine/model_engine_server/api/model_endpoints_v1.py`
- [OpenAI-compatible v2](https://github.com/scaleapi/llm-engine/blob/main/model-engine/model_engine_server/api/v2/chat_completion.py): API handlers · Chat, completion, and batch-compatible routes. · `model-engine/model_engine_server/api/v2/chat_completion.py`
- [Domain use cases](https://github.com/scaleapi/llm-engine/blob/main/model-engine/model_engine_server/domain/use_cases/model_endpoint_use_cases.py): application layer · Coordinates authorization, lifecycle, inference, fine-tuning, and triggers through abstractions. · `model-engine/model_engine_server/domain/use_cases/model_endpoint_use_cases.py`
- [Sync inference use cases](https://github.com/scaleapi/llm-engine/blob/main/model-engine/model_engine_server/domain/use_cases/sync_inference_use_cases.py): application layer · `model-engine/model_engine_server/domain/use_cases/sync_inference_use_cases.py`
- [Inference gateway contract](https://github.com/scaleapi/llm-engine/blob/main/model-engine/model_engine_server/domain/gateways/sync_model_endpoint_inference_gateway.py): domain gateway · Abstract boundary for endpoint inference. · `model-engine/model_engine_server/domain/gateways/sync_model_endpoint_inference_gateway.py`
- [Live endpoint gateway](https://github.com/scaleapi/llm-engine/blob/main/model-engine/model_engine_server/infra/gateways/live_model_endpoint_infra_gateway.py): infrastructure gateway · Cloud and Kubernetes implementation of endpoint lifecycle. · `model-engine/model_engine_server/infra/gateways/live_model_endpoint_infra_gateway.py`

### Serving and execution

- [Endpoint builder](https://github.com/scaleapi/llm-engine/blob/main/model-engine/model_engine_server/infra/services/live_endpoint_builder_service.py): provisioning service · Resolves model metadata and creates serving resources. · `model-engine/model_engine_server/infra/services/live_endpoint_builder_service.py`
- [Kubernetes endpoint delegate](https://github.com/scaleapi/llm-engine/blob/main/model-engine/model_engine_server/infra/gateways/resources/k8s_endpoint_resource_delegate.py): resource delegate · Builds direct Kubernetes endpoint resources. · `model-engine/model_engine_server/infra/gateways/resources/k8s_endpoint_resource_delegate.py`
- [Sync inference server](https://github.com/scaleapi/llm-engine/blob/main/model-engine/model_engine_server/inference/sync_inference/fastapi_server.py): serving runtime · Synchronous and streaming endpoint execution. · `model-engine/model_engine_server/inference/sync_inference/fastapi_server.py`
- [vLLM server](https://github.com/scaleapi/llm-engine/blob/main/model-engine/model_engine_server/inference/vllm/vllm_server.py): serving runtime · One of several model-serving backends. · `model-engine/model_engine_server/inference/vllm/vllm_server.py`
- [Async inference tasks](https://github.com/scaleapi/llm-engine/blob/main/model-engine/model_engine_server/inference/async_inference/tasks.py): Celery worker tasks · Queued asynchronous inference execution. · `model-engine/model_engine_server/inference/async_inference/tasks.py`

### Platform integrations

- [PostgreSQL](https://github.com/scaleapi/llm-engine/blob/main/model-engine/model_engine_server/db/base.py): control-plane database · Stores records and migrations. · `model-engine/model_engine_server/db/base.py`
- [Artifact storage](https://github.com/scaleapi/llm-engine/blob/main/model-engine/model_engine_server/infra/gateways/s3_llm_artifact_gateway.py): object storage gateway · S3 implementation selected behind file/artifact abstractions. · `model-engine/model_engine_server/infra/gateways/s3_llm_artifact_gateway.py`
- [GCS artifact storage](https://github.com/scaleapi/llm-engine/blob/main/model-engine/model_engine_server/infra/gateways/gcs_llm_artifact_gateway.py): object storage gateway · Alternative GCS artifact implementation. · `model-engine/model_engine_server/infra/gateways/gcs_llm_artifact_gateway.py`

### Deployment

- [Gateway deployment](https://github.com/scaleapi/llm-engine/blob/main/charts/model-engine/templates/gateway_deployment.yaml): Helm workload · Deploys the control-plane gateway workload. · `charts/model-engine/templates/gateway_deployment.yaml`
- [Endpoint builder deployment](https://github.com/scaleapi/llm-engine/blob/main/charts/model-engine/templates/endpoint_builder_deployment.yaml): Helm workload · Deploys dedicated endpoint provisioning workers. · `charts/model-engine/templates/endpoint_builder_deployment.yaml`
- [Kubernetes startup watcher](https://github.com/scaleapi/llm-engine/blob/main/charts/k8s-startup-watcher/templates/deployment.yaml): operational workload · Observes Kubernetes startup behavior. · `charts/k8s-startup-watcher/templates/deployment.yaml`

## Connections

- Python SDK → Completion API: uses
- Completion API → FastAPI app: calls
- FastAPI app → v1 resources: routes v1
- FastAPI app → OpenAI-compatible v2: routes v2
- v1 resources → Domain use cases: translates DTOs
- OpenAI-compatible v2 → Sync inference use cases: invokes
- Domain use cases → Sync inference use cases: coordinates inference
- Domain use cases → Live endpoint gateway: uses gateway
- Domain use cases → PostgreSQL: persists records
- Domain use cases → Artifact storage: stores artifacts
- Domain use cases → GCS artifact storage: alternative backend
- Sync inference use cases → Inference gateway contract: uses contract
- Inference gateway contract → Sync inference server: implemented by
- Live endpoint gateway → Endpoint builder: provisions through
- Endpoint builder → Kubernetes endpoint delegate: creates resources
- Kubernetes endpoint delegate → Sync inference server: deploys endpoint
- Sync inference server → vLLM server: serves models with
- Domain use cases → Async inference tasks: queues async work
- Gateway deployment → FastAPI app: deploys
- Endpoint builder deployment → Endpoint builder: deploys
- Kubernetes startup watcher → Gateway deployment: watches startup
