Skip to content

Record when the first token arrives and how fast a response is generated - #220

Merged
fylorn merged 1 commit into
mainfrom
feat/generation-speed
Sep 27, 2026
Merged

fylorn merged 1 commit into
mainfrom
feat/generation-speed

Conversation

@fylorn

@fylorn fylorn commented Sep 27, 2026

Copy link
Copy Markdown
Contributor

What changes

Requests now carry the moment the upstream produced its first token and a generation speed measured from there. Until now the only timing was the response headers, and the live rate (/live, shown in the menu bar) divided output by the time after the headers.

That was wrong in two ways:

  • Whole (non-streamed) responses. Their headers arrive only when generation is done, so "duration − headers" is the few milliseconds it takes to read the body. One such request could push /live to tens of thousands of tokens per second.
  • Streamed responses. Their headers usually come back as soon as the request arrives. Queueing and reading the prompt were counted as generation, so long prompts looked slow.

Protocol (version 27)

  • New event RequestFirstToken { id, ttft_ms }: the first text, reasoning or tool call in a successful streamed response. It is read in the upstream's format with the same reader the inference test uses; opening frames such as message_start and response.created do not count. It is part of the /in-flight replay.
  • RequestFinished.tokens_per_sec: the output produced after the first token, divided by the time from the first token to the end.
    • When reasoning was hidden before the first token (no reasoning text was streamed), those reasoning tokens were generated outside that time. If the upstream reports how many there were (OpenAI, Gemini), they are left out. If it does not (Anthropic), there is no rate rather than one several times too high.
    • Reasoning that is streamed as it happens is counted.
    • Whole responses, cancelled and failed requests, and generations shorter than half a second have no rate: with only a couple of chunks, the figure measures how the upstream chunks its output.
  • HistoryRow.ttft_ms and HistoryRow.tokens_per_sec.
  • New endpoints /token-rate and /token-rate/provider (TokenRateView { model, p50, samples }): the median rate by model and by upstream.
  • /latency and /latency/provider now report first-token time. Whole responses have no first token and are left out.
  • /live averages the per-request rates weighted by generation time.

Request store

Two new columns, ttft_ms and tokens_per_sec. The rate is fixed when the request is recorded, because leaving out hidden reasoning needs what only the gateway saw in the stream. Schema 21 → 22, so an existing request history is rebuilt empty on upgrade.

Unchanged

url-test still ranks upstreams by response-header time. That is a routing decision made before any output exists. The inference test (/speed/run) is unchanged.

Tests

  • tw-gateway/tests/first_token.rs runs a real gateway against real upstreams:
    • a stream whose headers come before a 300 ms prefill reports its first token after the prefill, and its rate is counted from there;
    • a whole response has neither a first token nor a rate;
    • a Chat Completions stream with 2000 hidden reasoning tokens gets a rate from the 20 visible ones.
  • ending.rs unit tests cover what counts as the first token (not the opening frames; a tool call counts when it starts), and every branch of the rate rule.
  • bus.rs: the live rate is weighted by generation time, and requests without a rate (whole responses, cancelled ones) are left out.
  • recorder.rs and db.rs: both fields are recorded (a cancelled stream keeps its first token but gets no rate); latency uses the first token; the median rate is given by model and by upstream.

cargo fmt --all --check, cargo clippy --workspace --all-targets -- -D warnings (and --lib), and cargo test --workspace all pass locally.

🤖 Generated with Claude Code

…ponse is generated

Requests now carry the moment the upstream produced its first token and a
generation speed measured from there, instead of timing generation from the
response headers.

- New event `RequestFirstToken { id, ttft_ms }`: the first text, reasoning or
  tool call in a successful streamed response, read in the upstream's format
  (opening frames such as `message_start` do not count).
- `RequestFinished.tokens_per_sec`: output after the first token divided by
  the time from the first token to the end. When reasoning was hidden before
  the first token, the reported reasoning tokens are left out; when the
  upstream does not report how many there were, there is no rate. Whole
  (non-streamed) responses, cancelled and failed requests, and generations
  shorter than half a second have no rate.
- The request store keeps `ttft_ms` and `tokens_per_sec` (schema 22), and
  `HistoryRow` carries both.
- New endpoints `/token-rate` and `/token-rate/provider`: the median rate by
  model and by upstream, with sample counts.
- `/latency` and `/latency/provider` now report the first-token time; whole
  responses have no first token and are left out.
- `/live` averages the per-request rates weighted by generation time. It used
  to divide output by the time after the response headers, so one whole
  response (headers arrive only when generation is done) could push the
  figure to tens of thousands of tokens per second.

Control-plane protocol version 27.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@fylorn
fylorn merged commit 82b7414 into main Sep 27, 2026
4 checks passed
@fylorn
fylorn deleted the feat/generation-speed branch September 27, 2026 17:10
@fylorn fylorn mentioned this pull request Sep 27, 2026
fylorn added a commit that referenced this pull request Sep 27, 2026
Minor release for #220.

- Bump the workspace version to 0.54.0
- release-notes/0.54.0.md

Control-plane protocol 27 (was 26): the RequestFirstToken event,
RequestFinished.tokens_per_sec, HistoryRow.ttft_ms and tokens_per_sec, and
the /token-rate endpoints. The request store schema is now 22 (was 21), so
the request history of an earlier version is emptied on the first start.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant