Cutting LLM Gateway Overhead: Parse Once, Ask Redis Less
A CPU profile showed our gateway parsing every streamed event twice and sending 128 Redis commands for one small chat request. Here is how we cut gateway CPU per streamed request by 14 to 23 percent, image requests by up to 47 percent, and Redis commands from 128 to 58, without changing a byte of output.
By LLM Gateway

Every request through an LLM gateway pays a tax: authentication, routing, cost tracking, logging, and for streamed responses, work on every single chunk. Next to model inference the tax is small, but it is paid on every request, it decides how many gateway instances you run, and it shows up first under load.
So we profiled LLM Gateway under load and looked for work that did not need to happen. We found two habits that ran on nearly every chat request: parsing streamed events twice, and asking Redis the same questions over and over. This post covers what we changed and what it bought, with the numbers from the pull requests (#5281 and #5282).
Where the time went
Streaming responses arrive as server-sent events: data: lines that each carry a JSON chunk. A chunk can be split across network reads, so the gateway has to find where each event ends before it can use it.
Our framing loop did that by checking whether the buffered text might be complete JSON and then running a validating JSON.parse to be sure. Once it found the boundary, the caller parsed the same text again. Two full parses per event, on every streamed response. The loop also read process.env.NODE_ENV per event, and deep-cloned whole chunks with JSON.parse(JSON.stringify()) when buffering for response healing.
Redis was the second habit. One small chat request sent about 128 Redis commands. Each cached database query, around 35 per request, cost a cache GET, a separate MGET of its tables' last-modified stamps, and a SET NX to throttle a fallback mirror, even on cache hits.
Parse each event once
The fix keeps the framing logic and returns the JSON it already parsed while finding the boundary, so each event is parsed exactly once. For candidates that end at a newline, a full bracket scan became a first-and-last-character check, which accepts exactly the same valid objects and arrays.
Framing code is the kind you only change with an oracle. The test suite keeps the old loop and checks that both find identical boundaries for OpenAI streams, Anthropic event: lines, id: and retry: fields, multi-line data, CRLF separators, missing trailing newlines and events from 150 KB to 2 MB, delivered in one chunk, split at random points, byte by byte, and in 16 KB reads.
Response healing now copies only choices[0].delta, the one part it modifies, instead of cloning the whole chunk.
Ask Redis less
- One round trip per cached read. The cache reads an entry and its table stamps in a single
MGET. Invalidation is unchanged: stamps are still compared on every read. - Skip redundant throttles. The fallback mirror only matters during a Postgres outage, so a process now skips the throttle
SET NXfor a key it checked in the last 5 seconds.
One warm small chat request went from 128 Redis commands to 58: SET from 35 to 1, GET from 41 to 7.
Stop serializing images to throw them away
Image responses carry megabytes of base64, which makes every unnecessary serialization expensive. Three steps did exactly that:
- Logging the response size ran
JSON.stringifyon the whole body when the provider sent noContent-Length. /v1/imagesforwards to chat completions in-process. The chat handler serialized a ~2 MB result, the images handler parsed it back, then serialized it again for its own response.- Collapsing the image stream parsed every event, including a multi-MB partial image it then discarded.
Now the response size comes from the upstream body length, the in-process hand-off passes the result object directly through a slot that only a trusted internal hop can use, and partial image events are recognized by their leading "type" key and skipped without parsing.
The numbers
We load-tested against a local mock upstream, measuring gateway process CPU per request, best of several interleaved runs. The host was shared and noisy, so the absolute numbers are inflated. Compare the columns, not the values.
Chat completions, fixed 120-token completion, concurrency 64:
| Payload | Before | After | Change |
|---|---|---|---|
| Small | 0.71 ms | 0.71 ms | −1% |
| Small, streamed | 2.16 ms | 1.66 ms | −23% |
| Large (77 KB prompt) | 1.75 ms | 1.70 ms | −2% |
| Large, streamed | 3.23 ms | 2.77 ms | −14% |
Images, 1.5 MB PNG from the mock upstream, concurrency 8, both changes combined:
| Request | Before | After | Change |
|---|---|---|---|
/v1/images/generations, OpenAI image | 44.1 ms | 26.7 ms | −39% |
/v1/images/generations, Gemini image | 39.9 ms | 21.1 ms | −47% |
| Chat completions, Gemini image | 24.9 ms | 20.7 ms | −17% |
| Chat completions, Gemini image, streamed | 20.9 ms | 12.7 ms | −39% |
Framing alone, on one 124-event stream, went from 312 µs to 118 µs.
Non-streamed small requests did not move, which is what we expected: they never touched the framing loop, and their Redis savings are network round trips, not CPU. The gains are where the waste was, in streaming and in large payloads.
What did not change
The same requests against the old and new code returned byte-identical output apart from generated ids and timestamps. Logging, billing, tool calls, reasoning, caching and content filtering behave as before. The one visible difference is in the request log: for responses without Content-Length, the recorded response size is now the upstream body length rather than the length of our re-serialized JSON.
None of this changes what a model costs or how fast it generates tokens. It means the gateway does less work per request, so the same instances handle more traffic and the time between a provider's chunk and your client's is a little shorter.
Try it
Frequently asked questions
- How much latency does an LLM gateway add?
- The gateway's own work is small next to model inference, which takes hundreds of milliseconds to minutes. In our load tests against a mock upstream, on a shared and noisy host, LLM Gateway spent under 2 ms of CPU per small streamed chat request after these changes. Network distance to the gateway and the provider usually matters more than processing time.
- What is SSE parsing in an LLM gateway?
- Streaming LLM responses arrive as server-sent events (SSE): lines prefixed with data: that each carry a JSON chunk. The gateway has to find where each event ends, parse it, count tokens and costs, and forward it, for every chunk of every streamed response.
- Did these optimizations change API responses?
- No. The same requests against the old and new code returned byte-identical output apart from generated ids and timestamps. Logging, billing, tool calls, reasoning, caching and content filtering are unchanged.
Models and providers in this post
One API key for every model.
Route to 200+ models with automatic failover, caching, and real-time cost analytics. Free to start — no credit card required.
Not ready to switch yet?
Follow new model releases instead.
A short roundup when new models land. Unsubscribe anytime.