Skip to content

Faster streaming in Kaeso 0.7.1: what we measured and what changed

A streamed request used to cost Kaeso three and a half times the CPU of a plain one. The three causes we found, what 0.7.1 changes, and what it measured with request logs off.

Published
Reading time
4 min
Author
Kaeso Team
A pale gradient of orange and peach, folding diagonally downward, with a warm light in one corner

Most requests to a chat model are streamed: the app shows the reply as it's written, and what people feel is the time to the first token. A gateway sits in the middle of every one of those streams, so whatever it spends on a chunk, it spends hundreds of times per reply.

While profiling Kaeso for 0.7.1, we found that a streamed request cost the gateway about 3.5 times the CPU of the same request without streaming: around 1,040 µs against 300 µs. Kaeso 0.7.1 changes how streams flow through the gateway. Measured with request logs off, it roughly halves the CPU of a streamed request and takes about a third off the time to the first token, without changing what apps receive. Part of that, passing a stream through unchanged, needs content logging off for it, so a default install gets less.

What we measured

The numbers below come from a zero-latency mock provider, so they show what the gateway itself adds: Kaeso pinned to 2 vCPU with two worker threads, PostgreSQL and Valkey, no ClickHouse (so no request logs, and same-format streams took the new pass-through path), 10 clients and 5,000 requests per run, the median of three alternating runs, no failures. A real provider takes hundreds of milliseconds per reply, so in production these milliseconds are a small share of the wait; the CPU per request is what decides how many streams one gateway carries.

  • Chat Completions
    Time to first token, p50
    6.82 → 4.76 ms (−30%)
    Total, p50
    9.08 → 5.44 ms (−40%)
    p99
    18.93 → 13.41 ms
    Requests a second
    1,054 → 1,706 (+62%)
    Gateway CPU per request
    1,040 → 496 µs (−52%)
  • Responses
    Time to first token, p50
    6.94 → 4.61 ms (−34%)
    Total, p50
    9.22 → 5.10 ms (−45%)
    p99
    19.05 → 13.12 ms
    Requests a second
    1,033 → 1,777 (+72%)
    Gateway CPU per request
    1,104 → 422 µs (−62%)
  • Anthropic Messages
    Time to first token, p50
    6.52 → 4.71 ms (−28%)
    Total, p50
    8.22 → 5.35 ms (−35%)
    p99
    17.56 → 12.92 ms
    Requests a second
    1,145 → 1,723 (+50%)
    Gateway CPU per request
    952 → 486 µs (−49%)

Requests without streaming kept a p50 between 2.1 and 2.5 ms in both versions.

Where the time went

One chunk at a time. The HTTP client hands the gateway one chunk at a time, and Kaeso handled them that way all the way through: each chunk took its own trip through a channel to the app's response and its own write to the socket, with a memory timer for every KiB. A short reply meant 33 to 38 writes and about a hundred task switches, and much of the extra CPU was the kernel's.

Decoding what didn't need decoding. When the app and the provider speak the same format, such as an OpenAI Responses app calling an OpenAI model, Kaeso still parsed every event twice, rebuilt it, compared it with the original to keep unknown fields, and wrote the same bytes back out. It also read the first chunks once to look at them and again to process them.

Work no one used. Translating an OpenAI Responses stream into Chat Completions or Anthropic Messages ran the machinery that preserves Responses-only fields, which the other formats then ignore.

What 0.7.1 does instead

  • Streams pass through unchanged when nothing needs to touch them. A Chat Completions, Responses or Anthropic Messages stream that goes to a provider of the same format is forwarded byte for byte when content logging, reply guardrails, reasoning continuity and built-in unmasking are off for it. Kaeso still reads what it needs on the way: the model, the usage, the stop reason and any error, for costs, limits and logs.
  • Ready chunks go out together. The first chunk is written as soon as it's there, so the time to the first token doesn't wait for anything. After that, chunks that are already waiting are written in one go, and the response body no longer passes through an extra task.
  • The first chunks are read once. What Kaeso reads to start a stream is the state it continues with.
  • Translation skips what the target ignores. A Responses stream translated to another format no longer computes the Responses-only details, and the Chat Completions encoder reuses the frame around plain text deltas.

In the codec alone, a recorded Responses stream to a Responses app went from 338 µs to 23 µs, and to a Chat Completions app from 236 µs to 67 µs.

What it means for your install

Nothing to configure. With request content logging on, which is the default, a same-format stream is still decoded so its reply can be logged, and gets the batched writes and the single read; with content logging off, it also passes through unchanged. A stream translated from Responses to another format gets the lighter translation either way. Either way apps receive the same streams as before; the pass-through ones are byte for byte what the provider sent.

These are the gateway's own numbers on a mock. To measure your install against your providers, How to benchmark an AI gateway without fooling yourself has the method, and Kaeso 0.7.1 lists everything else in the release.

By Kaeso Team

On this page

Own the gateway.Own the record.

curl -fsSLhttps://kaeso.app/install.sh | sh