MCPcopy Index your code
hub / github.com/mostlygeek/llama-swap

github.com/mostlygeek/llama-swap @v235 sqlite

repository ↗ · DeepWiki ↗ · release v235 ↗
1,316 symbols 5,774 edges 181 files 406 documented · 31%
README

llama-swap header image GitHub Downloads (all assets, all releases) GitHub Actions Workflow Status GitHub Repo stars

llama-swap

Run multiple generative AI models on your machine and hot-swap between them on demand. llama-swap works with any OpenAI and Anthropic API compatible server and is used by thousands of people to power their local AI workflows.

Built in Go for performance and simplicity, llama-swap has zero dependencies and is incredibly easy to set up. Get started in minutes - just one binary and one configuration file.

Features:

  • ✅ Easy to deploy and configure: one binary, one configuration file. no external dependencies
  • ✅ On-demand model switching
  • ✅ Use any local OpenAI compatible server (llama.cpp, vllm, tabbyAPI, stable-diffusion.cpp, etc.)
  • future proof, upgrade your inference servers at any time.
  • ✅ OpenAI API supported endpoints:
  • v1/completions
  • v1/chat/completions
  • v1/responses
  • v1/embeddings
  • v1/models - list available models
  • v1/audio/speech (#36)
  • v1/audio/transcriptions (docs)
  • v1/audio/voices
  • v1/images/generations
  • v1/images/edits
  • ✅ Anthropic API supported endpoints:
  • v1/messages
  • v1/messages/count_tokens
  • ✅ llama-server (llama.cpp) supported endpoints
  • v1/rerank, v1/reranking, /rerank
  • /infill - for code infilling
  • /completion - for completion endpoint
  • /props - requires ?model={model_id} query parameter to be provided. The autoload parameter is not supported and will be ignored.
  • ✅ SDAPI via stable-diffusion.cpp's server
  • /sdapi/v1/txt2img
  • /sdapi/v1/img2img
  • /sdapi/v1/loras - requires model in request body to fetch the correct loras
  • ✅ llama-swap API
  • /ui - web UI
  • /upstream/:model_id - direct access to upstream server (demo)
  • /running - list currently running models (#61)
  • POST /api/models/unload - manually unload all running models (#58)
  • POST /api/models/unload/:model_id - unload a specific model
  • /logs - remote log monitoring
    • GET /logs returns buffered plain text logs.
    • If Accept: text/html is sent, /logs redirects to /ui/.
    • GET /logs/stream keeps the connection open for live log streaming.
    • Stream endpoints send buffered history first by default; add ?no-history to stream only new lines.
    • GET /logs/stream/proxy streams proxy logs only.
    • GET /logs/stream/upstream streams upstream process logs only.
    • GET /logs/stream/{model_id} streams logs for one model (including IDs with slashes, like author/model).
  • /health - just returns "OK"
  • /metrics - system and GPU metrics for prometheus
  • ✅ API Key support - define keys to restrict access to API endpoints
  • ✅ Customizable
  • Run concurrent models with a custom DSL swap matrix (#643)
  • Automatic unloading of models after timeout by setting a ttl
  • Docker and Podman support using cmd and cmdStop together
  • Preload models on startup with hooks (#235)
  • Apply filters to requests to control inference with stripParams, setParams and setParamsByID

Web UI

llama-swap includes a real time web interface with a playground for testing out all sorts of local models:

image

View detailed token metrics:

image

Inspect request and responses:

image

Manually load and unload models:

image

Real time log streaming:

image

Installation

llama-swap can be installed in multiple ways

  1. Docker
  2. Homebrew (macOS and Linux)
  3. MacPorts (macOS)
  4. WinGet
  5. From release binaries
  6. From source

Docker Install (download images)

Two types of container images are built nightly for llama-swap:

  1. A unified container with llama-server, ik-llama-server, stable-diffusion.cpp, whisper.cpp and llama-swap built from source. This is only available for cuda and vulkan but has more capabilities. This one is recommended for use.
  2. A legacy image that is based on llama.cpp's images and llama-swap copied into the container. Use this one if you prefer to stay close to llama.cpp's container images.

Unified container (Recommended)

$ docker pull ghcr.io/mostlygeek/llama-swap:unified-cuda

# run with a custom configuration and models directory
$ docker run -it --rm --runtime nvidia -p 9292:8080 \
 -v /path/to/models:/models \
 -v /path/to/custom/config.yaml:/etc/llama-swap/config/config.yaml \
 ghcr.io/mostlygeek/llama-swap:unified-cuda

Legacy container

$ docker pull ghcr.io/mostlygeek/llama-swap:cuda

# run with a custom configuration and models directory
$ docker run -it --rm --runtime nvidia -p 9292:8080 \
 -v /path/to/models:/models \
 -v /path/to/custom/config.yaml:/app/config.yaml \
 ghcr.io/mostlygeek/llama-swap:cuda

more examples

# pull latest images per platform
docker pull ghcr.io/mostlygeek/llama-swap:cpu
docker pull ghcr.io/mostlygeek/llama-swap:cuda
docker pull ghcr.io/mostlygeek/llama-swap:vulkan
docker pull ghcr.io/mostlygeek/llama-swap:intel
docker pull ghcr.io/mostlygeek/llama-swap:musa

# tagged llama-swap, platform and llama-server version images
docker pull ghcr.io/mostlygeek/llama-swap:v166-cuda-b6795

# non-root cuda
docker pull ghcr.io/mostlygeek/llama-swap:cuda-non-root

Homebrew Install (macOS/Linux)

brew tap mostlygeek/llama-swap
brew install llama-swap
llama-swap --config path/to/config.yaml --listen localhost:8080

MacPorts (macOS)

[!NOTE] Maintained by MacPorts community - llama-swap port. It is not an official part of llama-swap.

sudo port install llama-swap
llama-swap --config path/to/config.yaml --listen localhost:8080

WinGet Install (Windows)

[!NOTE] WinGet is maintained by community contributor Dvd-Znf (#327). It is not an official part of llama-swap.

# install
C:\> winget install llama-swap

# upgrade
C:\> winget upgrade llama-swap

Pre-built Binaries

Binaries are available on the release page for Linux, Mac, Windows and FreeBSD.

Building from source

  1. Building requires Go and Node.js (for UI).
  2. git clone https://github.com/mostlygeek/llama-swap.git
  3. make clean all
  4. look in the build/ subdirectory for the llama-swap binary

Configuration

# minimum viable config.yaml

models:
  model1:
    cmd: llama-server --port ${PORT} --model /path/to/model.gguf

That's all you need to get started:

  1. models - holds all model configurations
  2. model1 - the ID used in API calls
  3. cmd - the command to run to start the server.
  4. ${PORT} - an automatically assigned port number

Almost all configuration settings are optional and can be added one step at a time:

  • Advanced features
  • matrix to run concurrent models with a custom swap logic DSL
  • hooks to run things on startup
  • macros reusable snippets
  • Model customization
  • ttl to automatically unload models
  • aliases to use familiar model names (e.g., "gpt-4o-mini")
  • env to pass custom environment variables to inference servers
  • cmdStop gracefully stop Docker/Podman containers
  • useModelName to override model names sent to upstream servers
  • ${PORT} automatic port variables for dynamic port assignment
  • filters rewrite parts of requests before sending to the upstream server

See the configuration documentation for all options.

How does llama-swap work?

When a request is made to an OpenAI compatible endpoint, llama-swap will extract the model value and load the appropriate server configuration to serve it. If the wrong upstream server is running, it will be replaced with the correct one. This is where the "swap" part comes in. The upstream server is automatically swapped to handle the request correctly.

In the most basic configuration llama-swap handles one model at a time. For more advanced use cases, using a matrix allows multiple models to be loaded at the same time. You have complete control over how your system resources are used.

Reverse Proxy Configuration (nginx)

If you deploy llama-swap behind nginx, disable response buffering for streaming endpoints. By default, nginx buffers responses which breaks Server‑Sent Events (SSE) and streaming chat completion. (#236)

Recommended nginx configuration snippets:

# SSE for UI events/logs
location /api/events {
    proxy_pass http://your-llama-swap-backend;
    proxy_buffering off;
    proxy_cache off;
}

# Streaming chat completions (stream=true)
location /v1/chat/completions {
    proxy_pass http://your-llama-swap-backend;
    proxy_buffering off;
    proxy_cache off;
}

As a safeguard, llama-swap also sets X-Accel-Buffering: no on SSE responses. However, explicitly disabling proxy_buffering at your reverse proxy is still recommended for reliable streaming behavior.

Monitoring Logs on the CLI

# sends up to the last 10KB of logs
$ curl http://host/logs

# streams combined logs
curl -Ns http://host/logs/stream

# stream llama-swap's proxy status logs
curl -Ns http://host/logs/stream/proxy

# stream logs from upstream processes that llama-swap loads
curl -Ns http://host/logs/stream/upstream

# stream logs only from a specific model
curl -Ns http://host/logs/stream/{model_id}

# stream and filter logs with linux pipes
curl -Ns http://host/logs/stream | grep 'eval time'

# appending ?no-history will disable sending buffered history first
curl -Ns 'http://host/logs/stream?no-history'

Do I need to use llama.cpp's server (llama-server)?

Any OpenAI compatible server would work. llama-swap was originally designed for llama-server and it is the best supported.

For Python based inference servers like vllm or tabbyAPI it is recommended to run them via podman or docker. This provides clean environment isolation as well as responding correctly to SIGTERM signals for proper shutdown.

Star History

[!NOTE] Thank you to everyone who has given this project a ⭐️!

Star History Chart

Extension points exported contracts — how you extend this code

Event (Interface)
Event represents an event contract [9 implementers]
internal/event/event.go
Swapper (Interface)
Swapper is the eviction policy: it decides which running models must be stopped before a target can serve. It is orthogo [4 …
internal/router/scheduler/scheduler.go
HTTPError (Interface)
HTTPError is an error that carries a complete HTTP response. A producer (e.g. a scheduler shedding a request) returns on [1 …
internal/shared/httperror.go
Middleware (FuncType)
Middleware wraps an http.Handler with cross-cutting behavior. It receives the next handler in the chain and returns a ha
internal/chain/chain.go
Process (Interface)
(no doc) [2 implementers]
internal/process/process.go
GroupedModels (Interface)
(no doc)
ui-svelte/src/lib/modelUtils.ts
LocalRouter (Interface)
LocalRouter is a Router backed by local processes whose state can be inspected and which can be individually stopped. Pe [2 …
internal/router/router.go
RenderedBlock (Interface)
(no doc)
ui-svelte/src/lib/markdown.ts

Core symbols most depended-on inside this repo

Errorf
called by 497
internal/logmon/logging.go
Error
called by 193
internal/logmon/logging.go
Run
called by 171
internal/process/process.go
ServeHTTP
called by 117
internal/router/router.go
LoadConfigFromReader
called by 116
internal/config/load.go
String
called by 90
internal/logmon/logging.go
Get
called by 83
internal/cache/cache.go
Header
called by 82
internal/shared/httperror.go

Shape

Function 793
Method 300
Struct 150
Interface 45
TypeAlias 18
Class 8
FuncType 2

Languages

Go90%
TypeScript10%

Modules by API surface

internal/router/scheduler/fifo_test.go53 symbols
internal/config/config_test.go37 symbols
internal/server/metrics.go29 symbols
internal/logmon/logging.go29 symbols
ui-svelte/src/lib/types.ts28 symbols
internal/config/matrix_dsl.go28 symbols
internal/router/scheduler/fifo.go27 symbols
internal/server/server_test.go24 symbols
internal/perf/monitor_test.go23 symbols
internal/event/event.go23 symbols
internal/config/merge_test.go23 symbols
internal/router/scheduler/scheduler.go22 symbols

Dependencies from manifests, versioned

github.com/aymanbagabas/go-osc52/v2v2.0.1 · 1×
github.com/billziss-gh/golibv0.2.0 · 1×
github.com/charmbracelet/bubblesv1.0.0 · 1×
github.com/charmbracelet/colorprofilev0.4.1 · 1×
github.com/charmbracelet/x/ansiv0.11.6 · 1×
github.com/charmbracelet/x/cellbufv0.0.15 · 1×
github.com/charmbracelet/x/termv0.2.2 · 1×
github.com/clipperhouse/displaywidthv0.9.0 · 1×

For agents

$ claude mcp add llama-swap \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact