# llama.cpp server

> **About this page**
>
> - Task definition version: `swaleio/llamacpp-server@b12855`
> - Summary: Serves a GGUF model through llama.cpp's OpenAI-compatible server as a long-lived runner task.
> - Published: 2026-12-01, the latest version
> - Page: https://hub.swale.io/swaleio/llamacpp-server/v/b12855
> - Latest version's copy: https://hub.swale.io/swaleio/llamacpp-server.md
> - Versions: https://hub.swale.io/swaleio/llamacpp-server/versions
> - Index of Swale Hub: https://hub.swale.io/llms.txt
>
> The YAML block below is the task definition's frontmatter, which declares its inputs, outputs and container, and its readme follows. Both are the text of the account that published this task definition, not Swale's.

```yaml
name: llama.cpp server
description: Serves a GGUF model through llama.cpp's OpenAI-compatible server as a long-lived runner task.
category: machine-learning
inputs:
  model:
    description: Absolute path inside the container of the .gguf model file to serve, e.g. one swaleio/hf-download or swaleio/llamacpp-quantize placed on the mounted workspace.
    required: true
  port:
    description: TCP port the server listens on. Defaults to 9931, llama-server's own default in this build; pass a different value to serve elsewhere, and point every consumer URL at the same port.
    default: "9931"
  ctx_size:
    description: Context size in tokens, shared by the server's parallel slots. Defaults to 0, which takes the model's own training context, as llama-server does; set a smaller value to save memory on long-context models.
    default: "0"
exec:
  # ghcr.io/ggml-org/llama.cpp:full-cuda13-b12855
  image: ghcr.io/ggml-org/llama.cpp@sha256:07798987d96bcdc082656662aa6603b76498f5816ae0cf68290658bc11d416e3
  args:
    - "--server"
    - "--model"
    - ${{inputs.model}}
    - "--host"
    - "0.0.0.0"
    - "--port"
    - ${{inputs.port}}
    - "--ctx-size"
    - ${{inputs.ctx_size}}
```

# llama.cpp server

Starts `llama-server` from [llama.cpp](https://github.com/ggml-org/llama.cpp)
for a GGUF model. It answers OpenAI-compatible requests
(`/v1/chat/completions`, `/v1/completions`, `/v1/embeddings`, `/v1/models`),
llama.cpp's own `/completion` and `/tokenize`, and `GET /health`, which returns
`200` once the model is loaded.

The image is llama.cpp's CUDA 13 build, used as-is: its entrypoint is
llama.cpp's `/app/tools.sh` dispatcher, and the leading `--server` argument runs
`llama-server` with the rest. llama-server listens only on `127.0.0.1` unless
told otherwise, so the definition always passes `--host 0.0.0.0`, which makes it
reachable by the run's other tasks.

This is a **runner** task: it serves requests indefinitely and never exits on
its own. Its lifetime is bounded by its own `terminate_on` list — the platform
terminates the runner when the tasks listed there complete. A workflow that
starts this task without wiring `terminate_on` never finishes.

## Inputs

| Input | Required | Default | Description |
|-------|----------|---------|-------------|
| `model` | yes | — | Absolute path of the `.gguf` model file, such as one `swaleio/hf-download` or `swaleio/llamacpp-quantize` placed on the workspace. |
| `port` | no | `9931` | Port the server listens on, llama-server's own default in this build. Consumers combine it with the runner's ip-address, so every consumer URL must use the same port. |
| `ctx_size` | no | `0` | Context size in tokens; `0`, llama-server's default, takes the model's training context. A smaller value saves memory on long-context models. |

## Outputs

None. Runner tasks are long-lived pods terminated via `terminate_on` and get no
output-tracking sidecar, so this task declares no outputs. Consumers read the
server's responses over HTTP instead.

## Compute

**GPU recommended.** The image loads its CUDA backend and offloads as many
layers as fit when the compute type has a GPU, and falls back to its CPU
backend otherwise. A quantized model of a few billion parameters answers at a
usable speed on a CPU compute type; larger models want a GPU.

## Runner lifecycle and consumers

- **Termination**: the runner's own `terminate_on` lists its consumers. When
  every task in that list completes, the platform terminates the runner.
- **Address**: consumers reach the server at `${{tasks.<id>.ip-address}}` on
  the server's port — `9931` unless the step passed `port`.
- **Ordering**: every consumer MUST list the runner in its own `start_on` —
  the address only exists once the runner task has started.
- **Readiness**: the address is available at pod start, before the model is
  loaded. Poll `GET /health` until it returns `200`, with `swaleio/http-request`
  and `retry: "360"`, `retry_delay: "5"`: a check every 5 seconds for up to 30
  minutes.

## Example

Fetch a 4-bit GGUF model, serve it, wait for it to load, and ask one question:

```yaml
name: llama.cpp server example
compute_type: cpu-4
entry_point: main

blocks:
  main:
    tasks:
      model:
        name: Fetch model
        uses: swaleio/hf-download
        args:
          repo: Qwen/Qwen2.5-0.5B-Instruct-GGUF
          include: "qwen2.5-0.5b-instruct-q4_k_m.gguf"
          dest: ${{env.WORKFLOW_STORAGE}}/gguf

      serve:
        name: Serve model
        uses: swaleio/llamacpp-server
        start_on: [model]
        terminate_on: [ask]
        args:
          model: ${{env.WORKFLOW_STORAGE}}/gguf/qwen2.5-0.5b-instruct-q4_k_m.gguf

      wait_ready:
        name: Wait for server
        uses: swaleio/http-request
        start_on: [serve]
        args:
          url: http://${{tasks.serve.ip-address}}:9931/health
          retry: "360"
          retry_delay: "5"
          expected_status: "200"

      ask:
        name: Chat completion
        uses: swaleio/http-request
        start_on: [wait_ready]
        args:
          url: http://${{tasks.serve.ip-address}}:9931/v1/chat/completions
          method: POST
          headers: |
            Content-Type: application/json
          body: '{"messages":[{"role":"user","content":"Say hello in one sentence."}],"max_tokens":64}'
          expected_status: "200"
          response_path: ${{env.WORKFLOW_STORAGE}}/completion.json
```

The model is Apache-2.0. Later tasks read the answer at
`${{env.WORKFLOW_STORAGE}}/completion.json`.

---

For the workflow syntax these examples use, see the
[workflow definition reference](https://docs.swale.io/reference/workflow-definition-syntax)
in the [Swale documentation](https://docs.swale.io).

Licensed under the [MIT License](https://github.com/swaleio/task-definitions/blob/main/LICENSE).
