How much latency does a model gateway add to each call, and how do you measure it separately from model time?
answer
- small hop, large model time
- sequential calls add overheads
- one host's monotonic clock
- time before upstream plus time after
- p99 per route, not mean
basics
~20 sA model gateway usually adds a few milliseconds per call: the extra hop, key and cap checks, and translation. Measure it inside the gateway as time before the upstream call plus time after it, per route, at p50 and p99.
solid answer
~40 sInside one region the hop is usually a few milliseconds: the extra network leg, checking the caller's gateway key, a spend-cap check, and translating the request and response. Against model time of hundreds of milliseconds to tens of seconds that is small, but it adds up in agent loops of many sequential calls and it shows in time to first token on short interactive calls. I would measure it inside the gateway: stamp `received`, `upstream_sent`, `upstream_done` and `client_done` on one host's monotonic clock and report `(upstream_sent - received) + (client_done - upstream_done)` per route at p50 and p99. A client-side timer or a synthetic probe covers the network leg the gateway cannot see, and the p99 is the number to watch, because queueing on a saturated replica shows there first.
code
json · 11 lines{
"route": "support-summary",
"provider": "provider-a",
"t_received_ms": 0.0,
"t_upstream_sent_ms": 3.1,
"t_upstream_done_ms": 1184.6,
"t_client_done_ms": 1185.4,
"stages_ms": { "key_check": 0.4, "cap_check": 1.9, "translate": 0.6 },
"overhead_ms": 3.9,
"upstream_ms": 1181.5
}go deeper
Recall that the gateway sits in front of every model call, so whatever it does per call is added latency, and that it is usually small next to model time.
Explain where the milliseconds come from (network leg, handshakes, key and cap checks, translation, queueing) and how timestamps on one gateway host split overhead from upstream time.
Show you would run overhead as its own per-route distribution, catch cold-replica spikes after deploys, and diagnose overhead that scales with response size as per-chunk work in the path.
Frame overhead as the platform team's own service objective, separate from provider latency, and weigh it against what the gateway buys for latency-sensitive features such as agent loops.
## What the gateway hop adds A **model gateway** is one internal endpoint that every application calls instead of calling each model provider directly. Because it sits in front of every model call, everything it does per call is added to that call's latency. Its own cost, the **overhead**, comes from a handful of places: - **The extra network leg.** The client now talks to the gateway, and the gateway talks to the provider. Inside one region that leg is usually a millisecond or two; across regions or the public internet it can be tens of milliseconds. - **Connection setup.** If the gateway opened a fresh encrypted connection to the provider for every call, each handshake would cost extra round trips. A pool of kept-alive upstream connections removes that, and can make a call through the gateway faster than one from a client that reconnects every time. - **Per-call work.** Checking the caller's gateway key, resolving the route and model alias, checking a spend cap, and translating the request and the response between the gateway's shape and the provider's. - **Remote lookups in the path.** Any check that calls another service, such as a shared spend counter, adds that service's latency and its tail. How that counter store works is covered with distributed rate limiting. - **Queueing.** A saturated replica (CPU, connection limits, runtime pauses) holds a request before doing any work. This is usually where the p99 comes from. ## Why it is small and still matters Model time dwarfs a few milliseconds on most calls, but not everywhere. In this leaf's illustrative scenario, the gateway's own overhead is 4 ms at p50 and 30 ms at p99: | call | model time (scenario) | does the overhead show? | |---|---|---| | long generation | 10-30 s | no | | short classification | 300 ms | barely, except at p99 | | time to first token in a chat | 400 ms | yes, users feel the tail | | agent task, 20 sequential calls | several seconds | yes, 20 x 4 ms = 80 ms per task at p50 | Sequential calls add their overheads; parallel calls do not. The tail matters more than the median, because the slowest calls belong to real users and the overhead's tail is the only part the platform team controls. How streamed tokens are buffered and forwarded, and what that does to perceived latency, is a separate subject from this per-call overhead. ## How to measure it Measure inside the gateway, on each call, rather than comparing end-to-end latency before and after it was introduced; provider latency varies by far more than the overhead, so a before-and-after comparison mostly measures the provider. 1. Stamp timestamps on **one host's monotonic clock**: `received`, `upstream_sent`, `upstream_done` and `client_done`. 2. Compute overhead as `(upstream_sent - received) + (client_done - upstream_done)`, and upstream time as `upstream_done - upstream_sent`. 3. Do not subtract a timestamp taken on another machine; clocks drift between hosts, and the result can even go negative. 4. Report **distributions** (p50, p95, p99) per route and per provider, never just a mean, which hides the tail. 5. Break the pre-upstream part into stages (key check, cap check, translation) so a regression points at its stage. 6. Cover the leg the gateway cannot see: client-side timing, or a synthetic probe that sends the same small request directly and through the gateway and compares the two. ## Reading the numbers - **Flat overhead as model time varies** is healthy: fixed per-call work does not depend on the answer's length. - **Overhead that grows with response size** means per-byte work in the path: re-serialising chunks, translating every stream event, or writing the body somewhere synchronously. - **Spikes right after a deploy** usually mean cold replicas: empty upstream connection pools and unloaded config. - **A rising p99 with a flat p50** points at queueing on saturated replicas, not at any single stage. ## Give the overhead a budget Once it is measured, the overhead becomes something the platform team can promise and defend: - **Set a per-route budget** for overhead p99, tighter for interactive routes than for batch-style ones. - **Gate releases on it.** A canary whose overhead p99 regresses past the budget rolls back, like one whose error rate rises. - **Charge new in-path features against it.** A new check in the path (another lookup, a heavier translation) must fit the budget or move out of the path. - **Show callers the split.** A dashboard of overhead next to upstream time ends the recurring argument about whether the gateway is what made a feature slow. Many platform teams publish the overhead p99 as their own service objective, separate from provider latency, because it is the part of every model call they can actually change.
- Overhead p99 jumps after every gateway deploy and settles after a few minutes; what is likely happening?New replicas start cold: empty upstream connection pools, so the first calls pay fresh handshakes, and config such as routes and key mappings may still be loading. Warm the pools and load config before a replica reports ready, and shift traffic onto new replicas gradually so none takes full load cold.
- Why not just compare end-to-end latency before and after introducing the gateway?Provider latency varies by hundreds of milliseconds between calls and across the day, which swamps a few milliseconds of overhead, so a before-and-after comparison mostly measures the provider. Splitting gateway time from upstream time on every call isolates the overhead no matter what the model did.
- The gateway's overhead grows with the length of the response; where do you look?Fixed work such as key and cap checks does not scale with size, so look for per-byte or per-chunk work in the path: re-serialising or translating each stream event, or writing the body somewhere synchronously. Whether request logs should be written in the path at all is its own design question; here, time each per-chunk stage.
- The gateway adds nothing worth measuring because model time dominates every call.
- Overhead is simply end-to-end latency with the gateway minus latency without it.
- Subtracting the client's send time from the gateway's receive time measures the network leg.
- Mean overhead is the right number to report and alert on.
- A gateway hop always makes a call slower than calling the provider directly.