Features / 11 of 14
Basics
What llama-swap does
One API in front of all your local models.
Inference servers like llama-server and vLLM load one model each. To use several models you would run several servers, on several ports, and start and stop them yourself.
llama-swap is a proxy that does this for you. Clients talk to one address. Each
request names a model in its model field, and llama-swap:
- starts the server for that model, if it is not running,
- stops the server it replaces, so the GPU memory is free,
- forwards the request and streams the response back.
client ── "model": "qwen" ──▶ llama-swap :8080 ──▶ llama-server (qwen)
└─ stops llama-server (gemma)
It is one Go binary and one YAML file, with no other dependencies. It works with any server that speaks the OpenAI or Anthropic API: llama.cpp and its forks, vLLM, stable-diffusion.cpp, whisper.cpp, ComfyUI and others.
Ask the llama
“What is llama-swap and when should I use it?”
Read more
Basics
Your first config
A model is a name and the command that starts its server.
Each entry under models is a model ID that clients can ask for. cmd is the
command that starts its server. llama-swap gives each model its own port
(from 5800 up) and puts it in ${PORT}.
# config.yaml
models:
qwen:
cmd: llama-server --port ${PORT} -m /models/qwen3-8b.gguf
gemma:
cmd: llama-server --port ${PORT} -m /models/gemma-3-12b.gguf
Start llama-swap with the file. It listens on port 8080.
llama-swap --config config.yaml
# add -watch-config to reload the file when it changes
llama-swap --config config.yaml -watch-config
Then call it like any OpenAI compatible server:
curl localhost:8080/v1/chat/completions \
-H 'Content-Type: application/json' \
-d '{"model": "qwen", "messages": [{"role": "user", "content": "Hi"}]}'
Run llama-swap --config config.yaml -validate to check a config without
starting the server.
Ask the llama
“How do I write a minimal llama-swap config for two llama.cpp models?”
Basics
Swapping models on demand
How llama-swap decides a model is ready.
When a request asks for a model that is not running, llama-swap stops the
running one, starts the new one, and holds the request until the new server
answers its health check. By default it polls /health on the server.
# seconds to wait for a model to become ready (default 120)
healthCheckTimeout: 300
# show "loading…" messages in chat clients while a model starts
sendLoadingState: true
models:
qwen:
cmd: llama-server --port ${PORT} -m /models/qwen3-8b.gguf
# the server's health endpoint, if it is not /health
checkEndpoint: /health
Clients see a slow first response and nothing else: no errors, no retries.
sendLoadingState sends loading progress in the reasoning field, so chat
apps like Open WebUI show that the model is starting.
If a model never becomes ready, its server log explains why. The web UI's
Logs page shows it, as does curl localhost:8080/logs/stream/qwen.
Ask the llama
“How does llama-swap know when a model has finished loading?”
Basics
The web UI
Test models, watch requests and read logs at /ui.
The web UI is at /ui. Nothing to install or configure. The sidebar has these pages:
- Activity: recent requests and their token metrics.
- Playground: chat and other tests against any model.
- Models: model state, with load and unload buttons.
- Logs: live logs from llama-swap and each model.
- Performance and Hardware: speed over time, and GPU and memory use.
- Help: a chat that answers questions about llama-swap and your config.
The UI can tag Activity rows with a session ID read from request headers:
ui:
activity:
session_id: ["X-Session-ID"]
Ask the llama
“What can I do in llama-swap's web UI?”
Read more
Basics
Unloading idle models
Free GPU memory after a model sits unused.
By default a model stays loaded until another model needs its place. Set a
ttl (time to live, in seconds) to unload it after it has been idle that long.
Each request resets the timer.
# default for every model; 0 means never unload
globalTTL: 600
models:
qwen:
cmd: llama-server --port ${PORT} -m /models/qwen3-8b.gguf
ttl: 300 # unload after 5 minutes idle
embeddings:
cmd: llama-server --port ${PORT} -m /models/bge-m3.gguf --embedding
ttl: 0 # never unload
ttl |
Meaning |
|---|---|
not set, or -1 |
use globalTTL |
0 |
never unload automatically |
more than 0 |
unload after this many idle seconds |
To unload by hand, use the Models page in the UI, or
curl -X POST localhost:8080/api/models/unload.
Ask the llama
“How do I unload a model after five minutes idle?”
Read more
Intermediate
Macros
Write a long command once and reuse it.
Model commands are often long and mostly the same. A macro is a named piece of
text you insert with ${name}.
macros:
llama: /opt/llama.cpp/build/bin/llama-server --port ${PORT} -ngl 99
ctx: 8192
models_dir: ${env.HOME}/models # read an environment variable
models:
qwen:
cmd: ${llama} -m ${models_dir}/qwen3-8b.gguf --ctx-size ${ctx}
qwen-long:
macros:
ctx: 32768 # overrides the global value here only
cmd: ${llama} -m ${models_dir}/qwen3-8b.gguf --ctx-size ${ctx}
Rules to know:
- A macro can use other macros, but only ones defined above it.
PORT,MODEL_IDandPIDare built in and cannot be redefined.${env.NAME}reads an environment variable, which keeps paths and secrets out of the file.
Ask the llama
“How do I avoid repeating a long command for every model?”
Read more
Intermediate
Names, aliases and listings
Control what clients see in /v1/models.
Clients find models with GET /v1/models. These settings change what they
see and which names a model answers to:
models:
qwen:
cmd: llama-server --port ${PORT} -m /models/qwen3-8b.gguf
name: "Qwen3 8B" # display name in listings and the UI
description: "Fast general chat model"
aliases:
- gpt-4o-mini # requests for this name go to qwen
useModelName: "qwen3-8b" # model name sent to the upstream server
draft-model:
cmd: llama-server --port ${PORT} -m /models/qwen3-0.6b.gguf
unlisted: true # works, but is not listed
Aliases help with clients that only offer a fixed list of model names. Set
includeAliasesInList: true at the top level to list aliases too.
Ask the llama
“How do I make a model answer to another name, like gpt-4o-mini?”
Intermediate
API keys
Require a key for every request.
llama-swap accepts any request by default. Add apiKeys and every request,
including the web UI and /api/ routes, needs one of them.
apiKeys:
- "${env.LLAMA_SWAP_KEY}" # keep the key out of the file
- "sk-old-key-being-rotated"
Clients send the key as Authorization: Bearer <key>, x-api-key: <key>, or
HTTP Basic auth. All keys have the same access. To rotate a key, add the new
one, move clients to it, then remove the old one.
Make a random key:
printf "sk-%s\n" "$(head -c 48 /dev/urandom | base64)"
An API key does not make it safe to put llama-swap on the internet: it starts processes on your machine. Keep it behind a firewall or VPN.
Ask the llama
“How do I require an API key?”
Intermediate
Models in containers
Run servers with Docker or Podman.
cmd can be any command that starts a server, including docker run. Two
extra settings make containers work well:
cmdStopstops the container. Without it llama-swap only stops the localdockerclient, and the container keeps its GPU memory.proxyis the address llama-swap forwards to, when it is not the defaulthttp://localhost:${PORT}.
models:
vllm-qwen:
proxy: http://127.0.0.1:${PORT}
cmd: |
docker run --name ${MODEL_ID} --rm --gpus all
-p ${PORT}:8000 -v /models:/models
vllm/vllm-openai:latest --model /models/Qwen3-8B
cmdStop: docker stop ${MODEL_ID}
unloadTimeout: 30 # seconds to wait for a slow shutdown
ttl: 600
cmd can span several lines; llama-swap joins them into one command. The
llama-swap Docker images also come with llama-server, whisper.cpp and
stable-diffusion.cpp built in, so many setups need no extra containers.
Ask the llama
“How do I run a model with docker run and stop the container when it unloads?”
Intermediate
Rewriting requests with filters
Remove or force request parameters per model.
Filters change the JSON request body before it reaches the model's server.
models:
qwen:
cmd: llama-server --port ${PORT} -m /models/qwen3-8b.gguf
filters:
# delete these keys from the request
stripParams: "top_k, min_p"
setParams:
temperature: 0.7 # always this value
max_tokens?: 4096 # "?" sets it only when the client did not
setParamsByID goes further: it creates extra model IDs that share one loaded
model and differ only in the parameters they set. Switching between them does
not reload anything.
models:
gpt-oss:
cmd: llama-server --port ${PORT} -m /models/gpt-oss-20b.gguf
filters:
setParamsByID:
"${MODEL_ID}:high":
chat_template_kwargs: { reasoning_effort: high }
"${MODEL_ID}:low":
chat_template_kwargs: { reasoning_effort: low }
Clients can now ask for gpt-oss:high or gpt-oss:low. The model field
itself can never be changed by a filter.
Ask the llama
“How do I force a temperature for a model no matter what the client sends?”
Advanced
Running several models at once
Keep more than one model loaded, as far as your hardware allows.
By default llama-swap runs one model at a time: starting a model stops the
one before it. The routing section lets several models stay loaded together.
Pick one of three routers with use:
| Router | Use it when |
|---|---|
group |
your setup is "these run together, those swap out". You name groups of models. The simplest, and the default. |
matrix |
what fits depends on the models, such as a 70B that needs every GPU alone but several small models that fit together. You list the combinations. |
capacity |
you know how much memory each model needs. You give the limits and llama-swap works out what fits. |
In every router, only the requested model is started. The others start when they are first requested.
group
Name groups of models and say how each group behaves. Each model belongs to at most one group.
routing:
router:
use: group
settings:
groups:
chat: # the big models: one at a time
swap: true
exclusive: true
members: [qwen, gemma, gpt-oss]
helpers: # small models that can all run together
swap: false
exclusive: false
members: [embeddings, reranker]
always-on: # nothing else can unload these
persistent: true
swap: false
exclusive: false
members: [whisper]
| Setting | true means |
|---|---|
swap |
only one member of this group runs at a time |
exclusive |
starting a member stops models in other groups |
persistent |
other groups can never stop this group's models |
matrix
List the combinations of models that fit. On each request llama-swap picks a combination that contains the requested model and stops the running models that cost least to stop.
routing:
router:
use: matrix
settings:
matrix:
vars: # short names for use in sets
g: gemma
q: qwen
v: voxtral
evict_costs: # how costly a model is to stop (default 1)
v: 50 # slow to start again, so avoid stopping it
sets:
chat_and_voice: "(g | q) & v"
big: "llama-70b"
In a set, & means "run together", | means "one of", and () groups.
"(g | q) & v" allows gemma with voxtral, or qwen with voxtral. A model
that is in no set can only run alone.
capacity
Declare resource limits, such as the memory of each GPU, and how much of each one every model uses. Any combination that fits can run; there is no list to write.
routing:
router:
use: capacity
settings:
capacity:
pools: # your own names and units, here MB
gpu0: 24000
ram: 64000
models:
qwen-32b:
cmd: llama-server --port ${PORT} -m /models/qwen3-32b.gguf
capacities: { gpu0: 20000, ram: 4000 }
evict_cost: 100 # prefer stopping other models first
embeddings:
cmd: llama-server --port ${PORT} -m /models/bge-m3.gguf --embedding
capacities: { gpu0: 2000 }
whisper:
cmd: whisper-server --port ${PORT} -m /models/whisper-large.bin
capacities: { gpu0: 3000 }
llama-swap does not measure memory. Use numbers that include the context and the server's own overhead.
Ask the llama
“How do I choose between the group, matrix and capacity routers to run several models at once?”
Advanced
Profiles and selectors
Change which real model a name points to.
Both let clients ask for a fixed name and get a different real model.
A profile is a set of name mappings that you switch on by hand, from the
web UI or the API. Your editor always asks for llm-code; the active profile
decides which model that is.
profiles:
coding:
description: "Coding models"
pins:
llm-code: qwen3-coder-30b
llm-chat: gpt-oss-120b
writing:
pins:
llm-code: gemma-3-27b
llm-chat: gemma-3-27b
curl -X PUT localhost:8080/api/profiles/active -d '{"name": "coding"}'
A selector is a name that is resolved on every request. With the warm
strategy it uses whichever target is already loaded, so you do not wait for a
swap.
selectors:
any-coder:
strategy: warm # also: pin, spillover
targets: [qwen3-coder-30b, gpt-oss-120b]
Ask the llama
“What is the difference between profiles and selectors, and when should I use each?”
Read more
Advanced
Preloading at startup
Have models ready before the first request.
The on_startup hook loads models, and can pick a profile, as soon as
llama-swap starts. The first request then does not wait for a model to load.
hooks:
on_startup:
profile: coding # activate this profile at startup and on reload
preload:
- qwen
- embeddings
routing:
router:
use: group
settings:
groups:
warm:
swap: false
exclusive: false
members: [qwen, embeddings]
Preloading several models only keeps them all loaded if they are allowed to run
together. Without the group above, loading embeddings would unload qwen.
Give preloaded models ttl: 0 if they should never unload.
Ask the llama
“How do I load a model when llama-swap starts?”
Read more
Advanced
Peers and remote models
Use models on other machines and cloud APIs through one address.
A peer is another llama-swap, or any OpenAI compatible API, that this
llama-swap forwards to. Its models appear as peer-name/model-id next to your
local models.
peers:
gpu-box:
proxy: http://192.168.1.23:8080
apiKey: ${env.GPU_BOX_KEY}
models: [qwen3-235b, flux-dev]
openrouter:
proxy: https://openrouter.ai/api
apiKey: ${env.OPENROUTER_API_KEY}
models: [deepseek/deepseek-v3.2]
filters:
setParams:
provider: { data_collection: deny }
Clients now ask for gpu-box/qwen3-235b or openrouter/deepseek/deepseek-v3.2
at the same address they use for local models. Peers accept the same
filters as models.
Combine peers with a spillover selector to use a local model first and
move extra requests to a remote one when it is busy.
Ask the llama
“How do I use models on another computer?”