Skip to main content

Request and streaming overhead

This page shows what LocalAI itself costs when it serves a request, apart from the model. The cost is CPU time and memory allocations in the LocalAI process, for each streamed token and for each request. On a large host this cost does not limit throughput. On a small host (a Jetson, an N100 mini PC, a laptop) the same CPU time is taken from the model, so it matters there.

The serving speed of llama.cpp itself (for example paged serving) is on the engine runtime settings page.

What LocalAI does for each request and each token

For each request, before the first token, LocalAI:

  • reads the JSON body and finds the model configuration;
  • applies the defaults of the configuration and checks it;
  • renders the chat template, or converts the messages for the backend when the model uses the tokenizer template of the backend;
  • builds the grammar when the request offers tools and the model uses one;
  • opens a gRPC stream to the backend.

For each token that the backend streams, LocalAI:

  • receives one gRPC message;
  • splits reasoning text from the answer (the backend can also do this, see Two backend modes);
  • when the request offers tools, looks for a tool call in the output;
  • encodes one server-sent event as JSON and writes it to the client.

Results

The numbers below compare LocalAI before and after the streaming changes of November 2026. The backend is the mock backend of the test suite, which streams as fast as LocalAI reads. Everything the process spends is therefore LocalAI’s own work.

Per streamed token, 16 concurrent streams

EndpointReplyCPU per tokenAllocations per token
/v1/chat/completions500 tokens19.8 µs → 11.3 µs24 → 15.5
/v1/chat/completions200 reasoning + 300 answer tokens18.2 µs → 11.8 µs24 → 15.5
/v1/chat/completions with tools300 tokens, then a tool call206 µs → 18.7 µs1004 → 31
/v1/messages500 tokens16.1 µs → 11.1 µs21 → 11
/v1/messages with tools300 tokens, then a tool call2695 µs → 18.5 µs14206 → 23
/v1/responses500 tokens18.9 µs → 12.2 µs22 → 12
/v1/responses with tools300 tokens, then a tool call1255 µs → 20.2 µs6744 → 28

With one stream the cost per token is similar. Before the changes, the cost per token with tools grew with the length of the output. After the changes, a long answer costs about the same per token as a short one.

The largest gap between two chunks (p99) with tools was 5 ms to 12 ms. It is now about 0.15 ms.

Two backend modes

Most llama.cpp models run in one or both of these modes:

  • Autoparser. The backend splits reasoning, answer and tool calls itself and sends them as separate deltas (ChatDeltas). LocalAI does not need to parse the text.
  • Tokenizer template. With use_tokenizer_template: true, the backend renders the chat template. LocalAI sends the messages instead of a prompt.

Per streamed token with the autoparser, 16 concurrent streams, on a model with backend: llama-cpp and use_tokenizer_template: true:

EndpointReplyCPU per tokenAllocations per tokenChunk gap p99
/v1/chat/completions500 tokens23.1 µs → 9.7 µs26 → 17221 µs → 75 µs
/v1/chat/completions200 reasoning + 300 answer tokens21.7 µs → 10.2 µs24 → 17295 µs → 70 µs
/v1/chat/completions with tools300 tokens, then a tool call23.5 µs → 8.9 µs27 → 18167 µs → 62 µs
/v1/messages500 tokens18.7 µs → 11.6 µs24 → 14159 µs → 117 µs
/v1/messages with tools300 tokens, then a tool call2155 µs → 10.9 µs13929 → 15124 ms → 0.1 ms
/v1/responses500 tokens22.2 µs → 14.2 µs24 → 14170 µs → 143 µs
/v1/responses with tools300 tokens, then a tool call896 µs → 12.4 µs6509 → 1569 ms → 0.08 ms

Per request, and time to first byte

One request at a time, with a reply of one token. The CPU time and the allocations are for the whole request.

EndpointCPU per requestAllocations per requestTime to first byte (p50)
/v1/chat/completions1100 µs → 650 µs883 → 4150.85 ms → 0.42 ms
/v1/chat/completions, backend: llama-cpp1850 µs → 650 µs1015 → 4191.69 ms → 0.47 ms
/v1/chat/completions with tools1950 µs → 800 µs2773 → 10461.28 ms → 0.58 ms
/v1/chat/completions, tokenizer template1050 µs → 500 µs870 → 3730.92 ms → 0.37 ms
/v1/chat/completions with tools, tokenizer template1800 µs → 500 µs2299 → 5761.11 ms → 0.39 ms
/v1/messages950 µs → 500 µs871 → 4100.58 ms → 0.21 ms
/v1/responses1200 µs → 750 µs952 → 4460.87 ms → 0.44 ms

With backend: llama-cpp, LocalAI asked the backend for the model metadata on every request of a model without a multimodal projector. It now asks once for each running backend process.

What changed

  • The tool-call parse skips the output while the output cannot hold a call: a call needs a <, { or [. After such a byte, the parse skips prose bytes that cannot start JSON. The parsers of the preset formats are built once.
  • The reply is no longer copied in full for each token. The usage counter keeps only the last event of a stream, not the whole stream.
  • The chat endpoint encodes each event into a reused buffer. It writes the events that are ready together, with one flush. It never waits for an event that is not ready.
  • For each request, LocalAI reuses the GPU probe for 30 seconds, looks up the model instead of listing all models, and compiles the backend name pattern once.

The streams are byte for byte the same as before. The test suite compares them for each endpoint (tests/e2e/stream_golden_test.go).

Measure it yourself

  1. Build LocalAI, the mock backend and the load tool:

    make build
    make build-mock-backend
    go build -o streamload ./scripts/bench/cmd/streamload
  2. Add a model that uses the mock backend. Put an empty file bench.bin in the models directory, and this file as bench.yaml:

    name: bench
    backend: mock-backend
    context_size: 32768
    parameters:
      model: bench.bin
  3. Start LocalAI with the mock backend and the metrics endpoint. Set the same GOMAXPROCS for every run that you compare:

    GOMAXPROCS=4 ./local-ai run --models-path ./models \
      --external-grpc-backends mock-backend:$PWD/tests/e2e/mock-backend/mock-backend
  4. Run the load tool on the same host. It reads the CPU time of the server from /proc, so it runs on Linux only:

    GOMAXPROCS=2 ./streamload -url http://127.0.0.1:8080 -pid $(pgrep -x local-ai) \
      -model bench -endpoint chat -c 16 -r 8 -spec "n=500" -tokens 500

    The tool prints one JSON line: CPU time and allocations per token, the time to first byte, and the gaps between chunks. It reads the allocation counters from /metrics and sends no API key, so run the server without API keys.

The -spec value goes to the mock backend in the prompt, after the MOCK_BENCH marker:

FieldEffect
n=<count>Stream <count> answer tokens.
reasoning=<count>Stream <count> reasoning tokens first.
tool=1End with a tool call, streamed in 4-byte pieces.
deltas=1Send the text as ChatDeltas, as the llama.cpp autoparser does.
msg=1With deltas=1, also put the answer text in the message, as the llama.cpp backend does.
calls=1With deltas=1 and tool=1, send the tool call as tool-call deltas.

Set -tokens to the number of tokens that the backend sends, so that the tool can divide by it. Use -endpoint with chat, chat-tools, anthropic, anthropic-tools, responses or responses-tools.

To test the tokenizer template, add template: {use_tokenizer_template: true} to the model. The mock backend then reads the marker from the last message.

Limits of these numbers

  • The backend is a mock. A real model streams much more slowly, so a real server spends less CPU time on LocalAI for each second. The cost for each token is the same.
  • The host was an x86 machine with other work on it. On another host, and with another load, the numbers change. Compare two builds on the same host, one after the other, with the same GOMAXPROCS.
  • The CPU time has a resolution of 10 ms, so measure at least a few seconds of load.
  • The mock backend sends fixed words. Text with many numbers or quoted strings costs more in the tool-call parse.