Repository navigation
Record when the first token arrives and how fast a response is generated - #220
Merged
Merged
Conversation
…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>
Merged
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
/liveto tens of thousands of tokens per second.Protocol (version 27)
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 asmessage_startandresponse.createddo not count. It is part of the/in-flightreplay.RequestFinished.tokens_per_sec: the output produced after the first token, divided by the time from the first token to the end.HistoryRow.ttft_msandHistoryRow.tokens_per_sec./token-rateand/token-rate/provider(TokenRateView { model, p50, samples }): the median rate by model and by upstream./latencyand/latency/providernow report first-token time. Whole responses have no first token and are left out./liveaverages the per-request rates weighted by generation time.Request store
Two new columns,
ttft_msandtokens_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-teststill 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.rsruns a real gateway against real upstreams:ending.rsunit 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.rsanddb.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), andcargo test --workspaceall pass locally.🤖 Generated with Claude Code