← Blog
A lavender selector switch with an orange-tipped lever tilted to the right.
Published on
2026-11-24

llama-swap: switch between two local AI models on one host

If you use one local language model for an hour and another for five minutes, keeping both servers in memory all day may buy you little. With llama-swap, a client can keep one API address and name the model it wants in each request. On a single host, the default configuration starts the requested backend and stops the previous one when the model changes. The first request after a switch pays the cost of starting the server and loading its weights.

We tried this with two small GGUF model files and the CPU build of llama.cpp on Windows. We sent four text-completion requests in the order A, A, B, A. Each produced eight tokens. The two consecutive A requests shared a process. Returning to A after B started a new process with A’s weights. Here’s how to reproduce that result and decide whether switching fits your own traffic.

Set up two models behind one local address

Keep the two programs’ jobs straight. llama-swap accepts the client request, selects the configured model and manages the backend process. llama-server, supplied by llama.cpp, opens the GGUF file and generates the tokens. The client only needs the address of llama-swap.

Create a working directory and put the following files under it. These paths match the tested Windows x64 configuration. Keep the DLLs from the CPU package beside llama-server.exe.

  • Extract llama-swap.exe into runtime/v275/. llama-swap v275 release
  • Extract the Windows CPU x64 build of llama.cpp so the executable is at runtime/cpu/llama-server.exe. llama.cpp build b12676
  • Save stories260K.gguf and stories15M-q4_0.gguf directly under runtime/. These small story-completion models make it practical to test the process changes on a CPU. Pinned GGUF model files

Configure llama-swap and start the proxy

Save the following as config.yaml in the working directory. The keys story-a and story-b are the names the client sends. Each points to a different file and passes a matching alias to its model server. Leave ${PORT} as written so llama-swap assigns the backend ports. Both the proxy and its backends will listen on loopback.

healthCheckTimeout: 15
startPort: 19481
models:
  story-a:
    cmd: >
      runtime/cpu/llama-server.exe
      --model runtime/stories260K.gguf --alias story-a
      --host 127.0.0.1 --port ${PORT}
      --ctx-size 256 --parallel 1 --threads 2 --n-gpu-layers 0
  story-b:
    cmd: >
      runtime/cpu/llama-server.exe
      --model runtime/stories15M-q4_0.gguf --alias story-b
      --host 127.0.0.1 --port ${PORT}
      --ctx-size 256 --parallel 1 --threads 2 --n-gpu-layers 0

From that directory, run these PowerShell commands. The first checks the configuration syntax. The second starts the proxy and stays in the foreground. A valid configuration alone tells you nothing about whether a model file will load or fit in memory.

.\runtime\v275\llama-swap.exe -config config.yaml -validate
.\runtime\v275\llama-swap.exe -config config.yaml -listen 127.0.0.1:19480

This example uses the default single-model behavior. llama-swap also supports other loading policies. If you already have a configuration with groups or capacity rules, inspect it before assuming that loading B will evict A. Configuration reference for the tested release

Send a request, then check which model is running

Save this client as try_models.py and run python try_models.py in a second terminal. It uses Python’s standard library and the same proxy address for every call. The text-completion endpoint suits these small models. After each response, the client reads /running to check which backend is active.

import json
from urllib.request import Request, urlopen

base = "http://127.0.0.1:19480"
for name in ("story-a", "story-a", "story-b", "story-a"):
    payload = {"model": name, "prompt": "Once upon a time",
               "max_tokens": 8, "temperature": 0, "stream": False}
    request = Request(base + "/v1/completions", data=json.dumps(payload).encode(),
                      headers={"Content-Type": "application/json"})
    with urlopen(request, timeout=45) as response:
        result = json.load(response)
    with urlopen(base + "/running", timeout=5) as response:
        active = [m["model"] for m in json.load(response)["running"]]
    print(name, active, result["usage"]["completion_tokens"],
          repr(result["choices"][0]["text"]))

On our first request, llama-swap started story-a and waited until its health check passed. The next request used the same process. We checked the model-server process ID and its GGUF argument independently of the response text. That matters here because both models continued the prompt with similar words.

  • A, then A again. Both requests returned HTTP 200. The only ready model was story-a, and both used process 47940 with stories260K.gguf.
  • Switch to B. Only story-b appeared as ready. Process 39076 had loaded stories15M-q4_0.gguf.
  • Return to A. Process 11988 loaded A’s original weights and returned a completion. The client had kept the same URL throughout.

Your process IDs will differ. The thing to check is whether consecutive requests reuse the expected process and whether a model change leaves the intended backend ready. Stop the proxy with Ctrl+C when you finish the exercise.

Diagnose a switch that never becomes ready

A backend can start and still be unable to serve a request. By default, llama-swap checks /health and waits for HTTP 200 before forwarding inference traffic. To observe the client’s experience, we used a separate test backend that returned HTTP 503 on every health check. With the example’s 15-second health-check timeout and no progress updates, the completion request returned HTTP 500. The proxy log identified the health-check timeout. The next request for the healthy B model succeeded.

If this happens on your host, check the backend’s own startup log first. A missing model file, an incompatible build and a long load can all leave an application waiting, but they call for different fixes. Confirm the health endpoint and the assigned port. Then measure a successful cold start with your intended model and compare it with the time your client is willing to wait. In v275, a backend that reports changing load progress can restart the health-check timer. The cold-start guide explains how waiting also affects an inference request when capacity has been scaled down.

Choose between switching models and keeping backends ready

Look at a sequence of real requests. Long runs of A interspersed with an occasional B give a single host time to reuse each loaded model. Traffic that alternates A, B, A, B puts startup work in the path of nearly every request. Before buying hardware, record the frequency of switches, cold and warm response times, peak memory with your real context length, and the wait your users can tolerate. Our tests used small CPU models and sequential calls. GPU memory use, simultaneous requests and production response times need their own measurements.

When the models need to stay warm on different machines, the placement decision changes. Hivenet Router gives applications one API for the inference backends you operate and can select among them using configured capacity and policies. The guide to model routing rules and aliases explains how to give clients a stable model name as the backend fleet changes. To connect a model on one of those hosts, follow the guide to serving a GGUF model with llama.cpp and Hivenet Router. The single-host example above runs independently with llama-swap and llama.cpp.

Your next workload belongs on Hivenet.

Pick one AI, compute, or storage workload and see the difference for yourself. Spin it up in minutes, or let our team map your fastest path to production.