How performance is measured
Last updated: 2026-11-27T14:42:00-03:00 (UTC-3)
How much the gateway adds to each call, and what it costs in CPU and memory, compared with calling the provider directly. The measurements use gwbench, a tool in this repository that anyone can run.
The current numbers are in Performance. This page explains the method and shows how to reproduce the results. Comparisons with other gateways are a separate subject, under Benchmarks.
Methodology (2026-10-26)
Section titled “Methodology (2026-10-26)”- Bounded CPU: the gateway runs in a container with
--cpus 2and respects the cgroup CPU quota. - Simulated upstream: a simulated provider (
gwbench -mock) in the same network namespace, reached over loopback. The client reaches the gateway and the direct baseline through the same host port forwarding. - Authenticated: every call carries a gateway key.
- Two upstream profiles: instant, which isolates the cost of the gateway, and realistic, with a 300 ms TTFT and 16 tokens every 20 ms (~640 ms per response). The realistic profile is selected by model name (
m@300ms,20ms, orm--300ms--20mswhere a model path segment refuses@and,). - Two modes: minimal, with no database; and production, with per-request persistence in PostgreSQL. The minimal mode was measured before T087 made PostgreSQL mandatory; the binary no longer starts that way, so those numbers cannot be reproduced as they are.
- Cost beyond latency: CPU per request and peak memory come from the Docker API for the gateway’s container. Warmup is excluded.
- Alternating rounds, with the order of targets rotated between rounds.
- Version: the gateway at the commit given in Performance.
Environment limit: at concurrency ≥128 with an instant upstream, the direct target itself has a p99 of ~200 ms. That is the connection queue of the host port forwarding (OrbStack), not of the gateway. In these cases, compare p50, CPU and memory, not p99 or absolute req/s.
Production mode: the price of durability
Section titled “Production mode: the price of durability”In production mode the gateway records durable_usage (the receipt, written before inference), usage_analytics, request_logs and request_timing, with the default pool of 10 connections.
The price of durability is visible, and it is a design choice. The durable receipt is written synchronously before the provider is called, so that a crash never loses billing. That costs ~3.5 ms per request and ~0.5 ms of PostgreSQL CPU per request (6 s of database CPU for ~12 thousand requests). With a realistic upstream, the 3.5 ms disappears inside the provider’s ~640 ms. The limit is the pool of 10 connections: around 2.2 thousand req/s per instance with an instant upstream. At 8a552359 (2026-11-14) a per-request CPU regression in usage valuation lowered that to ~750 req/s on 2 CPUs before the pool was reached; 614a7ff9 fixed it and is back at the baseline’s ~2,000–2,200 req/s; see the integrated A/B.
What the measurement fixed
Section titled “What the measurement fixed”- 502 on a reused connection (17.5% with llama.cpp): sends to the upstream are now retryable in the safe cases.
- Fixed concurrency ceilings: 32 for chat and 4 for the other operations, including
/v1/messagesand/v1/responses. At concurrency 128, the gateway rejected 46.8% of requests with idle CPU. Admission is now configurable (see network and timeouts): 512 slots, 256 MiB of bodies and a 2 s wait. - HTTP/2 disabled toward providers, and a narrow pool: the transport ran on HTTP/1.1 without warning.
max_connections_per_host: 100capped the gateway at ~147 req/s with a realistic upstream, whatever the concurrency, and at 512 the p50 went to 3.3 s. After the fix: 727 req/s and a p50 of 639 ms. - Per-request CPU and the garbage collector (2026-10-31). A CPU, allocation, mutex and block profile of the chat route under load showed that a fresh 32 KiB copy buffer per answer was two thirds of all allocated bytes, took the runtime’s global heap lock, and made the collector run every ~400 requests; the collector, not the request work, set the tail. The fixes, one at a time: pooled copy buffers; no work for features a request does not use (latency routing off, no semantic cache session, no sampled capture); and collector pacing (see architecture). Measured with the per-request harness below.
Per-request harness (2026-10-31)
Section titled “Per-request harness (2026-10-31)”A Linux container on the development Mac (Apple M4 Pro, 12 vCPUs): the gateway pinned to 4 cores with GOMAXPROCS=4, a closed-loop HTTP/1.1 keep-alive load generator on 4 other cores, and an instant simulated provider on the last 4, all over loopback. The configuration is the minimal one: one static key (every call authenticated), info logs to /dev/null, no database. The request is a ~90 B Chat Completions body and the answer ~235 B. Added latency is the c=1 percentile through the gateway minus the same percentile straight to the provider (6 s windows each); CPU per request is the process’s user+system time over a 6 s window at c=8, divided by the answers; throughput under a p99 bound is the best 6 s window, over concurrencies 1 to 64, whose p99 stayed under the bound with no errors. Rounds alternate the builds being compared. Numbers from different sessions are not comparable with each other: the Mac’s own load moves them by up to ~20%.
| Same protocol (Chat to an OpenAI target) | added p50 | added p99 | CPU µs/req | req/s at p99 < 10 ms | req/s at p99 < 1 ms | peak RSS |
|---|---|---|---|---|---|---|
| before (one buffer per answer) | 69 µs | 1,498 µs | 53.9 | 32,311 | none | 42 MB |
| pooled buffers | 67 µs | 444 µs | 47.3 | 62,450 | 15,592 | 42 MB |
| pooled buffers + skipped feature work | 69 µs | 108 µs | 47.2 | 65,868 | 19,294 | 40 MB |
+ collector pacing (GOGC 200) |
69 µs | 91 µs | 44.1 | 84,873 | 77,783 | 53 MB |
Each row is the mean of three rounds against the row above it, in the same session. On a Chat to Messages translation, collector pacing gave the same shape: CPU per request −5%, throughput under a 10 ms bound +31%, under a 1 ms bound 2.3×. With GOGC=400 the same-protocol throughput under 10 ms rose a further ~10% for ~25 MB more peak memory; that trade is left to the operator.
Integrated A/B against the 2026-10-26 baseline (T044)
Section titled “Integrated A/B against the 2026-10-26 baseline (T044)”Measured on 2026-11-14 to answer two questions before any cloud run: did anything integrated since the last recorded baseline regress the hot path, and what must a later optimization of the protocol translator beat. Two sessions, each against the 2026-10-26 baseline (54414f9) running side by side: the first with the integration tip 8a552359, which found the regression described below; the second with 614a7ff9, which fixes it. The last repetition of the second session is recorded in Performance.
Environment. Mac Mini M4 Pro (12 cores, 24 GB), macOS 26.6.2, OrbStack 2.2.5 (Docker 29.8.2, Linux VM with 12 CPUs and 11.7 GiB). Each gateway in its own container with --cpus 2 and no memory limit; the simulated provider (gwbench -mock, built from the tip), PostgreSQL 16.15 (one server, one database per gateway, default settings, no CPU limit) and the gateways share one network namespace and talk over loopback; the client (gwbench, darwin/arm64, Go 1.27.3) reaches every target through the same host port forwarding. No other benchmark or build ran during the rounds.
Builds. Each commit built from its own clean source with its own Dockerfile: 54414f9 with Go 1.25.14 (its floating golang:1.25-alpine tag), image sha256:753e297b9b153f8f28b6b8fd5c0e1451f1796eb3108d1db7b6211ff6a49f2867; 8a552359 with Go 1.27.3 (pinned), image sha256:84f05d3e5defe5b233f4a77406f1bd0460448a5b426f5260b87ba37101293161, reporting revision 8a552359b775c1e3dde76cc8711c85df56e598bb, unmodified. Second session: 54414f9 rebuilt the same way (Go 1.25.14, image sha256:ac6d9b71a586c290081bff401d9eaf2a48e3469342440ff7294a7420ab9b9807) and 614a7ff9 with Go 1.27.3, image sha256:6ac36c6a22a9887912ef63f58c43d09cc7a82dd385eb62176fb7c0642c733688, reporting revision 614a7ff9bf1980765229658b7451ac8ba25a0e55, unmodified. The images were local and removed after each session.
Configuration. Both commits ran the same YAML: the shipped configuration of 54414f9 (every feature it enables, PostgreSQL with the default pool of 10 connections), with request_timing on, OTLP logs and traces off (no collector existed, and exporting to a dead endpoint would measure its retries), one authenticated key and two aliases on one OpenAI-protocol provider pointing at the mock (m for the instant profile, m@300ms,20ms for the realistic one). One line was removed from the baseline’s YAML because the tip refuses it: the community price-source flag under the name it had before it became price_source_cost_map; it was off, and stays off on both. A third target, shipped defaults, ran the tip with the tip’s own shipped configuration and the same edits, which adds the features enabled by default since then.
Rounds. Seven repetitions in the first session and four in the second; in each, the target order rotates, and gwbench rotates it again between its own rounds. Instant upstream without streaming: concurrency 1 (3,000 requests per target in 3 rounds) and concurrency 32 and 128 (16,000 in 4 rounds); realistic upstream with streaming: concurrency 32 (1,600 in 4 rounds); 50 warmup calls per target. Added latency is the target’s percentile minus direct’s in the same run. Each cell is the median of the session’s repetitions with their minimum and maximum; Δ is computed per repetition against the baseline of the same session and then summarized the same way, so a Δ whose range crosses zero is noise.
After the fix (614a7ff9, second session, four repetitions): the current numbers.
| Point | Build | added p50 (ms) | added p90 (ms) | added p99 (ms) | req/s | CPU ms/req | peak memory (MB) |
|---|---|---|---|---|---|---|---|
| instant, c=1 | 54414f9 |
2.22 [2.12–2.27] | 2.63 [2.46–2.81] | 6.07 [5.43–6.59] | 432 [413–451] | 0.27 [0.26–0.27] | 22 [14–30] |
614a7ff9 |
2.21 [2.17–2.37] | 2.83 [2.54–3.54] | 4.98 [4.05–5.79] | 411 [362–452] | 0.26 [0.24–0.28] | 55 [40–61] | |
| Δ | +0.04 [−0.06–+0.09] | +0.26 [−0.27–+0.96] | −1.08 [−2.54–+0.34] | −35 [−62–+39] | −0.01 [−0.03–+0.01] | +29 [+25–+39] | |
| instant, c=32 | 54414f9 |
13.22 [12.17–13.62] | 14.11 [11.87–14.74] | 16.00 [15.96–273.15] | 2,220 [1,403–2,386] | 0.29 [0.28–0.32] | 25 [20–30] |
614a7ff9 |
13.46 [12.50–13.72] | 14.45 [13.96–28.89] | 35.85 [13.92–53.03] | 2,048 [1,685–2,200] | 0.26 [0.25–0.28] | 56 [52–63] | |
| Δ | +0.19 [+0.04–+0.49] | +0.30 [−0.72–+17.03] | +2.24 [−220.12–+33.19] | −81 [−701–+615] | −0.03 [−0.06–−0.01] | +34 [+22–+37] | |
| instant, c=128 | 54414f9 |
56.44 [52.68–58.50] | 74.32 [60.72–105.17] | 184.94 [−77.44–386.49] | 1,815 [1,174–2,103] | 0.31 [0.30–0.35] | 36 [34–41] |
614a7ff9 |
56.38 [53.73–57.38] | 69.97 [62.22–178.52] | 380.44 [−105.81–1,045.54] | 1,491 [1,190–2,158] | 0.28 [0.26–0.30] | 68 [61–76] | |
| Δ | −0.06 [−1.12–+1.05] | −4.36 [−42.94–+117.80] | +125.75 [−352.82–+1,122.97] | −325 [−912–+984] | −0.03 [−0.09–0.00] | +32 [+20–+40] | |
| realistic, stream, c=32 | 54414f9 |
23.29 [21.58–30.20] | 33.89 [32.26–35.33] | −123.57 [−131.45–−114.70] | 44 [44–44] | 2.54 [2.53–2.59] | 31 [30–32] |
614a7ff9 |
29.69 [20.99–32.77] | 32.06 [29.03–36.69] | −126.78 [−131.06–−117.23] | 44 [44–44] | 3.27 [3.17–3.27] | 65 [64–65] | |
| Δ | +4.32 [−0.59–+6.73] | −0.65 [−5.59–+1.36] | −1.86 [−5.71–+0.87] | 0 | +0.70 [+0.64–+0.74] | +33 [+32–+36] |
- The instant-upstream regression is gone: latency, throughput and CPU per request match the baseline within the spread at every concurrency; CPU per request is even slightly lower at c=32 (−0.03 ms, every repetition below zero).
- One residual remains, in streaming CPU: +0.70 ms per streamed response (~28%), every repetition above zero; latency and TTFT are unaffected (TTFT p50: direct 310.1 ms,
54414f9322.5 ms,614a7ff9321.4 ms). Its cause is below; T077.002 removed most of it. - Peak memory is ~30 MB higher on every point, in both sessions. Part of it is the deliberate collector pacing (
GOGC200, 2026-10-31, see the per-request harness above); the rest was ~6 MB of start-up state multiplied by that pacing, explained and cut in T159. The containers had no memory limit. - Shipped defaults (
614a7ff9with its own defaults) tracked614a7ff9within the spread (c=32: added p50 13.32 ms, 1,780 req/s, 0.27 CPU ms/req). - Errors: none at c=1, in the stream, or for
614a7ff9at c=32. At c=128, HTTP 503durable_usage_unavailable:54414f92,323 of 64,000, all in one burst of one repetition;614a7ff971 of 64,000; shipped defaults 18 of 64,000 at c=32. As in the first session, they come in bursts during host stalls and do not separate the builds. - The ~25 ms streaming latency advantage of
8a552359in the first session did not reproduce: in the second session614a7ff9matches the baseline. It is treated as an effect of that session, not of the code.
Before the fix (8a552359, first session, seven repetitions): kept as history.
| Point | Build | added p50 (ms) | added p90 (ms) | added p99 (ms) | req/s | CPU ms/req | peak memory (MB) |
|---|---|---|---|---|---|---|---|
| instant, c=1 | 54414f9 |
2.22 [2.12–2.83] | 2.54 [2.30–5.05] | 6.55 [5.95–7.93] | 424 [304–463] | 0.27 [0.26–0.34] | 19 [15–26] |
8a552359 |
3.05 [3.01–3.19] | 4.78 [4.34–5.55] | 7.88 [7.74–8.34] | 278 [258–286] | 1.40 [1.33–1.42] | 58 [44–61] | |
| Δ | +0.89 [+0.22–+1.07] | +2.31 [−0.33–+3.04] | +1.49 [+0.05–+2.32] | −153 [−198–−30] | +1.13 [+1.03–+1.16] | +34 [+29–+41] | |
| instant, c=32 | 54414f9 |
13.46 [11.98–14.10] | 14.07 [11.37–32.33] | 17.16 [16.08–307.40] | 2,195 [1,160–2,440] | 0.29 [0.28–0.33] | 25 [19–27] |
8a552359 |
35.93 [34.13–37.61] | 43.38 [37.94–54.81] | 243.78 [42.08–391.32] | 753 [592–904] | 1.66 [1.59–1.69] | 82 [74–90] | |
| Δ | +22.47 [+22.15–+23.74] | +26.92 [+11.05–+40.75] | +29.20 [−63.63–+374.16] | −1,333 [−1,599–−407] | +1.35 [+1.30–+1.40] | +56 [+52–+64] | |
| instant, c=128 | 54414f9 |
55.22 [51.18–59.03] | 72.88 [56.74–225.33] | 730.88 [16.91–1,562.34] | 1,341 [940–2,131] | 0.33 [0.31–0.36] | 35 [31–37] |
8a552359 |
146.37 [141.72–148.70] | 163.95 [151.67–241.44] | 319.27 [10.85–1,530.90] | 775 [622–868] | 1.67 [1.64–1.72] | 105 [100–113] | |
| Δ | +90.55 [+88.88–+91.95] | +94.93 [−39.77–+156.57] | −453.71 [−1,206.09–+1,513.99] | −566 [−1,475–−201] | +1.34 [+1.29–+1.42] | +70 [+68–+76] | |
| realistic, stream, c=32 | 54414f9 |
23.89 [16.65–27.72] | 32.33 [28.60–32.62] | −114.76 [−132.69–+36.74] | 44 [44–45] | 2.48 [2.40–2.53] | 26 [23–28] |
8a552359 |
−0.21 [−12.08–+5.32] | 1.62 [−3.88–+15.40] | 17.95 [−155.91–+183.50] | 46 [45–46] | 5.25 [5.17–5.39] | 82 [78–90] | |
| Δ | −25.34 [−28.74–−19.47] | −29.73 [−36.32–−14.08] | −18.79 [−46.76–+298.26] | +1 [+1–+2] | +2.80 [+2.69–+2.87] | +56 [+52–+67] |
- Shipped defaults tracked the tip within the spread on every point (c=32: added p50 37.49 ms, 734 req/s, 1.73 CPU ms/req): the features enabled by default since the baseline add nothing measurable here.
- TTFT with the realistic upstream: direct 310.1 ms,
54414f9321.8 ms [321.2–322.3],8a552359328.4 ms [321.2–330.4]. The tip is ~6.5 ms later at the median, but its best repetition equals the baseline; it is not called a regression from this run. - Errors: none at c=1 or in the realistic stream. With the instant upstream, both builds returned a few HTTP 503
durable_usage_unavailable(the receipt is fail-closed when the pool of 10 connections does not answer withinacquire_timeout):54414f914 and 69 per 112,000 requests at c=32 and c=128,8a55235962 and 80. They came in bursts during host stalls, in both builds; the rate (≤0.07%) does not separate them. - p99 at c=128 is not comparable: direct itself went from 2,430 to 17,250 req/s between repetitions, the host port-forwarding queue described in the environment limit above.
- Not comparable with the 2026-10-26 production row: that ran on Docker Desktop without
request_timingand with a database of its own; only numbers from the same session are compared here.
Regression found and fixed: valuation price-source lookup on every request
Section titled “Regression found and fixed: valuation price-source lookup on every request”At 8a552359, with the instant upstream the tip spends ~5× the CPU per request (0.27 → 1.40 ms at c=1), so under load the 2-CPU container saturates at ~750 req/s instead of ~2,200, and the queue shows up as added latency (+22 ms p50 at c=32, +91 ms at c=128). With the realistic upstream the gateway is not CPU-bound and latency is unaffected (it was ~25 ms better at p50 in that session, which did not reproduce after the fix), but CPU per request still doubles.
A CPU profile of each build under the same c=32 load (net/http/pprof in a scratch build, never committed) shows where:
54414f9 |
8a552359 |
|
|---|---|---|
connectionPreset (valuation price sources), share of gateway CPU |
12% | 65% |
of which compiling the template regular expressions (templateBase) |
— | 55% |
of which copying and sorting the preset catalog (config.ProviderPresets) |
6% | 9% |
Usage valuation (usage_valuation, on in both shipped configurations) resolves the connection’s price sources more than once per request, and each connectionPreset call:
- compiles two regular expressions per template preset of the protocol when the base URL is not an exact preset URL, which is the case for any self-hosted, local or proxied OpenAI-compatible endpoint, the mock included. Introduced by
127fa907(2026-10-31, “price template connections”), which matches regional connections written withoutpreset:. - copies and sorts the whole preset catalog, twice; the catalog grew from 18 to 98 entries with the generated presets of
8d750217(2026-10-26). This part also reaches connections on an official provider URL.
A Go benchmark of connectionPreset alone (Apple M4 Pro): for a custom base URL, 10.1 µs and 53 allocations at 54414f9 against 287 µs, 9,568 allocations and ~1 MB at 8a552359; for https://api.openai.com/v1, 9.2 µs against 18.3 µs. A diagnostic build of the tip that only compiles each template’s expression once (scratch, not committed) confirmed the attribution in three more alternating repetitions:
| c=32, instant | 54414f9 |
8a552359 |
8a552359 + cached template expression |
|---|---|---|---|
| CPU ms/req | 0.29 | 1.66 | 0.41 |
| req/s | 2,140 | 714 | 1,360 |
| added p50 | 13.4 ms | 38.7 ms | 19.1 ms |
The remaining +0.11 ms per request was mostly the catalog copy and sort (25% of the diagnostic build’s CPU).
The fix, 614a7ff9: the sorted preset catalog is computed once and iterated without copying (config.RangeProviderPresets), templateBase and templateHost cache their compiled expressions per template, and connectionPreset parses the connection URL once. The connectionPreset benchmark for a custom base URL went from 287 µs and 9,568 allocations to 0.99 µs, 1 allocation and 144 B. End to end, before and after, each against the baseline of its own session:
Δ against 54414f9, instant |
8a552359 (before) |
614a7ff9 (after) |
|---|---|---|
| CPU ms/req, c=1 | +1.13 [+1.03–+1.16] | −0.01 [−0.03–+0.01] |
| CPU ms/req, c=32 | +1.35 [+1.30–+1.40] | −0.03 [−0.06–−0.01] |
| req/s, c=32 | −1,333 [−1,599–−407] | −81 [−701–+615] |
| added p50, c=32 | +22.47 ms [+22.15–+23.74] | +0.19 ms [+0.04–+0.49] |
| added p50, c=128 | +90.55 ms [+88.88–+91.95] | −0.06 ms [−1.12–+1.05] |
| CPU ms/req, realistic stream c=32 | +2.80 [+2.69–+2.87] | +0.70 [+0.64–+0.74] |
Residual after the fix: stream valuation decoded every event (T077.002)
Section titled “Residual after the fix: stream valuation decoded every event (T077.002)”The streaming CPU left above the baseline (+0.70 ms per response) was profiled the same way, under the realistic stream at c=32 for 20 s, about 920 responses per build. The whole difference sits in the stream usage observer: streamUsageObserver.write takes 0.27 s at 54414f9 and 0.98 s at 614a7ff9, and 0.62 s of it is valueEvent, 82% of that in encoding/json. For usage valuation, valueEvent decodes every SSE event of the answer into a generic any tree (UseNumber) to check the reported model, cache counters and the response profile: about 0.67 ms per 18-event answer. It came with the quota/valuation profiles of ed0c8561 (T004.001, 2026-11-01) and was extended by 4e04735e (T110, 2026-11-08). Response flushing and the rest of the relay cost the same in both builds. It does not show in latency at this load, only in CPU, and so in streams per CPU.
The fix (T077.002): most events of an answer are text deltas that carry no usage, no tier and no billed category. The observer now proves that from the event’s bytes, without building the tree: the event must be one valid JSON object whose every key, at every depth, is a name the walker only passes through (id, choices, delta, content, text, index and similar), plus the few it checks by value (type, role, model, service_tier, usage: null, reasoning text), compared byte for byte. Anything else, including an escaped, non-ASCII or differently cased key, a repeated key that fails on any occurrence, an event that carries usage, message or response, or one near the walker’s depth or node limits, goes through the full decode exactly as before. A Gemini chunk’s running usageMetadata is decoded alone and recorded the same way. A characterization recorded on f3262c14 before the change (stream_valuation_golden: 133 streams across the four dialects, malformed, repeated, missing and tier-changing usage, cache counters, framing and chunked reads, under six valuation settings) passes unchanged, and a differential test and a fuzz target check that whenever the byte check accepts an event the full decode would have left the same state.
Observer alone, one 18-event answer (go test -bench BenchmarkStreamUsageObserver, Apple M4 Pro, median of six):
| Dialect, valued | before | after | allocations | bytes |
|---|---|---|---|---|
| Chat Completions | 77.0 µs | 41.4 µs (−46%) | 1,169 → 456 | 74,548 → 27,460 |
| Responses | 85.4 µs | 56.5 µs (−34%) | 1,237 → 697 | 80,862 → 47,926 |
| Messages | 48.6 µs | 29.4 µs (−40%) | 879 → 449 | 55,727 → 25,079 |
| Gemini | 169.7 µs | 129.6 µs (−24%) | 2,306 → 1,417 | 146,378 → 95,802 |
Thinking deltas (T201 follow-up, 2026-11-27). Once a Messages answer that thinks was valued, each of its thinking deltas went through the decoded path: the key thinking is one the walker checks by name. Measured with the same benchmark on an 18-event answer whose twelve deltas are thinking deltas with their estimated_tokens and a signature delta (messages_thinking, median of six, the text answer of the same run beside it):
| Messages, valued | text deltas | thinking deltas, decoded | thinking deltas, proven from the bytes |
|---|---|---|---|
| time | 35.2 µs | 69.7 µs | 39.4 µs (−43%) |
| allocations | 449 | 911 | 449 |
| bytes | 25,382 | 61,258 | 25,936 |
That is 2.9 µs and 38 allocations per thinking delta, twice the observer’s cost on a stream that thinks at length, so the scan now proves a thinking text too: a string under thinking in a Messages object every type of which is thinking or thinking_delta, which is exactly what the full valuation takes out before its walk. signature and estimated_tokens joined the passed-through names. The equivalence test over the golden corpus and its mutations, and TestStreamValuationFastPathThinkingDeltas, hold the scan to the full valuation’s answer; without valuation the stream is unchanged (23 µs).
The same stream without valuation is unchanged (31–32 µs for Chat). End to end, the realistic stream at c=32 on the setup above (each gateway in a --cpus 2 container, PostgreSQL, the gwbench mock, 1,600 streams per target in 4 rounds per repetition, alternating order), on 2026-11-15:
| CPU ms per streamed response | 54414f9 |
f3262c14 |
T077.002 |
|---|---|---|---|
session 1, 6 repetitions (build 575cf216) |
2.57 [2.38–2.62] | 3.27 [3.24–3.32] | 2.69 [2.50–2.75] |
| session 2, 4 repetitions (final build) | 2.55 [2.47–2.63] | 3.32 [3.27–3.33] | 2.70 [2.65–2.79] |
- Against
f3262c14: −0.61 ms [−0.75 to −0.52] in session 1 and −0.62 ms [−0.68 to −0.48] in session 2, every repetition below zero. That is about 80% of the tip’s excess over the baseline (+0.72 and +0.78 ms in these sessions). - Against the baseline: +0.10 ms [−0.12 to +0.34] in session 1 and +0.16 ms [+0.15 to +0.18] in session 2. About 0.15 ms per streamed response (~6%) remains. In the observer benchmark the largest remaining per-event decode added since the baseline is the GenAI telemetry of each event (T052), about a quarter of the valued observer’s time.
- Latency, TTFT and errors: no change that the spread separates (TTFT p50 321–322 ms for all three builds), and no errors.
- The session 1 build differs from the final one only in counting the checked values as walk nodes and in sharing the media type list with the walker, both found in review. Images:
54414f9built with its floating Go 1.25 image, the others with Go 1.27.3.
What was not measured
Section titled “What was not measured”- Translation cells. At the time
gwbenchsent only Chat Completions and its mock served only/v1/chat/completions, so the only cell served was Chat → OpenAI (same protocol). They are measured since 2026-11-15 in Translated cells. - Other gateways: none in this run; their numbers are unchanged.
Baseline for the protocol translator work: the translated cells session, which measured every cell against the same-protocol anchor in the same rounds.
Translated cells (T077.001)
Section titled “Translated cells (T077.001)”Measured on 2026-11-15 to give the translator work (T077) a baseline per cell: what each client route costs against each upstream dialect, beside the same-protocol anchor (Chat → OpenAI Chat) in the same rounds. gwbench now sends Chat Completions, Responses and Anthropic Messages (a target’s api), and its mock answers the four dialects the gateway translates to, JSON and SSE, with each dialect’s own usage fields and terminal event (see How to reproduce).
Cells. Three client routes against four upstream dialects of the same mock, twelve cells on the tip: Chat → OpenAI Chat (anchor), OpenAI Responses (api: responses), Anthropic Messages, Gemini; Responses → OpenAI Responses (native passthrough), OpenAI Chat (api: chat_completions), Anthropic Messages, Gemini; Messages → Anthropic Messages (native passthrough), OpenAI Chat, OpenAI Responses (api: responses), Gemini. One alias per cell and profile, each with a single target.
The 2026-10-26 baseline (54414f9) serves only the three same-protocol cells (Chat → OpenAI Chat, native Responses, native Messages). Its configuration validation refuses an Anthropic target outside a messages alias and a Gemini target outside a generate_content alias, and it has no api setting on OpenAI targets: Chat → Anthropic, Chat → Gemini and Chat → Responses came later the same day (ab0b1fc1, f87dbb91), and the Messages and Responses ingress to other dialects with the canonical translation of 5cf8bd1a (2026-10-30). So the nine translated cells have no baseline row; the anchor of the same session is their reference.
Environment. Mac Mini M4 Pro (12 cores, 24 GB), macOS 26.6.2, OrbStack 2.2.5 (Docker 29.8.2, Linux VM with 12 CPUs and 11.7 GiB), the T044 layout: each gateway in its own container with --cpus 2 and no memory limit; the mock, PostgreSQL 16.15 (postgres:16.15-alpine, image sha256:aa6cb33cf4c19c765cefcf48d96176258800870a9a1e930ad126200f9d825139, one database per gateway, default settings, data on the VM disk) and the gateways in one network namespace over loopback; the client (gwbench, darwin/arm64, Go 1.27.3) reaches every target through the same host port forwarding. No other benchmark or build of this work ran during the rounds; an idle PostgreSQL container of another task stayed up, and host load averages were 2.4–4.6 at the start of the repetitions.
Builds. The host had about 5 GB of free disk, too little for two Docker builds, so each commit was built on the host from its own clean source (git archive), with the flags of its own Dockerfile, and run on the Dockerfile’s runtime base (gcr.io/distroless/static-debian12:nonroot@sha256:afa5c872c891853ca7fcf1f12c3edb23f7eeef36189728842dd51042ff57f7ab) with the binary mounted: 2a091a57 with Go 1.27.3 (CGO_ENABLED=0, -trimpath -buildvcs=false, revision 2a091a57e9876a6154073fa0f0f56dbc105f1898, unmodified), binary sha256:2d10749deaf2c3a4e5522279729a9caafdbaf2f2c639e7ccc3601e3cc299fa5e; 54414f9 with Go 1.25.14 (-trimpath, as its Dockerfile), binary sha256:59f04d21984c35fe46f5083ecfc36e8a4b9488ffd356d68fd401f2c99d64ed0b. The console bundles were built with the host’s Node 26 instead of the Dockerfile’s Node 22; they do not run on the request path. The mock is gwbench -mock of this change (linux/arm64, sha256:73ee6278b7d692e9a1faf8d6e9e1c65dae8bea25b91262f502ab48eedb66e1e1).
Configuration. The T044 YAML on both builds (the 2026-10-26 shipped configuration with request_timing on and OTLP off, PostgreSQL with the default pool of 10 connections), with the providers and aliases of the cells: an OpenAI-protocol, an Anthropic-protocol and (tip only) a Gemini-protocol connection to the mock. The realistic profile is m@300ms,20ms, and m--300ms--20ms on the Gemini targets, whose model segment refuses @ and ,.
Rounds. Four repetitions; in each, the target order rotates, and gwbench rotates it again between its own rounds. Every point has a direct target per client dialect (the mock called in that dialect), every tip cell and the baseline’s three cells. Instant upstream without streaming: concurrency 1 (3,000 requests per target in 3 rounds) and concurrency 32 (8,000 in 4 rounds); realistic upstream with streaming: concurrency 32 (1,280 in 4 rounds); 50 warmup calls per target. Added latency is the target’s p50 minus the p50 of the direct target of its own dialect in the same run. After each repetition the three usage tables were truncated with both gateways idle, to keep the database within the free disk. Each cell is the median of the four repetitions with their minimum and maximum, in absolute values. The fourth repetition is recorded in Performance.
Realistic upstream, stream, c=32 (16 tokens per answer): the cells separate here, by CPU.
| Client route → upstream | Build | added p50 (ms) | TTFT p50 (ms) | CPU ms per response | peak memory (MB) |
|---|---|---|---|---|---|
| Chat, direct to the mock | — | — | 310.09 [309.60–310.79] | — | — |
| Responses, direct to the mock | — | — | 310.58 [309.65–311.58] | — | — |
| Messages, direct to the mock | — | — | 309.77 [309.54–311.46] | — | — |
| Chat → OpenAI Chat (same protocol) | 2a091a57 |
23.14 [14.68–32.07] | 323.42 [323.14–325.22] | 2.63 [2.54–2.72] | 67 [65–68] |
54414f9 |
17.34 [9.44–20.69] | 323.51 [322.37–324.63] | 2.50 [2.35–2.56] | 24 [24–25] | |
| Chat → OpenAI Responses | 2a091a57 |
20.28 [10.49–23.66] | 321.74 [320.88–322.90] | 3.83 [3.77–4.01] | 67 [65–67] |
| Chat → Anthropic Messages | 2a091a57 |
25.70 [17.48–28.47] | 321.63 [319.43–323.85] | 3.59 [3.53–3.81] | 67 [65–67] |
| Chat → Gemini | 2a091a57 |
17.71 [11.49–20.94] | 324.69 [323.65–325.50] | 4.11 [4.04–4.45] | 67 [65–68] |
| Responses → OpenAI Responses (same protocol) | 2a091a57 |
22.00 [14.93–28.69] | 322.24 [320.91–323.06] | 2.88 [2.72–2.95] | 68 [65–68] |
54414f9 |
16.07 [10.91–21.72] | 321.46 [320.85–322.88] | 2.86 [2.73–2.94] | 24 [24–25] | |
| Responses → OpenAI Chat | 2a091a57 |
12.87 [9.79–18.15] | 323.80 [321.84–324.73] | 3.42 [3.27–3.62] | 64 [60–66] |
| Responses → Anthropic Messages | 2a091a57 |
15.29 [11.67–20.95] | 322.51 [321.75–322.85] | 4.16 [3.99–4.21] | 65 [65–66] |
| Responses → Gemini | 2a091a57 |
11.54 [7.77–20.06] | 326.43 [325.99–326.58] | 4.87 [4.79–4.92] | 67 [65–67] |
| Messages → Anthropic Messages (same protocol) | 2a091a57 |
15.18 [8.90–28.07] | 322.11 [321.11–323.20] | 2.53 [2.44–2.60] | 67 [65–67] |
54414f9 |
13.19 [9.46–14.72] | 322.52 [321.54–325.91] | 2.46 [2.33–2.55] | 25 [24–25] | |
| Messages → OpenAI Chat | 2a091a57 |
18.51 [8.18–19.95] | 323.78 [323.00–326.26] | 3.21 [3.08–3.35] | 63 [62–66] |
| Messages → OpenAI Responses | 2a091a57 |
10.38 [5.69–22.19] | 322.62 [321.31–324.81] | 4.34 [4.27–4.44] | 66 [63–66] |
| Messages → Gemini | 2a091a57 |
8.48 [1.25–16.76] | 325.34 [324.24–326.83] | 4.63 [4.58–4.87] | 67 [65–68] |
Instant upstream, c=32, JSON.
| Client route → upstream | Build | added p50 (ms) | p99 (ms) | req/s | CPU ms/req | peak memory (MB) |
|---|---|---|---|---|---|---|
| Chat, direct to the mock | — | — | 8.11 [7.93–8.35] | 13,542 [11,413–17,034] | — | — |
| Responses, direct to the mock | — | — | 9.31 [8.78–9.91] | 14,009 [11,056–17,062] | — | — |
| Messages, direct to the mock | — | — | 8.92 [7.90–9.46] | 19,633 [13,040–22,510] | — | — |
| Chat → OpenAI Chat (same protocol) | 2a091a57 |
13.98 [12.97–14.92] | 62.34 [31.77–330.35] | 1,667 [583–2,185] | 0.29 [0.26–0.42] | 57 [52–59] |
54414f9 |
13.76 [13.06–14.57] | 116.81 [21.75–231.03] | 1,283 [810–2,220] | 0.30 [0.29–0.44] | 23 [20–24] | |
| Chat → OpenAI Responses | 2a091a57 |
14.15 [13.47–14.29] | 52.23 [36.56–192.39] | 1,735 [1,307–2,032] | 0.27 [0.26–0.32] | 58 [52–59] |
| Chat → Anthropic Messages | 2a091a57 |
13.89 [12.89–14.12] | 38.95 [28.19–281.79] | 2,045 [1,284–2,215] | 0.26 [0.26–0.29] | 58 [51–59] |
| Chat → Gemini | 2a091a57 |
13.77 [13.04–14.54] | 29.85 [20.14–1,094.88] | 2,087 [718–2,183] | 0.27 [0.26–0.36] | 58 [51–59] |
| Responses → OpenAI Responses (same protocol) | 2a091a57 |
13.16 [12.77–13.66] | 27.41 [22.48–225.84] | 2,131 [1,258–2,268] | 0.27 [0.26–0.29] | 58 [51–59] |
54414f9 |
13.09 [12.82–13.52] | 32.60 [22.09–260.45] | 2,131 [1,037–2,260] | 0.29 [0.28–0.32] | 24 [20–24] | |
| Responses → OpenAI Chat | 2a091a57 |
13.60 [13.35–14.15] | 26.52 [22.68–37.28] | 2,120 [1,910–2,186] | 0.27 [0.27–0.29] | 57 [51–59] |
| Responses → Anthropic Messages | 2a091a57 |
14.09 [13.51–14.40] | 84.73 [21.69–298.47] | 1,597 [865–2,171] | 0.32 [0.28–0.37] | 57 [51–59] |
| Responses → Gemini | 2a091a57 |
13.75 [13.54–13.93] | 69.57 [24.29–157.42] | 1,905 [1,211–2,122] | 0.29 [0.28–0.38] | 57 [51–59] |
| Messages → Anthropic Messages (same protocol) | 2a091a57 |
13.66 [12.64–14.66] | 62.12 [21.64–224.54] | 1,695 [1,068–2,283] | 0.30 [0.26–0.35] | 57 [51–59] |
54414f9 |
13.48 [12.75–14.72] | 47.18 [28.84–109.25] | 1,916 [1,052–2,232] | 0.28 [0.27–0.38] | 23 [20–24] | |
| Messages → OpenAI Chat | 2a091a57 |
13.75 [12.96–20.19] | 40.97 [23.96–117.04] | 1,828 [886–2,203] | 0.28 [0.28–0.40] | 57 [52–59] |
| Messages → OpenAI Responses | 2a091a57 |
13.93 [13.29–43.30] | 64.38 [27.33–140.49] | 1,744 [663–2,136] | 0.31 [0.28–0.50] | 57 [52–59] |
| Messages → Gemini | 2a091a57 |
14.29 [13.18–28.18] | 72.28 [29.16–460.55] | 1,600 [504–2,126] | 0.33 [0.28–0.53] | 57 [52–59] |
Instant upstream, c=1, JSON.
| Client route → upstream | Build | p50 (ms) | p99 (ms) | req/s | CPU ms/req | peak memory (MB) |
|---|---|---|---|---|---|---|
| Chat, direct to the mock | — | 0.10 [0.09–0.10] | 0.28 [0.23–0.31] | 8,339 [7,862–8,804] | — | — |
| Responses, direct to the mock | — | 0.09 [0.09–0.10] | 0.23 [0.20–0.25] | 9,068 [8,914–9,246] | — | — |
| Messages, direct to the mock | — | 0.09 [0.09–0.09] | 0.21 [0.20–0.23] | 9,436 [9,312–9,525] | — | — |
| Chat → OpenAI Chat (same protocol) | 2a091a57 |
1.84 [1.67–2.57] | 8.59 [4.99–28.63] | 385 [168–517] | 0.28 [0.23–0.62] | 55 [41–57] |
54414f9 |
4.60 [2.80–6.07] | 14.51 [9.73–22.14] | 204 [147–269] | 0.57 [0.42–0.81] | 20 [15–21] | |
| Chat → OpenAI Responses | 2a091a57 |
5.64 [5.62–5.77] | 11.54 [11.17–14.43] | 178 [153–185] | 0.67 [0.63–0.72] | 55 [41–57] |
| Chat → Anthropic Messages | 2a091a57 |
5.77 [5.60–6.22] | 11.66 [10.06–21.08] | 150 [139–173] | 0.75 [0.68–0.80] | 55 [40–56] |
| Chat → Gemini | 2a091a57 |
5.72 [5.31–6.22] | 9.95 [8.83–11.03] | 182 [154–209] | 0.65 [0.55–0.84] | 55 [40–56] |
| Responses → OpenAI Responses (same protocol) | 2a091a57 |
5.72 [4.36–6.12] | 11.07 [8.50–28.83] | 163 [129–232] | 0.73 [0.47–0.73] | 55 [41–56] |
54414f9 |
5.77 [5.14–6.27] | 10.55 [9.88–13.86] | 170 [151–209] | 0.76 [0.56–0.88] | 21 [14–21] | |
| Responses → OpenAI Chat | 2a091a57 |
5.01 [2.34–5.65] | 10.40 [9.12–13.31] | 209 [168–270] | 0.56 [0.43–0.68] | 56 [40–60] |
| Responses → Anthropic Messages | 2a091a57 |
5.48 [2.48–5.99] | 18.60 [11.98–23.54] | 167 [162–243] | 0.71 [0.35–0.79] | 55 [42–57] |
| Responses → Gemini | 2a091a57 |
3.33 [2.74–6.43] | 20.78 [8.03–71.98] | 172 [131–252] | 0.65 [0.46–0.82] | 55 [41–57] |
| Messages → Anthropic Messages (same protocol) | 2a091a57 |
5.16 [2.94–5.64] | 12.73 [9.39–54.18] | 170 [116–278] | 0.66 [0.35–0.77] | 54 [41–57] |
54414f9 |
5.10 [2.64–5.58] | 10.78 [9.23–18.42] | 207 [180–246] | 0.55 [0.47–0.62] | 19 [15–20] | |
| Messages → OpenAI Chat | 2a091a57 |
4.15 [2.47–6.14] | 8.94 [7.63–34.41] | 230 [117–324] | 0.55 [0.35–0.81] | 55 [41–64] |
| Messages → OpenAI Responses | 2a091a57 |
5.24 [2.53–6.70] | 14.78 [4.66–28.78] | 168 [133–371] | 0.70 [0.27–1.06] | 55 [41–57] |
| Messages → Gemini | 2a091a57 |
3.64 [2.58–6.03] | 9.68 [8.51–13.96] | 226 [163–270] | 0.58 [0.44–0.76] | 55 [41–57] |
- Streaming is where translation costs. Against the anchor’s 2.63 ms of CPU per streamed response, the translated cells cost 3.21 (Messages → OpenAI Chat) to 4.87 ms (Responses → Gemini), every repetition above the anchor: per-repetition Δ from +0.57 [+0.47–+0.72] to +2.22 [+2.16–+2.33] ms, up to +85%. The upstream dialect sets most of it: Gemini +1.48 to +2.22, OpenAI Responses +1.24 to +1.74, Anthropic +0.96 to +1.50, OpenAI Chat behind Responses or Messages +0.57 to +0.79. The two native passthroughs stay close to the anchor (Messages 2.53, Δ −0.11 [−0.17–−0.03]; Responses 2.88, Δ +0.24 [+0.09–+0.35]) and cost what the baseline’s do (2.46 and 2.86; Δ tip −
54414f9+0.08 [0.00–+0.13] and +0.01 [−0.06–+0.07]); the anchor is +0.13 [+0.08–+0.29] over its baseline. Latency and TTFT do not separate the cells at this load: added p50 ranges overlap, and TTFT p50 is 321.6–326.4 ms everywhere, the Gemini cells 2–3 ms later. - Without streaming, translation is not visible. At c=32 every cell is within the anchor’s spread: CPU 0.26–0.33 against 0.29 ms per request, added p50 13.2–14.3 ms against 14.0 ms, with the per-repetition Δ ranges crossing zero; the cost is PostgreSQL and the pool, as in T044.
- c=1 does not separate the cells either. Its latency is the synchronous PostgreSQL commit, and in this session it was bimodal between repetitions (p50 around 2.5 or 5.6 ms, for the same target); CPU per request at c=1 grows with that wall time (idle background work divided by fewer requests), so the anchor’s 1.84 ms and 0.28 CPU ms are an artifact of its rounds, not a cheaper path. A check with shorter alternating rounds (1,800 requests in 6 rounds) put the anchor, Chat → Anthropic, native Messages and the baseline’s anchor all at 2.8–3.0 ms p50 and 0.27–0.39 CPU ms.
- Peak memory is 63–68 MB on the tip against 24–25 MB on the baseline, on every cell; it does not depend on the cell (T159).
- Errors: none in the stream or at c=1 except one HTTP 503 in 12,000 (Responses → Anthropic). At c=32, HTTP 503
durable_usage_unavailablein bursts on Chat → OpenAI Chat (33 of 32,000), Chat → OpenAI Responses (18), native Responses (19) and on the baseline (9 and 14), as in T044.
Where the streamed translation spends (pprof)
Section titled “Where the streamed translation spends (pprof)”A CPU profile of each tip cell (scratch build with net/http/pprof, never committed), 20 s under the realistic stream at c=32, about 920 responses each, alone on the gateway. Gateway CPU in the 20 s, and the main parts:
| Cell | total | relay to the client (incl. ingress encoder) | upstream decoder goroutine | of which provider meter | stream usage observer |
|---|---|---|---|---|---|
| Chat → OpenAI Chat (anchor) | 1.44 s | 0.67 s | — | — | 0.26 s |
| Messages → Anthropic (native) | 1.37 s | 0.72 s | — | — | 0.20 s |
| Responses → OpenAI Responses (native) | 1.72 s | 0.99 s | — | — | 0.39 s |
| Chat → Anthropic | 1.98 s | 0.68 s | 0.63 s | 0.12 s | 0.32 s |
| Chat → OpenAI Responses | 2.16 s | 0.92 s | 0.63 s | 0.12 s | 0.37 s |
| Chat → Gemini | 2.39 s | 0.79 s | 0.91 s | 0.31 s | 0.38 s |
| Messages → OpenAI Chat | 1.91 s | 1.12 s | — | — | 0.34 s |
| Responses → OpenAI Chat | 2.17 s | 1.33 s | — | — | 0.26 s |
| Responses → Anthropic | 2.36 s | 1.03 s | 0.67 s | 0.12 s | 0.20 s |
| Messages → OpenAI Responses | 2.53 s | 1.15 s | 0.70 s | 0.15 s | 0.25 s |
| Responses → Gemini | 2.51 s | 1.11 s | 0.65 s | 0.21 s | 0.34 s |
| Messages → Gemini | 2.65 s | 0.96 s | 0.93 s | 0.35 s | 0.33 s |
The baseline T077 must beat is these rows and the CPU per response above. The hot spots, each per SSE event of the answer:
- Upstream decoder (
translateAnthropicStream,translateGeminiStream,translateResponsesStream, run in a goroutine behind anio.PipebytranslateChatInBackground): each event is decoded withencoding/jsoninto a typed struct, and each canonical Chat chunk is built as nestedmap[string]anyand encoded withjson.Marshal(send), then written through the pipe and parsed again downstream. In Chat → Gemini, 0.23 s of the 0.91 s is the decode, 0.21 s the chunk encoding, 0.11 s the line scanner. Chat → Anthropic fromab0b1fc1(ADR 0124) and Chat → Gemini fromf87dbb91(ADR 0111), both 2026-10-26; Chat → Responses from5cf8bd1a(2026-10-30). - Provider meter decodes every event a second time into a generic tree (
geminiMeter.chunk,anthropicStreamMeter.event,responsesStreamMeter.eventthroughquotaJSONObject/boundedJSONObject), to prove the native usage before translation: 0.31–0.35 s in the Chat and Messages → Gemini cells, because every Gemini chunk carriesusageMetadata, and 0.12–0.15 s for Anthropic and Responses. Fromed0c8561(T004.001, 2026-11-01). This is the translated-path counterpart of what T077.002 removed from the native stream observer. - Ingress encoder (
translationWriterwithmessagesStream/responsesStream, canonical Chat → Messages or Responses events): it decodes each canonical chunk again (json.Unmarshal) and encodes the dialect’s events (json.Marshal); 0.46 s in Responses → OpenAI Chat (responsesStream.chunk0.24 s,send0.23 s) and 0.26 s in Messages → OpenAI Chat, which is why the relay column of the Responses and Messages routes is 0.3–0.7 s above the anchor’s. From5cf8bd1a(2026-10-30, canonical translation). - The stream usage observer still reads the translated stream (0.20–0.38 s, like the anchor’s 0.26 s), although the provider meter has already proved the usage; and the per-event GenAI telemetry (
genAIResponse.observeStreamEvent, 0.05–0.11 s, T052) runs on it too.
So a translated stream decodes each event two to four times (provider decoder, provider meter, ingress encoder, observer) and encodes it once or twice; most of the excess is encoding/json on small per-event values. Not fixed here; see T077 results.
Not measured: the GenerateContent ingress (gwbench does not send it, although the mock answers Gemini), Cohere and Bedrock upstreams (the mock does not speak them), tool calls, reasoning and images in the translated streams (the mock answers text only), and the instant upstream with streaming.
T077 results
Section titled “T077 results”Measured on 2026-11-15: the four hot spots above and the GenAI telemetry decode (T077.003), cut without changing any byte the client receives or any usage, valuation, quota or telemetry result.
What changed. Each change reads from the bytes only what it can prove decodes the same, and sends anything else through the previous decode:
- Upstream decoder (
translate_chunks.go): the Anthropic, Gemini and Responses adapters append each canonical Chat chunk directly injson.Marshal’s key order and string escaping, one write per chunk as before, instead of building nested maps; they read the scanner’s bytes instead of copying each line twice. Theio.Pipegoroutine stays: it carries the cancellation and error contract of the adapters, and with the encoding gone its cost is small. - Provider meters (
quota_meter_fast.go): a byte scan proves whatquotaJSONObjectwould accept (valid UTF-8 and JSON, one object, no repeated or escaped key, within its depth and node limits) and reads the members a meter checks by value. Text deltas, pings and the other events a meter only needs well formed are proven from the bytes; message start and stop, terminal Responses events, finishing or grounded Gemini candidates, errors and prompt feedback take the full meter. A Gemini runningusageMetadatais built as the same tree and given to the same code. - Ingress encoders (
translate_ingress_fast.go): canonical text chunks (id,choiceswithindex, the delta strings andfinish_reason) are read into the same structjson.Unmarshalfills, andcontent_block_delta,response.output_text.deltaandresponse.reasoning_summary_text.deltaare appended directly. Tool calls, usage, errors and repeated, escaped, case-variant or mistyped members takejson.Unmarshal. - Observer telemetry (
genai_telemetry_fast.go, T077.003): the identity and finish reasons of every Chat and Gemini chunk are read from the bytes instead of decodingchoicesorcandidatesinto a typed slice.
The observer’s own envelope decode of the translated stream (hot spot 4) is unchanged: it still sets the request’s usage record, termination and telemetry, and replacing it would change the valuation path T077.002 already narrowed.
Equivalence. A golden recording written on 4ee0fd1a before any change (stream_translation_golden: 141 upstream streams of the three adapters, with usage on and off and two meter settings, observed again as Chat; every ingress encoder over 240 canonical streams (those adapters’ outputs among them) in four read sizes; and the observer’s counters and telemetry over 188 native streams; tools, reasoning, refusals, escaping, invalid UTF-8, escaped, case-folded and repeated keys, deep nesting, provider errors and cut streams included) passes unchanged after each change, as do the T077.002 valuation golden and the translated and native stream route tests on PostgreSQL. Each fast path has a differential test over that corpus and its mutations (the fast path must leave exactly the state the full decode leaves, and a declined input must change nothing) and a fuzz target, run 65–70 s each.
Component benchmarks (go test -bench, Apple M4 Pro, median of six runs of 2,000 iterations, one 18-event answer):
| Benchmark | 4ee0fd1a |
T077 | allocations | bytes |
|---|---|---|---|---|
| Anthropic adapter with meter | 46.4 µs | 22.4 µs | 1,104 → 246 | 125,973 → 84,037 |
| Gemini adapter with meter | 104.9 µs | 64.2 µs | 2,323 → 679 | 205,635 → 117,123 |
| Responses adapter with meter | 60.9 µs | 37.3 µs | 1,405 → 349 | 138,280 → 93,468 |
| Anthropic meter alone | 12.9 µs | 9.0 µs | 574 → 183 | 28,048 → 14,477 |
| Gemini meter alone | 47.6 µs | 38.7 µs | 1,581 → 580 | 84,640 → 46,256 |
| Responses meter alone | 22.3 µs | 17.1 µs | 929 → 297 | 37,075 → 24,000 |
| Messages ingress encoder | 30.0 µs | 16.1 µs | 461 → 272 | 28,883 → 16,708 |
| Responses ingress encoder | 50.6 µs | 31.6 µs | 834 → 469 | 47,998 → 38,278 |
| GenerateContent ingress encoder | 33.2 µs | 30.4 µs | 549 → 584 | 38,496 → 38,781 |
| Observer, Chat, unvalued | 33.1 µs | 24.7 µs | 413 → 344 | 24,543 → 23,141 |
| Observer, Chat, valued | 43.8 µs | 35.3 µs | 456 → 387 | 27,645 → 26,261 |
| Observer, Gemini, valued | 153.9 µs | 145.2 µs | 1,732 → 1,662 | 105,689 → 104,373 |
The Messages and Responses observers are unchanged (their telemetry already decoded only terminal events).
End to end. The T077.001 layout and configuration (each gateway in its own --cpus 2 container, PostgreSQL 16.15 on the VM disk, the gwbench mock, OrbStack 2.2.5, macOS 26.6.2), with 4ee0fd1a and the T077 tip running side by side and every one of the twelve cells on both; three alternating repetitions, the target order rotated per repetition and again by gwbench between its rounds; realistic stream c=32 (1,280 streams per target in 4 rounds) and instant c=32 (8,000 requests in 4 rounds), 50 warmup calls. Both binaries were built on the host from their own clean sources with the Dockerfile’s flags (Go 1.27.3, linux/arm64, CGO_ENABLED=0 -trimpath -buildvcs=false) and run on the Dockerfile’s distroless base: 4ee0fd1a sha256:0ae790e1a0679270b14d5b39b0f9b91587c504509758e7907a64b34c35a0a2f0, T077 tip 0f025206 sha256:4bbe93eb875799e3ea939ae6332c064b4ccf4d341abd6b058de5c39ca47c5956. Host load averages 3.0–5.8 at the start of the repetitions; no other benchmark or build of this work ran during them. Median and [minimum–maximum] of the three repetitions:
| Realistic stream, c=32: CPU ms per response | 4ee0fd1a |
T077 | Δ per repetition |
|---|---|---|---|
| Chat → OpenAI Chat (anchor, same protocol) | 2.65 [2.64–2.67] | 2.63 [2.63–2.67] | −0.03 [−0.03–+0.02] |
| Messages → Anthropic (same protocol) | 2.55 [2.51–2.61] | 2.54 [2.54–2.59] | −0.01 [−0.08–+0.08] |
| Responses → OpenAI Responses (same protocol) | 2.93 [2.90–2.95] | 2.95 [2.90–2.99] | +0.02 [0.00–+0.03] |
| Messages → OpenAI Chat | 3.25 [3.22–3.29] | 2.90 [2.89–2.93] | −0.32 [−0.40–−0.32] |
| Responses → OpenAI Chat | 3.59 [3.42–3.60] | 3.17 [3.13–3.17] | −0.42 [−0.43–−0.29] |
| Chat → Anthropic | 3.87 [3.85–3.90] | 3.14 [3.11–3.15] | −0.74 [−0.75–−0.73] |
| Chat → OpenAI Responses | 4.04 [4.00–4.06] | 3.27 [3.23–3.28] | −0.79 [−0.81–−0.72] |
| Responses → Anthropic | 4.26 [4.22–4.31] | 3.39 [3.37–3.41] | −0.88 [−0.90–−0.86] |
| Chat → Gemini | 4.37 [4.30–4.41] | 3.66 [3.63–3.72] | −0.65 [−0.78–−0.64] |
| Messages → OpenAI Responses | 4.54 [4.50–4.55] | 3.53 [3.46–3.55] | −1.01 [−1.09–−0.95] |
| Messages → Gemini | 4.81 [4.73–4.85] | 3.96 [3.91–3.97] | −0.85 [−0.88–−0.82] |
| Responses → Gemini | 5.01 [4.85–5.13] | 4.17 [4.09–4.20] | −0.81 [−1.04–−0.68] |
- Every translated cell is cheaper in every repetition, by 0.32 to 1.01 ms per streamed response (10 to 22%). The same-protocol cells do not move, as expected: none of their paths changed except the Chat telemetry, which is small next to the relay.
- Against the anchor (2.63 ms on the tip): the excess of the translated cells fell from +0.60 to +2.36 ms on
4ee0fd1ato +0.27 to +1.54 ms, about 45% of it removed on average (Messages → OpenAI Chat 2.90 ms, Chat → Anthropic 3.14 ms, Responses → Gemini 4.17 ms). - Latency, TTFT, throughput and memory do not separate the builds at this load: added p50 ranges overlap (2–30 ms on both), TTFT p50 is 319.6–325.8 ms on both, 45–46 streams per second, peak memory 62–70 MB on both.
- Instant upstream, c=32: every cell within the other build’s spread (CPU 0.25–0.31 ms per request on both, added p50 13.7–15.1 ms on both); translation is not visible without streaming, as in T077.001. No errors in either point.
4ee0fd1ameasured 0.04–0.28 ms more per translated stream than2a091a57did in the T077.001 session (Chat ingress most); its anchor is the same. That difference is between sessions and commits (T077.004–T077.006 valuation work came in between) and was not attributed here; within this session both builds ran the same rounds.
What remains (about +0.3 to +1.5 ms per translated stream over the anchor):
- The upstream adapters still decode each provider event with
encoding/jsoninto a typed struct, and the Gemini runningusageMetadatais still priced on every chunk (quotaGeminiUsage), which keeps the Gemini cells highest. - The observer still decodes the translated stream’s envelope per event (the hot spot 4 above); it owns the request’s usage record and the T077.002 valuation rules, and changing it was left out of this behavior-preserving change.
- The
io.Pipebetween the adapter goroutine and the relay remains; a direct transform would change the adapters’ cancellation and in-band error contract. - Tool-call, usage and error chunks take the full decode in the ingress encoders, and the GenerateContent encoder (not measured end to end) still encodes its chunks from maps.
- The escaping of
appendJSONStringmatches the pinned toolchain’sencoding/json(Go 1.27 on json/v2); a build withGOEXPERIMENT=nojsonv2escapes invalid UTF-8 differently, whichTestAppendJSONStringMatchesMarshalreports.
Not recorded in Performance (no -record run); evidence in docs/internal/test-results/t077/ (ignored).
Memory since 2026-10-26 (T159)
Section titled “Memory since 2026-10-26 (T159)”Measured on 2026-11-15, to explain the ~30 MB of peak memory the integrated A/B found on every point against 54414f9, and to cut what is not needed.
How it was attributed. Each build alone in the T044 container and configuration (--cpus 2, PostgreSQL, no memory limit), from a scratch build that adds net/http/pprof and a runtime/metrics dump (never committed): heap profiles (inuse_space with GODEBUG=memprofilerate=1 for exact per-component bytes at idle, inuse_space and alloc_space under load) and the runtime’s memory classes, read 8 s after start and every 0.5 s during the instant c=32 load (16,000 requests). 54414f9 was built here with Go 1.27.3 as well, so that the code and not the toolchain is compared; with its own Go 1.25.14 it used no less (27 MB against 20 MB at c=32 in the same session), so the toolchain is not the cause.
Live heap at idle, by component (MB, exact):
| Component | 54414f9 |
c6a6ea8f (before) |
T159 |
|---|---|---|---|
| Validation regular expressions compiled at package init | 1.58 | 4.57 | 0.16 (only those used) |
| Console page held with its script and stylesheet inlined | 0.90 | 2.30 | 0.02 |
| Price template patterns (the T044 fix’s cache) | — | 0.29 | 0.26 |
| JSON Schema library metaschemas (its own package init) | — | 0.36 | 0.36 |
| Configuration (decoded, validated, snapshots) | 0.21 | 0.44 | 0.44 |
| HTTP routes | 0.15 | 0.32 | 0.32 |
| PostgreSQL pool and statement caches | 0.14 | 0.26 | 0.27 |
| Everything else | 0.56 | 1.40 | 0.56 |
| Live heap | 3.54 | 9.94 | 2.39 |
- Regular expressions: the tree declares about 270 package-level patterns (93 at
54414f9), most to validate identifiers of features that are off by default, and many with counted repetitions such as{1,255}that compile to 25–110 KB each. All were compiled at start whatever the configuration. - Console page:
index.htmlwith the 2.0 MB script and 0.3 MB stylesheet inlined (so the CSP can hash them) was built at start and kept for the life of the process; the bundle grew from 0.9 MB. - Not in the heap: the WebAssembly runtime and the QuickJS engine (nothing is compiled with
extensionsoff;quickjs.wasmis 1 MB of the binary), the Codex catalog and the guardrail datasets (already parsed on first use), the preset and cost-map catalogs (sorted once, a few KB), connection pools beyond PostgreSQL’s (empty at idle).
Where the peak comes from (one gateway, MB; idle 8 s after start, and the highest working set during the c=32 load):
54414f9 idle |
c6a6ea8f idle |
T159 idle | 54414f9 c=32 |
c6a6ea8f c=32 |
T159 c=32 | T159 c=32, gc_percent 100 |
|
|---|---|---|---|---|---|---|---|
| Live heap (last collection) | 3.2 | 8.9 | 2.4 | 5.3 | 12.3 | 4.8 | 4.8 |
| Heap goal | 6.6 | 27.5 | 8.2 | 11.2 | 38.5 | 16.0 | 10.4 |
| Heap objects | 3.6 | 11.3 | 2.5 | 5.5 | 26.0 | 6.9 | 5.4 |
| Freed heap not yet returned to the OS | 1.8 | 13.3 | 0.0 | 4.0 | 10.3 | 7.1 | 2.5 |
| Goroutine stacks | 0.6 | 0.8 | 0.8 | 1.6 | 2.5 | 1.5 | 2.5 |
| Runtime metadata | 3.0 | 3.4 | 3.2 | 3.5 | 4.6 | 3.7 | 3.7 |
| Goroutines (most seen) | 23 | 30 | 30 | 118 | 107 | 102 | 104 |
| Collections during the 16,000 requests | 287 | 19 | 90 | 148 | |||
| Working set | 11.4 | 31.8 | 8.8 | 19.7 | 50.0 | 26.1 | 20.2 |
- The +30 MB was the live heap multiplied by the collector pacing. The heap goal is live × (1 +
gc_percent/100) and the runtime keeps up to ~10% over the goal before returning freed pages, so the 6.4 MB of new start-up state became ~20 MB of heap atgc_percent200 (goal 38.5 MB against 11.2 MB). - Start-up garbage set the first goal: a collection during boot ran while the configuration checks and the console validation still held several MB, and the freed pages stayed resident (13.3 MB at idle).
- Allocation per request is not the cause:
alloc_spaceunder load is about 52 KB per request onc6a6ea8fagainst about 96 KB on54414f9(pooled buffers, the T044 fix); the collector ran 19 times instead of 287. - The binary doubled (26 → 54 MB): its file pages count in the working set while active, 5.1 MB at idle for
54414f9against 8.1–8.7 MB for the later builds.
What changed (T159).
- Patterns compile on first use (
internal/lazyregexp): every package-levelregexp.MustCompileislazyregexp.New, compiled under async.Oncethe first time it matches, and eagerly in test binaries so a malformed pattern still failsgo testat init. A test forbids new package-level compilations, and the detectors of a configured guardrail compile when the configuration is validated, not on the first scanned request. Unit effect: 40 patterns of{1,255}hold 1.9 KB until used and 4.4 MB once compiled. - The console page is served in place: it is still built, validated and hashed at start, then laid out as the index’s own fragments around the two members, which each request reads from the embedded files; the layout is proven byte for byte against the built page at start, and anything that cannot be proven (a member with CRLF line ends, a tag that also occurs inside the script) keeps the built page. The heap holds 306 bytes instead of 2.4 MB; Range requests and
Content-Lengthare unchanged. - Start-up garbage is released: one
debug.FreeOSMemorybefore the listener (a few ms of boot), so the first heap goal follows what the server keeps.
Allocation per request, CPU and every served byte are unchanged; the focused tests of every package touched pass on PostgreSQL.
Collector pacing: gc_percent stays 200. T159 at its default and with GOGC=100 ran in the same rounds as the T044 points below (five repetitions, per-repetition Δ of 200 against 100):
| Point | peak memory, 200 | peak memory, 100 | Δ peak (MB) | CPU ms/req, 200 | CPU ms/req, 100 | Δ CPU (ms) | req/s, 200 | req/s, 100 |
|---|---|---|---|---|---|---|---|---|
| instant, c=1 | 32.6 [28.0–37.5] | 26.4 [25.5–30.8] | +2.5 [0.0–+11.3] | 0.37 [0.25–0.69] | 0.36 [0.26–0.55] | −0.01 [−0.18–+0.40] | 296 [175–439] | 294 [207–419] |
| instant, c=32 | 31.8 [24.7–35.3] | 28.4 [19.8–30.5] | +6.2 [+0.5–+7.2] | 0.26 [0.26–0.28] | 0.28 [0.27–0.30] | −0.02 [−0.05–0.00] | 2,051 [1,475–2,300] | 1,878 [1,123–2,406] |
| instant, c=128 | 45.9 [40.9–48.1] | 38.7 [32.2–40.4] | +8.7 [+4.9–+10.9] | 0.27 [0.26–0.31] | 0.28 [0.28–0.31] | −0.01 [−0.02–0.00] | 1,598 [955–2,449] | 1,993 [1,068–2,394] |
| realistic stream, c=32 | 38.8 [32.2–39.9] | 30.8 [26.4–31.6] | +7.2 [+5.8–+9.1] | 2.65 [2.59–2.74] | 2.74 [2.61–2.82] | −0.08 [−0.14–+0.04] | 44 | 44 |
With the live heap back at ~5 MB under load, 200 costs 6–9 MB of peak memory per instance and saves 0.08 ms of CPU per streamed response (~3%, lower in four of five repetitions) and ~0.01–0.02 ms per instant request; latency, throughput and the best throughput under a p99 bound do not separate the two at this load, where the pool of 10 PostgreSQL connections sets the ceiling. The 2026-10-31 harness, which measures the collector at high request rates without that ceiling, found the larger gain (throughput under a 1 ms p99 bound 2.3–4×). The memory cost is bounded where it matters: under a cgroup limit the soft limit at 90% of it applies whatever gc_percent is. An operator who prefers the memory sets runtime.gc_percent: 100.
The T044 points re-measured (2026-11-15, the T044 layout, YAML and points; four gateways side by side: 54414f9 built with Go 1.25.14 as in T044, c6a6ea8f, T159 cf83f6fb and the same T159 binary with GOGC=100, the last three with Go 1.27.3; each binary its own file, built on the host with the Dockerfile’s flags and run on its distroless base; five repetitions, target order rotated per repetition and again by gwbench, usage tables truncated between repetitions; the fifth recorded in Performance). Median [minimum–maximum] of the repetitions; Δ per repetition against 54414f9:
| Point | Build | added p50 (ms) | added p99 (ms) | req/s | CPU ms/req | peak memory (MB) | Δ peak (MB) |
|---|---|---|---|---|---|---|---|
| instant, c=1 | 54414f9 |
2.39 [1.82–5.19] | 8.26 [5.20–10.26] | 323 [214–473] | 0.32 [0.26–0.59] | 22.6 [13.7–23.1] | |
c6a6ea8f |
2.36 [2.08–5.62] | 7.37 [4.21–11.02] | 290 [193–416] | 0.40 [0.25–0.58] | 60.9 [40.5–61.9] | +37.9 [+26.8–+40.0] | |
| T159 | 2.35 [2.13–5.98] | 7.53 [3.28–11.77] | 296 [175–439] | 0.37 [0.25–0.69] | 32.6 [28.0–37.5] | +14.3 [+8.2–+15.5] | |
| T159, 100 | 2.40 [2.17–5.30] | 7.99 [4.79–9.04] | 294 [207–419] | 0.36 [0.26–0.55] | 26.4 [25.5–30.8] | +7.4 [+3.4–+11.8] | |
| instant, c=32 | 54414f9 |
13.36 [11.77–15.10] | 39.98 [19.66–211.41] | 1,995 [1,364–2,473] | 0.29 [0.28–0.32] | 23.7 [19.4–27.1] | |
c6a6ea8f |
13.50 [12.21–14.20] | 36.44 [14.08–43.26] | 1,969 [1,653–2,234] | 0.26 [0.25–0.28] | 58.4 [49.5–59.9] | +32.5 [+30.1–+36.7] | |
| T159 | 13.80 [12.16–14.86] | 35.35 [21.44–129.73] | 2,051 [1,475–2,300] | 0.26 [0.26–0.28] | 31.8 [24.7–35.3] | +7.5 [+5.3–+9.4] | |
| T159, 100 | 14.55 [12.21–17.10] | 38.41 [14.08–262.52] | 1,878 [1,123–2,406] | 0.28 [0.27–0.30] | 28.4 [19.8–30.5] | +2.0 [+0.4–+6.8] | |
| instant, c=128 | 54414f9 |
58.52 [51.26–61.86] | 146.88 [−126.81–961.07] | 1,481 [1,013–2,378] | 0.33 [0.30–0.35] | 34.8 [31.8–36.8] | |
c6a6ea8f |
57.31 [51.06–98.03] | −109.48 [−132.12–768.07] | 2,188 [888–2,423] | 0.26 [0.26–0.31] | 70.7 [64.2–72.5] | +34.2 [+31.0–+36.8] | |
| T159 | 58.06 [50.73–66.67] | 133.96 [−128.78–809.98] | 1,598 [955–2,449] | 0.27 [0.26–0.31] | 45.9 [40.9–48.1] | +11.3 [+7.5–+13.3] | |
| T159, 100 | 58.29 [51.65–72.27] | −59.84 [−125.34–521.53] | 1,993 [1,068–2,394] | 0.28 [0.28–0.31] | 38.7 [32.2–40.4] | +2.6 [+0.4–+3.9] | |
| realistic stream, c=32 | 54414f9 |
21.11 [18.11–26.54] | −123.10 [−144.94–22.60] | 44 | 2.56 [2.51–2.61] | 25.8 [24.1–26.4] | |
c6a6ea8f |
28.30 [13.68–36.57] | −135.57 [−146.01–20.12] | 44 | 2.65 [2.59–2.79] | 62.1 [54.5–65.2] | +36.1 [+30.4–+38.8] | |
| T159 | 23.76 [14.91–36.87] | −138.99 [−144.18–10.00] | 44 | 2.65 [2.59–2.74] | 38.8 [32.2–39.9] | +12.8 [+8.1–+13.9] | |
| T159, 100 | 24.11 [20.92–31.72] | −123.89 [−139.82–63.52] | 44 | 2.74 [2.61–2.82] | 30.8 [26.4–31.6] | +5.1 [+2.3–+5.6] |
- Peak memory fell 23–28 MB against
c6a6ea8fon every point, to 32.6 MB at c=1, 31.8 MB at c=32, 45.9 MB at c=128 and 38.8 MB on the realistic stream; at idle after start the working set is 17.3 MB (54414f915.7 MB,c6a6ea8f42.4 MB). - CPU per request and latency are unchanged against
c6a6ea8f: CPU at c=32 and c=128 0.26–0.27 ms on both (below54414f9in every repetition), streamed 2.65 ms on both; added p50 within each other’s spread at every point; TTFT p50 320.9–322.4 ms for all four builds. No errors in any repetition. - This session was noisier than T044 at c=1 (host load averages 1.9–4.9 at the start of the repetitions): every build,
54414f9included, has a repetition at 0.55–0.69 ms of CPU per request, so the c=1 medians (0.32–0.40) sit above T044’s 0.26–0.27 and the recorded c=1 highlight carries that noise. c=128 p99 is not comparable, as in T044.
What remains against 54414f9 (+7.5 to +14 MB at the default pacing):
- Collector pacing, 6–9 MB: the decision above. At
gc_percent100 the excess is +2.0 to +7.4 MB. - The binary’s file pages, ~3 MB: active pages of the 54 MB binary against the 26 MB one (8.1 against 5.1 MB at idle). Not a heap matter; it shrinks only with less code.
- Heap, about even: T159 keeps less live heap at idle than
54414f9(2.4 against 3.5 MB); under load the live heap is 4.8 against 5.3 MB. Of what stays, the largest items are the JSON Schema library’s own metaschemas (0.36 MB, compiled by the library at package init), the price template patterns (0.26 MB, bounded by the preset catalog) and the HTTP routes (0.32 MB, twice the baseline’s). - Start-up peak: the console validation still builds the inlined page and its copies once at start (several MB of garbage, released before the listener); the process’s high-water mark includes it, the working set during service does not.
Evidence in docs/internal/test-results/t159/ (ignored).
Single charge (T160.001)
Section titled “Single charge (T160.001)”What changed. A request under a budget made five durable PostgreSQL commits: two before the provider (the quota reservation and the durable receipt) and three after it (the quota measurement, the quota settlement and the receipt’s finalization). With single_charge_settlement the three post-response writes are one transaction. See durable receipts.
How it was measured (2026-11-16). The T044 load generator (gwbench, with gwbench -mock as the instant upstream) against the gateway binary of this change, run natively on the host (Apple M4 Pro, Go 1.27.3), with PostgreSQL 16.15 (postgres:16-alpine) in a local container with default settings and the default pool of 10 connections. One YAML for both targets, differing only in the flag: durable_usage, usage_valuation, request_logs, request_timing, budget_limits and tpm_limits on, one key under a monthly key budget and a token-per-minute ceiling, one approved rate. Each target got a fresh database; flag off and on alternated, with the order swapped every round; 3,000 measured requests plus 200 warmup per run, max_tokens 16. Commits are pg_stat_database.xact_commit of the gateway’s database over the run, divided by the usage events it recorded (warmup included on both sides). Every reservation settled (settled for all 3,200 per run) and every flag-on receipt was finalized; no request failed.
| Concurrency | Flag | Commits per request (4 runs) | Requests per second (4 runs) | p50 latency (ms, 4 runs) |
|---|---|---|---|---|
| 32 | off | 4.96, 4.97, 4.94, 4.92 | 95.9, 93.0, 94.6, 94.6 | 324.9, 336.5, 328.1, 329.6 |
| 32 | on | 3.00, 2.99, 2.96, 3.00 | 95.7, 95.1, 97.5, 93.9 | 322.2, 319.8, 315.9, 328.5 |
| 1 | off | 4.78, 5.09, 5.05, 5.05 | 85.2, 76.7, 75.6, 76.8 | 11.6, 12.9, 13.6, 13.2 |
| 1 | on | 2.84, 3.06, 3.04, 2.80 | 93.1, 86.8, 85.8, 89.9 | 10.5, 11.2, 11.9, 11.0 |
- Commits: about two fewer per request (median 4.95 → 2.99 at c=32), as expected: the post-response commits go from three to one, the two before the provider stay. The counts include the read-only transactions of each request, which are the same on both sides.
- No throughput regression. At c=32 the median is 94.6 req/s off and 95.4 on, within each other’s spread: every request of the run charges the same key budget, and the row lock on that one counter, not the commit count, sets the pace. The single charge holds that lock for one transaction instead of two.
- Latency at c=1: about 2 ms less per request (median p50 13.0 → 11.1 ms), the two synchronous commits that are no longer made.
- Limits. One machine, a local container (not the T044 OrbStack layout with
--cpus 2), one hot budget counter, an instant upstream; absolute numbers are not comparable with the tables above, only the off/on pairs within this session.
Optional original Chat JSON
Section titled “Optional original Chat JSON”The preserve_chat_body flag is disabled by default. It can avoid encoding allocations for eligible unchanged same-format requests, while content/ambiguity checks cost CPU. This integration’s bounded component characterization excluded decoding and HTTP and was used to choose that default; it is not a new gateway-versus-bench result. The response usage parser also avoids the second top-level decode without changing reported counters or native quota proof. Use the complete workload and the same feature set for any comparison; historical tables above do not certify this new flag or its complete performance. See eligibility.
Typed telemetry and cancellation bookkeeping
Section titled “Typed telemetry and cancellation bookkeeping”The integration replaces variadic request/attempt log arguments with typed attributes, uses bounded ID-presence bookkeeping and removes a redundant parent-context callback. Standard cancellation causes distinguish the administrative stop from a client departure while preserving completion and receipt ownership. Shared baseline response-header storage keeps independent append capacity. These are implementation changes under existing controls; they are not a new performance certification. Compare the complete accepted revision under the same workload before assigning a CPU, allocation or throughput improvement; historical tables above do not measure this combined change.
Per-request CPU cuts (2026-11-18, T173)
Section titled “Per-request CPU cuts (2026-11-18, T173)”What changed on the request path. For a request that uses no optional feature, four kinds of work that had no reader were removed, and the per-request log line moved off the request goroutine:
- The buffered answer is no longer decoded a second time for its GenAI identity and finish reasons, and the server span and the Chat attempt span are no longer built, when there is no trace exporter, no request events writer and
agent_sessionsis off. Usage accounting still reads the answer’susagemember as before. With any of those readers, the work runs as before. - The canary lookup returns before it JSON-encodes every target to map the request back to its alias, when neither the model nor its alias has a canary policy.
- The root handler attaches the configuration snapshot, the provider network, the two empty feature markers and the usage state as one context node instead of five, which shortens every later context lookup.
logging.write_mode: async(the new default, asynchronous log writes) encodes and writes the log on one worker behind a bounded queue, with whole lines written in batches.
GOGC stays 200 and the 90% soft limit under a cgroup limit stays the default (runtime.* in YAML, environment first; Go runtime settings); the gc_pacing startup line now names the source of each value.
Rig. The T172 local rig: OrbStack linux/arm64, gateway on CPUs 0-3 (GOMAXPROCS=4), load generator 4-7, mock 8-10, PostgreSQL 16.15 on 11, gateway stdout to /dev/null, binary bind-mounted. Both builds CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -trimpath ./cmd/gateway: 871db7d4 unmodified against the T173 change, the latter run twice, at its default (async) and with logging.write_mode: sync. Each run: 30 s idle, c=1 for 15 s (gateway leg minus direct leg), c=8 and c=16 for 10 s, c=32 for 30 s. Three repetitions with the order rotated; Δ is the median of the per-repetition differences against the unmodified build. Every response was 200.
| openai → openai | CPU/request c=16 | req/s c=16 | req/s c=32 | added p50 c=1 | added p99 c=1 | idle RSS | steady RSS c=32 |
|---|---|---|---|---|---|---|---|
871db7d4 |
51.9 µs [50.3–51.9] | 57,134 | 61,973 | 73.6 µs | 119.0 µs | 34.6 MiB | 37.8 MiB [37.5–51.8] |
T173, async |
45.0 µs, −13% (c=8 −16%) | 67,989, +19% | +17% | −7.3 µs | −12.2 µs | +0.3 MiB | +4.3 MiB [41.9–52.2] |
T173, sync |
46.0 µs, −11% | 64,453, +13% | +11% | −5.9 µs | −15.4 µs | 0.0 | −2.5 MiB |
| openai → anthropic | CPU/request c=16 | req/s c=16 | req/s c=32 | added p50 c=1 | added p99 c=1 | idle RSS | steady RSS c=32 |
|---|---|---|---|---|---|---|---|
871db7d4 |
61.7 µs [59.7–62.2] | 49,041 | 52,396 | 89.8 µs | 146.3 µs | 34.4 MiB | 38.1 MiB |
T173, async |
56.1 µs, −10% | 54,888, +11% | +11% | −6.8 µs | −21.5 µs | +0.7 MiB | +1.8 MiB |
T173, sync |
54.7 µs, −12% | 53,819, +9% | +11% | −3.6 µs | −23.7 µs | −0.1 MiB | +2.5 MiB |
Reading. CPU per request repeats within about ±2% here, so the two modes differ in CPU per request by less than the noise on the translated cell and by about 2 points on the same-protocol one. async gives the higher throughput in both cells and the lower median added latency, because the request goroutine no longer waits on the log encoder and its mutex; that is why it is the default. Its anonymous memory under load is 1.9–3.1 MiB higher (the queue and the 32 KiB batch buffer, plus records waiting under load). Steady RSS at c=32 varies by up to 15 MiB between repetitions of the same build here (OrbStack reclaims file pages mid-run), so only idle RSS and anonymous memory are comparable; idle memory is unchanged. The T172 prototype measured −15% CPU and +17% throughput on the same-protocol cell, but with logging.level: warn (no request line at all) and preserve_chat_body; the asynchronous writer still encodes every line, only elsewhere, which accounts for the remaining few points.
Span store request path cost
Section titled “Span store request path cost”The span store is built so that, off, it costs nothing on the request path and, on, it costs one allocation and one non-blocking channel send per request; the writer works on its own goroutine and its own database connections. BenchmarkChatRequest (internal/gateway/span_store_bench_test.go) serves one buffered Chat Completions request through the whole root handler against a local upstream; with the flag on, the benchmark’s writer is never drained, so its numbers are the request path alone (collection and enqueue), not the writer.
Run (2026-11-22, T186.002). Apple M4 Pro, Go 1.27.4, -benchtime 5000x, five interleaved rounds of two runs of the commit before the span store (1622d06a) and of the change, ten runs each; medians, ranges in brackets.
| Flag | Build | ns/op | B/op | allocs/op |
|---|---|---|---|---|
span_store off |
before | 57,446 [55,965–59,131] | 25,086 | 244 |
span_store off |
after | 57,955 [56,757–60,082] | 25,132 | 244 |
span_store on |
before (flag unknown, no effect) | 57,314 [56,281–59,854] | 25,108 | 244 |
span_store on |
after | 58,392 [56,252–60,602] | 25,661 | 245 |
Reading. Off, the allocation count is unchanged and time and bytes move within the run-to-run spread (the ranges overlap; B/op varies by about ±200 between runs of the same build). On, a request adds exactly one allocation, the recorder in the 1,024-byte size class (960 bytes then; 1,008 bytes since the flag-on cost pass below, which carries the request’s ledger cost in it), which holds two steps inline (a routing decision and one attempt); a request with more steps, such as a fallback, spills them into one more allocation. Time stays within the spread. The design budget was at most three added allocations and 1 KiB per request.
Under load, with the writer and PostgreSQL (2026-11-23, T186.002)
Section titled “Under load, with the writer and PostgreSQL (2026-11-23, T186.002)”The benchmark above leaves the writer out. This run sends real load through the whole binary, writer included, against PostgreSQL. It checks the T186.002 acceptance bound: with the flag on, p99 within 2% of the build before the span store; with the flag off, no change.
History, same rig.
- First run (2026-11-22). The flag on cost, at c=8 and c=32: median p99 +24% to +40%, throughput −11% to −13%, gateway CPU +21-28 µs per request, and 53-57 WAL records per request against 29-34. There were two causes. Span batches waited for the WAL flush of the synchronous per-request commits, and two of the five secondary indexes were never read. The fix:
SET LOCAL synchronous_commit = offin the span batch transaction, and three secondary indexes, two of them partial onkind = 'request'. - Second run (2026-11-22, after that fix). Median p99 +12% to +21%, throughput −8% to −10%, and gateway CPU +24-28 µs per request. PostgreSQL still spent ~130 µs more CPU per request, with 45-48 WAL records per request. Off matched the build before the span store in both runs.
What changed since. A cost pass on the writer. It carries each attempt’s cost from the request instead of reading it back from the ledger (ledger_reads counts the fallback). It writes the trace summaries with one unnest statement per table. gateway_spans has no primary key, only an index on trace_id. The hexadecimal id checks are length checks. The summaries have a BRIN index on first_at.
Environment. Mac Mini M4 Pro (12 cores, 24 GB), macOS 26.6.2, OrbStack 2.2.5 (Docker 29.8.2, Linux VM with 12 CPUs). The T172 rig: each process in its own container, the containers sharing one network namespace, over loopback. The gateway runs on CPUs 0-3 (GOMAXPROCS=4), the load generator (gwbench) on 4-7, and the instant simulated provider (gwbench -mock) on 8-10. PostgreSQL 16.15 (postgres:16.15-alpine) runs on CPU 11 with default settings, a fresh volume and one database per build. Gateway stdout and stderr go to /dev/null, and the binaries are bind-mounted. CPU per request and peak memory (working set) come from the Docker API for the gateway’s container, as gwbench reads them.
Builds. All built with CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -trimpath ./cmd/gateway and Go 1.27.4. The baseline is 1622d06a, the commit before the span store, built from a clean worktree. The candidate is the working tree (T186.002), run three times: with span_store off, on with sample_rate: 1.0, and on with sample_rate: 0.1. All four processes ran at once; only the one under load was busy.
Configuration.
- All builds: each build ran its own shipped
config/gateway.yamlwith automatic migration and its one static key. Theopenaiprovider points at the mock (chat-default→m, instant).otlp_logsandotlp_tracesare off on all builds: no collector existed, and exporting to a dead endpoint would measure its retries (as in the T044 run). - Flag-on builds: they differ only by
span_store: trueand the shippedspansblock (queue 8,192, batch 500, flush 1 s, 2 connections). Atsample_rate: 1.0every request writes its spans. - The 0.1 build: at
sample_rate: 0.1the defaultkeep_always(error,fallback,guardrail_flagged,evaluated) applies, but every request is a plain 200. So about one trace in ten is written, the cost at a realistic sampling rate.
Rounds.
- Repetitions: seven. In each, every point runs every build in turn, with the order rotated between repetitions. The baseline runs twice per repetition; that A/A pair is the noise.
- Points: buffered and streamed Chat Completions at c=8 and c=32, 2.5 s apart.
gwbenchsends a whole run one way, so no mixed run was measured. c=1 is left out because on this rig it falls at random into two latency states. - Each run: 50 warmup calls, then 7,500 and 8,400 buffered requests, or 7,800 and 8,700 streamed, a window of about 6-7 s.
- Totals: 140 runs, every response 200. The tables show medians of the seven repetitions, with ranges in brackets.
| Point | Build | p50 ms | p99 ms | req/s | CPU µs/req | peak memory MB |
|---|---|---|---|---|---|---|
| buffered c=8 | before (1622d06a) |
5.82 [5.51–6.50] | 11.62 [10.62–14.06] | 1,296 [1,143–1,378] | 304 [302–309] | 33.8 [30.0–41.0] |
| buffered c=8 | span_store off |
6.03 [5.50–6.40] | 11.61 [9.68–18.60] | 1,287 [1,180–1,392] | 307 [304–308] | 33.8 [28.7–41.5] |
| buffered c=8 | on, sample_rate 1.0 |
6.22 [5.77–6.70] | 13.16 [12.27–14.97] | 1,207 [1,127–1,294] | 328 [319–331] | 35.5 [30.9–38.1] |
| buffered c=8 | on, sample_rate 0.1 |
5.96 [5.70–6.19] | 11.73 [10.98–12.79] | 1,269 [1,181–1,327] | 310 [306–314] | 32.6 [25.8–34.0] |
| buffered c=32 | before (1622d06a) |
23.13 [21.38–25.08] | 36.05 [32.80–59.88] | 1,342 [1,212–1,461] | 295 [290–300] | 36.1 [33.1–37.4] |
| buffered c=32 | span_store off |
22.10 [21.16–24.22] | 35.10 [32.19–53.28] | 1,404 [1,257–1,476] | 294 [285–299] | 37.8 [36.1–38.3] |
| buffered c=32 | on, sample_rate 1.0 |
23.66 [22.75–27.23] | 39.14 [34.90–46.07] | 1,297 [1,142–1,367] | 317 [311–319] | 36.8 [33.3–37.6] |
| buffered c=32 | on, sample_rate 0.1 |
22.64 [20.98–24.91] | 34.31 [32.72–37.77] | 1,366 [1,266–1,465] | 300 [296–302] | 33.5 [30.5–34.1] |
| streamed c=8 | before (1622d06a) |
6.56 [6.04–7.02] | 13.58 [12.49–18.55] | 1,159 [1,048–1,257] | 398 [385–402] | 33.3 [30.5–33.9] |
| streamed c=8 | span_store off |
6.59 [6.08–7.16] | 13.04 [12.34–13.87] | 1,159 [1,058–1,247] | 397 [394–400] | 32.2 [30.7–33.9] |
| streamed c=8 | on, sample_rate 1.0 |
6.92 [6.51–7.61] | 14.30 [12.70–20.58] | 1,073 [972–1,158] | 420 [415–428] | 35.4 [33.1–37.5] |
| streamed c=8 | on, sample_rate 0.1 |
6.54 [6.26–7.14] | 12.49 [11.63–14.22] | 1,168 [1,059–1,214] | 399 [398–404] | 31.7 [29.2–32.7] |
| streamed c=32 | before (1622d06a) |
24.05 [22.46–25.33] | 34.14 [30.17–37.90] | 1,303 [1,242–1,404] | 378 [375–382] | 37.3 [33.5–38.4] |
| streamed c=32 | span_store off |
24.59 [22.77–26.35] | 43.14 [32.09–53.49] | 1,255 [1,192–1,382] | 382 [377–387] | 38.5 [33.5–40.3] |
| streamed c=32 | on, sample_rate 1.0 |
25.64 [23.96–27.74] | 46.40 [38.82–68.55] | 1,177 [1,115–1,294] | 407 [406–410] | 39.2 [37.6–41.0] |
| streamed c=32 | on, sample_rate 0.1 |
24.35 [22.76–25.50] | 37.71 [33.43–64.00] | 1,277 [1,209–1,372] | 386 [383–391] | 33.9 [33.0–34.5] |
Each Δ compares a run with the first baseline run of the same repetition; the per-repetition values are then summarized the same way. The A/A column is the baseline’s second run, so it measures the noise:
| Point | Δ | A/A | off | on 1.0 | on 0.1 |
|---|---|---|---|---|---|
| buffered c=8 | p99 | +7.7% [−7.3%–+43.5%] | −1.4% [−12.1%–+52.3%] | +10.1% [+0.4%–+30.9%] | +1.0% [−10.9%–+15.3%] |
| buffered c=32 | p99 | +6.5% [−20.2%–+24.2%] | −6.1% [−28.2%–+41.2%] | +2.5% [−34.6%–+28.3%] | −9.2% [−43.7%–+10.1%] |
| streamed c=8 | p99 | −2.6% [−34.7%–+29.3%] | −4.0% [−29.6%–+3.4%] | +3.5% [−22.9%–+44.0%] | −8.0% [−33.0%–−0.6%] |
| streamed c=32 | p99 | +20.6% [+9.6%–+61.6%] | +25.6% [−1.6%–+43.0%] | +22.4% [+12.0%–+103.8%] | +10.4% [−5.8%–+83.2%] |
| buffered c=8 | p50 | +2.6% [−3.2%–+9.4%] | +0.9% [−1.5%–+9.3%] | +4.6% [+3.0%–+11.7%] | +2.4% [−8.5%–+8.1%] |
| buffered c=32 | p50 | +1.9% [−7.1%–+3.6%] | −2.2% [−7.0%–+4.7%] | +4.7% [−1.3%–+18.3%] | −1.9% [−9.7%–+8.3%] |
| streamed c=8 | p50 | +1.7% [−6.1%–+7.9%] | +0.7% [−8.2%–+2.9%] | +2.7% [+0.4%–+18.8%] | −0.6% [−4.0%–+9.8%] |
| streamed c=32 | p50 | +2.0% [−3.2%–+6.2%] | +2.5% [−2.8%–+9.4%] | +9.5% [+4.5%–+12.9%] | +2.4% [−3.0%–+7.4%] |
| buffered c=8 | req/s | −3.3% [−8.1%–−0.1%] | +1.0% [−5.4%–+3.3%] | −6.2% [−10.8%–−1.4%] | −3.7% [−5.9%–+8.3%] |
| buffered c=32 | req/s | −2.5% [−4.7%–+4.5%] | +4.2% [−7.3%–+7.5%] | −3.3% [−15.8%–+0.9%] | +0.6% [−6.6%–+12.7%] |
| streamed c=8 | req/s | −1.5% [−8.9%–+10.8%] | +0.0% [−2.1%–+10.8%] | −4.6% [−14.5%–−0.6%] | +1.1% [−5.5%–+8.2%] |
| streamed c=32 | req/s | −2.5% [−7.4%–+1.9%] | −4.3% [−10.6%–+2.8%] | −9.2% [−16.3%–−5.7%] | −2.6% [−8.5%–+3.9%] |
| buffered c=8 | CPU/req | −1.0% [−1.6%–+1.3%] | +0.3% [−1.3%–+1.7%] | +7.2% [+4.9%–+9.6%] | +1.3% [+0.0%–+3.3%] |
| buffered c=32 | CPU/req | +0.7% [−1.0%–+1.7%] | +0.7% [−2.7%–+2.4%] | +7.1% [+5.7%–+9.7%] | +2.4% [+0.0%–+3.4%] |
| streamed c=8 | CPU/req | −0.3% [−1.5%–+3.4%] | −0.3% [−0.8%–+2.9%] | +5.8% [+4.0%–+8.6%] | +0.3% [−0.3%–+3.6%] |
| streamed c=32 | CPU/req | +0.5% [−0.3%–+2.6%] | +1.0% [−0.8%–+3.2%] | +7.4% [+6.5%–+8.5%] | +2.4% [+1.0%–+3.5%] |
The writer kept up, without reading the ledger.
- Both flag-on builds: each received 228,200 requests during the measured runs, warmups included.
ledger_readsis 0 inGET /api/admin/span-store, andgateway_span_store_ledger_reads_totalagrees.dropped_queue_full,dropped_write_failedanddropped_trace_limitare 0, no batch failed, and the queue was empty after every run. - At 1.0: 228,200
writtenfor each ofrequest,routingandprovider_attempt, in 1,471 batches at 17 ms per batch on average (47 ms in the first run).gateway_spansholds 228,200 rows of each kind, andgateway_trace_summariesholds 228,200 traces. - At 0.1: 22,905
writtenand 205,295sampled_outper kind (10.0%), in 1,468 batches at 4 ms. The tables hold 22,905 rows of each kind and 22,905 traces.
PostgreSQL side check. After the rounds, at buffered c=32, with three alternations of each build:
| Build | PostgreSQL CPU µs per request | WAL records per request |
|---|---|---|
| before the span store | 658-737 (median 666) | 31-34 |
| on, 1.0 | 715-754 (median 715) | 42.5-45 (45-48 in the second run, 53-57 in the first) |
| on, 0.1 | 654-731 | 33.5-37 |
WAL syncs stayed at ~0.5 per request in every build.
Reading. On this rig, per-repetition p99 noise is much wider than 2%. The A/A pair moves p99 by −35% to +62% between repetitions. Its median Δ is −3% to +8% at three points and +21% at streamed c=32, where the baseline’s second run was slower in all seven repetitions. A 2% p99 bound can therefore be neither confirmed nor refuted from p99. The verdict rests on the medians and on the signals that repeat tightly.
-
Off is indistinguishable from the build before the span store. Every Δ sits inside the A/A spread: CPU per request −0.3% to +1.0%, throughput −4% to +4%, p50 −2% to +3%. The working set shows no consistent difference, and lifetime peak RSS is 60.6 MB against 62.4 MB.
-
On,
sample_rate1.0, no longer separates from the noise in p99. Its median p99 Δ is +3% to +22%, within about 6 points of the A/A median at every point, against +24% to +40% in the first run. It still costs something measurable:- throughput −3% to −9%, below the A/A median at every point, though less than before;
- p50 +3% to +10%;
- gateway CPU +6% to +7%, about 21-29 µs per request, above the A/A range in every repetition. This is the writer’s encoding and COPY.
PostgreSQL’s extra CPU per request fell from ~130 µs to ~50 µs.
-
On,
sample_rate0.1, shows no difference that the rig resolves in p99, p50 or throughput. Gateway CPU is +0.3% to +2.4%, about 1-9 µs per request, at the edge of the A/A range. PostgreSQL CPU and WAL are within the baseline’s spread. Its working set is lower than the baseline’s at every point, and its lifetime peak RSS is 57.9 MB.
Flag-on cost pass: where the cost was (2026-11-22, T186.002)
Section titled “Flag-on cost pass: where the cost was (2026-11-22, T186.002)”Profiles taken before the cost pass, on a separate local harness (native gateway with GOMAXPROCS=4, PostgreSQL 16.15 limited to one CPU with pg_stat_statements, 6,000 requests per run, builds side by side). They chose what the pass changed; the T172-rig run above is the measurement of record.
- Gateway: in a 20 s pprof under load the span writer is 0.8% of gateway CPU (about 6 µs per request), and span collection on the request path is below the profile’s cutoff. The rest of the flag-on CPU appears as scheduler and syscall time around database waits, not in span code.
- PostgreSQL: the largest writer cost was the trace summary upsert, one statement per trace, then the COPY, the ledger read-back and the per-trace bound read. In a microbenchmark of 54,450 rows the hexadecimal id CHECKs were about a third of insert CPU.
- After the pass: the writer’s own statement time fell from 103–135 to 46–59 µs per request at every point. What remains in the WAL (pg_walinspect) is about 3.8 heap inserts of span rows (344–464 B each), about 4.9 B-tree leaf inserts and one speculative-insert confirm per new trace. A BRIN index on
copy_idwould save about 4 WAL records and 20 µs of PostgreSQL CPU per request, but the analytics copy’scopy_id > cursor ORDER BY copy_id LIMITwould then read and sort the whole backlog on every pass, so it stays a B-tree.
With the analytics store copy (2026-11-23, T186.002)
Section titled “With the analytics store copy (2026-11-23, T186.002)”What the span copy to the analytics store adds (analytics_store_spans, analytics store), and whether it keeps up. The rig and rounds are those of the run above, with two changes:
- ClickHouse: a ClickHouse container,
clickhouse/clickhouse-server:25.12.11.4(the digest the repository pins), runs on CPU 10 in the same network namespace. Nothing is published to the host, so it is reached only over loopback. It uses the repository’s server file (deploy/helm/gateway/files/clickhouse/config.xml) and one database per mode. - Mock: the mock moved to CPUs 8-9 to free that core. PostgreSQL stays on 11.
Modes. The analytics store is enabled as shipped once it is turned on (analytics_store_usage: true, the analytics_store block pointed at ClickHouse with tls.mode: disable):
- A:
1622d06a, which copies usage only. - B: the working tree (T186.002) with
span_storeon (sample_rate: 1.0), copying usage only. - C: B with
analytics_store_spanson, so the spans are copied too.
A runs twice per repetition as the A/A pair. The points are buffered and streamed Chat Completions at c=8 and c=32, seven repetitions, 112 runs, and every response was 200.
A grant was missing. With the user and grants of the shipped users.xml, the span copy never started. Its first statement, ALTER TABLE gateway_spans_v1 MODIFY TTL …, needs ALTER TTL. That privilege is neither in the bundled user nor in the documented grant list, and the copy reported only clickhouse_server_error every 2 s. The usage copy was not affected. The run added ALTER TTL to the benchmark’s user. Fixed in T186.002 before acceptance: the shipped user now has ALTER TTL, the documented grant list names it, the copy changes the TTL only when analytics_store.spans_retention_days differs from the table’s (so an unchanged retention needs no ALTER at all), and a missing grant is reported as clickhouse_access_denied. Checked against a ClickHouse 25.12.11.4 started with the shipped users.xml.
| Point | Mode | p50 ms | p99 ms | req/s | gateway CPU µs/req | ClickHouse CPU µs/req |
|---|---|---|---|---|---|---|
| buffered c=8 | A | 6.17 [5.63–6.63] | 12.06 [10.07–14.10] | 1,211 [1,131–1,364] | 308 [306–317] | 35 [32–57] |
| buffered c=8 | B | 6.36 [5.91–7.16] | 13.27 [11.04–14.19] | 1,176 [1,057–1,296] | 334 [327–341] | 50 [37–75] |
| buffered c=8 | C | 6.46 [5.87–7.51] | 15.15 [11.36–22.27] | 1,146 [975–1,294] | 343 [333–357] | 75 [59–97] |
| buffered c=32 | A | 22.44 [21.94–24.10] | 35.72 [31.49–53.94] | 1,351 [1,251–1,420] | 300 [295–302] | 38 [23–55] |
| buffered c=32 | B | 23.86 [22.65–26.65] | 38.83 [33.79–41.97] | 1,315 [1,182–1,376] | 326 [320–331] | 37 [29–46] |
| buffered c=32 | C | 23.87 [23.53–27.25] | 50.33 [39.50–75.69] | 1,225 [1,085–1,315] | 332 [328–338] | 73 [61–94] |
| streamed c=8 | A | 6.77 [6.52–7.09] | 12.46 [11.57–14.03] | 1,131 [1,082–1,186] | 398 [390–405] | 30 [26–52] |
| streamed c=8 | B | 7.13 [6.62–7.67] | 14.44 [12.94–18.61] | 1,056 [978–1,146] | 423 [418–429] | 46 [30–55] |
| streamed c=8 | C | 7.02 [6.80–7.52] | 16.12 [14.64–18.61] | 1,069 [961–1,104] | 431 [426–441] | 81 [64–95] |
| streamed c=32 | A | 24.18 [23.39–27.41] | 35.47 [33.61–44.31] | 1,292 [1,146–1,340] | 387 [379–394] | 31 [22–68] |
| streamed c=32 | B | 25.22 [24.21–29.56] | 39.96 [35.55–62.88] | 1,231 [1,028–1,276] | 412 [407–425] | 52 [26–61] |
| streamed c=32 | C | 25.78 [25.11–28.33] | 47.15 [41.02–66.31] | 1,190 [1,069–1,233] | 421 [415–422] | 71 [65–86] |
ClickHouse CPU covers the window and the catch-up after it. In the table below, C against B is the span copy alone; each Δ is a median over the seven repetitions, with its range:
| Point | Δ p99, A/A | Δ p99, C vs B | Δ p50, C vs B | Δ req/s, A/A | Δ req/s, C vs B | Δ CPU/req, A/A | Δ CPU/req, C vs B |
|---|---|---|---|---|---|---|---|
| buffered c=8 | +7.4% [−7.9%–+17.2%] | +7.7% [−4.3%–+67.8%] | +1.7% [−5.4%–+7.3%] | −3.2% [−7.0%–+5.6%] | −2.6% [−10.6%–+4.8%] | +0.6% [−0.9%–+2.3%] | +1.8% [+0.0%–+7.9%] |
| buffered c=32 | +8.5% [−37.1%–+31.7%] | +30.6% [+10.1%–+89.2%] | +1.2% [−2.5%–+10.9%] | −1.8% [−10.5%–+4.7%] | −6.8% [−11.8%–+2.2%] | +0.3% [−0.7%–+4.7%] | +2.1% [−0.9%–+5.6%] |
| streamed c=8 | +4.5% [−7.1%–+95.3%] | +11.0% [+0.0%–+23.5%] | −0.3% [−8.4%–+5.4%] | −1.4% [−13.2%–+5.3%] | −1.0% [−7.6%–+6.9%] | −0.3% [−1.2%–+1.3%] | +1.9% [−0.2%–+4.3%] |
| streamed c=32 | +12.9% [−15.8%–+51.1%] | +12.1% [−12.6%–+86.6%] | +3.9% [−11.0%–+5.7%] | −3.7% [−10.1%–+8.4%] | −4.0% [−10.2%–+13.0%] | +0.3% [−1.8%–+2.1%] | +1.7% [−0.9%–+3.7%] |
Copy lag and completeness. The status endpoints were sampled every 2 s during each run (GET /api/admin/analytics-store for usage, the analytics_copy block of GET /api/admin/span-store for spans). After the load ended, the PostgreSQL and ClickHouse row counts were compared every ~0.5 s.
- During load:
- the usage copy’s lag peaked at 0.2-2.0 s (median 1.3 s in B and C), with up to 2,436 rows pending;
- the span copy’s lag peaked at 0.2-2.2 s (median 1.2 s), with up to 7,221 spans pending, about two seconds of spans at ~3.5 thousand per second.
- After load: ClickHouse held every usage row within 0.1-2.4 s and every span within 0.2-3.1 s (median 1.2 s). That is inside one or two copy intervals (
copy.interval: 2s). - At the end:
- Spans:
gw_c.gateway_spans_v1held 228,200 rows of each ofrequest,routingandprovider_attempt, the same withFINAL, matchinggateway_spansin PostgreSQL kind by kind. - Usage: every usage table matched its PostgreSQL source: A 456,400, B 228,200, C 228,200.
- Errors: the copies recorded no error during the runs. Both copies were
currentand reachable, with no open gaps.ledger_readswas 0 and no span batch failed.
- Spans:
- ClickHouse memory: its anonymous memory (cgroup
anon) peaked at 410-749 MiB in every mode. The medians were 672 MiB for A, 670 MiB for B and 699 MiB for C, so the span copy adds no visible memory.
PostgreSQL side check (buffered c=32, three alternations, window plus 5 s of catch-up):
| Mode | PostgreSQL CPU µs per request | WAL records per request | ClickHouse CPU µs per request |
|---|---|---|---|
| A | 667-828 | 30-35 | 51-66 |
| B | 706-805 | 42-46 | 51-70 |
| C | 708-833 | 45-47 | 58-92 |
The change-log triggers on gateway_spans fire on update and delete only, so inserts write no extra rows for the copy.
Reading.
- The copy keeps up at 1.1-1.4 thousand requests per second (3.3-4 thousand spans per second). It stays within about two seconds during load, is complete within about three seconds after it, and its row counts are exact.
- Turning the span copy on costs the request path a little, measurably.
- Gateway CPU: about +2% (6-9 µs per request), above the A/A medians at every point. This is the copy loop, which runs in the gateway process.
- p50: within the noise.
- Throughput: −1% to −7%.
- p99: +8% to +31%, beyond the A/A noise only at buffered c=32 (+31%, every repetition above zero), where PostgreSQL, this rig’s limit, also serves the copy’s reads.
- PostgreSQL: the side check does not resolve a PostgreSQL CPU difference between B and C. Its spread between alternations is wider than the effect.
- B against A repeats the span store’s own cost from the run above: CPU +7% to +9%, throughput −3% to −7%.
Relay-capacity comparisons
Section titled “Relay-capacity comparisons”Record the stream_admission flag, applied generation/body/relay limits, pool headroom, detected memory and value sources with a measurement. Disabled mode retains the legacy lifecycle; enabled mode may detach relays while holding input-body reservations. Compare equivalent modes and accounting. A constrained-container functional check or nominal 80 KiB reserve does not establish an RSS ceiling or a new throughput improvement. Final workload comparisons remain separate from integration validation.
How to reproduce
Section titled “How to reproduce”go run ./cmd/gwbench -mock 0.0.0.0:8000go run ./cmd/gwbench -config bench.json -concurrency 1,32,128 -markdown{"targets": [ {"name": "direct", "base_url": "http://127.0.0.1:18096/v1", "model": "m@300ms,20ms", "key_env": "BENCH_NONE", "container": "mock"}, {"name": "ai-gateway", "base_url": "http://127.0.0.1:18080/v1", "model": "bench-real", "key_env": "GATEWAY_KEY", "container": "gateway"}], "options": {"requests": 4096, "concurrency": 512, "rounds": 2, "warmup": 16, "stream": true, "max_tokens": 16, "prompt": "Name three colors.", "baseline": "direct"}}- Keys: only in environment variables (
key_env).headerreplacesAuthorization(Azure:api-key), andheadersadds fixed, non-secret headers. container: enables CPU and memory through the Docker API. The socket comes fromDOCKER_HOST,/var/run/docker.sock, OrbStack or Docker Desktop. A read failure shows up aserroin the table and on stderr, never as an empty cell.- Errors: the error kinds (status, transport) are listed below each table. A 200 whose body does not decode or carries an
error, a Chat answer withoutchoices, a Responses object that is neithercompletednorincompleteat the output ceiling, and a stream that ends before its dialect’s terminal event ([DONE],response.completedorresponse.incompleteformax_output_tokens,message_stop), carries an error event, or has neither text nor usage count as errors, so a cut answer never looks like a fast one.-recordrequires-commit, stored with the scenario. api: the client dialect of a target:chat(default,/chat/completions),responses(/responses, withinput,max_output_tokensandstore: false) ormessages(/messages, withanthropic-version: 2023-06-01). Output tokens come from each dialect’s own usage (completion_tokens, oroutput_tokensinresponse.completedandmessage_delta); TTFT is the first text delta.baselineper target: names the target itsoverhead p50is measured against, in place ofoptions.baseline, so a Messages route is compared with the mock called in Messages.- The mock’s dialects:
POST /v1/chat/completions,/v1/responses(the event sequence fromresponse.createdtoresponse.completed, whoseresponse.usagecarries the counts),/v1/messages(message_startwith the input usage,content_block_deltatext deltas,message_deltawith the stop reason and cumulativeoutput_tokens,message_stop) and/v1beta/models/{model}:generateContentor:streamGenerateContent?alt=sse(one chunk per token with the runningusageMetadata, events ended by CRLF CRLF as Google sends them, the finish reason on the last one). Every answer reports 8 input tokens and-mock-tokensoutput tokens and is the same for the same model; the realistic profile ism@300ms,20ms, orm--300ms--20msin a Gemini model segment. - Output:
-concurrencyrepeats the round for each level. The JSON output is a list of reports, one per level.-record docs/performance/results.json -scenario <id>writes a run into the data behind Performance.
Real providers (2026-10-26)
Section titled “Real providers (2026-10-26)”With a real provider, the network and the provider’s own variance dominate; these rounds check that the gateway adds nothing visible, not how fast it is. Streaming, 80 requests per target (160 in the focused Gemini round), the gateway in a 2-CPU container. The keys reach the container through an inherited environment variable, with no file.
OpenAI (gpt-4.1-mini):
| Target | c=1 p50 | c=1 overhead | c=8 p50 | c=8 overhead | c=8 TTFT p50 | memory |
|---|---|---|---|---|---|---|
| direct (OpenAI) | 658.7 ms | — | 648.0 ms | — | 563.4 ms | — |
| ai-gateway | 660.1 ms | +1.3 ms | 654.4 ms | +6.5 ms | 559.9 ms | 16 MB |
At concurrency 1 the provider’s variance dominates: direct itself had a p99 of 10.6 s. With 80 calls per target, to keep the cost to cents, overheads below ~10 ms are within the noise. At this low throughput, CPU per request includes the process’s idle consumption during the round, so it is not comparable with the simulated provider.
Anthropic (claude-haiku-4-5), through Anthropic’s OpenAI-compatible endpoint, with a user key and anthropic-workspace-id sent through the fixed provider headers:
| Target | c=1 p50 overhead | c=8 p50 overhead | c=8 TTFT p50 |
|---|---|---|---|
| direct (compatible endpoint) | — | — | 420.8 ms |
| ai-gateway (compatible) | +15.5 ms | +6.0 ms | 426.0 ms |
Gemini (gemini-3.5-flash-lite), focused round at concurrency 8, 160 requests and 8 rounds. Through Google’s OpenAI-compatible endpoint, the gateway tracked direct (1,038.4 ms against 1,059.9 ms at p50), but that endpoint was ~120 ms slower at p50 and ~400 ms slower at p90 than the native API (streamGenerateContent). An isolated test, without connection reuse, did not show the difference: the TLS handshake on each call masked it. The native translation was built, and p90 did not change.
Actual cause and fix: testing the direct client itself, the cause appeared: HTTP/2. With 8 streams multiplexed over one connection, Google responded ~380 ms slower at p50 than with HTTP/1.1 and one connection per stream. With automatic http_version (HTTP/1.1 for Google):
| Target | p50 | p90 | TTFT p50 | p50 overhead | CPU ms/req | memory |
|---|---|---|---|---|---|---|
| direct HTTP/1.1 | 926.3 ms | 1,151.3 ms | 889.3 ms | — | — | — |
| direct HTTP/2 | 1,308.3 ms | 1,630.7 ms | 1,262.4 ms | +382.1 ms | — | — |
| ai-gateway (native, HTTP/1.1) | 933.7 ms | 1,193.9 ms | 899.3 ms | +7.4 ms | 3.14 | 20 MB |
Bedrock (gpt-oss-20b through Bedrock’s OpenAI-compatible endpoint), concurrency 1, 100 requests per target, with streaming. Direct is signed with SigV4 by gwbench itself ("aws_sigv4": {"region": "us-east-1", "credential": "process"}).
| Target | p50 | p70 | p95 | errors |
|---|---|---|---|---|
| direct (Bedrock SigV4) | 646.6 ms | 3,992 ms | 5,391 ms | 0% |
| ai-gateway | 583.3 ms | 3,057 ms | 5,589 ms | 0% |
The provider’s variance ranges from 0.6 to 6 s and hides any gateway overhead; that is why the gateway’s own cost is measured against the simulated provider. At concurrency 8, the account quota returned 429 on ~40% of calls, the same for direct and for the gateway.
See also
Section titled “See also”- Performance: the current numbers.
- Local load test: JSON and streaming under load, with injected failures.
- Network and timeouts: admission and connection pool.