Skip to content

Add vLLM-compatible per-request metrics - #553

Closed
WynnD wants to merge 1 commit into
NVIDIA:mainfrom
WynnD:metrics/per-request-metrics
Closed

WynnD wants to merge 1 commit into
NVIDIA:mainfrom
WynnD:metrics/per-request-metrics

Conversation

@WynnD

@WynnD WynnD commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

/metrics reports server aggregates, not the timing of one request, so a client,
gateway, or billing and SLA tool cannot report prompt or decode speed per
request; timing the HTTP round trip instead mixes queueing and client transfer
into the number. vLLM answers this with a metrics object on the response,
enabled by --enable-per-request-metrics, and gateways already read it.
Emitting the same object lets those consumers read Flare responses unchanged,
and the HTTP layer already sees every offered token, so the timings are recorded
there without touching the runtime or the prepared core.

What changed

The option is --enable-per-request-metrics, or [server] per_request_metrics = true; both default to off. flare run and the desktop controller pass it to
ServerConfig, whose Bootstrap rejects a non-boolean value.

helios/src/server/http.rs:

  • RequestTiming records HTTP admission, the first and last output token, the
    first non-empty answer or reasoning frame on a stream, and the committed
    output-token count. GenerationEvent::Token and GenerationEvent::LogprobToken
    advance it; GenerationEvent::Finished adds the committed count.
  • A non-streaming OpenAI Chat or Completions response gains a top-level
    metrics object. A streaming response gains it on the chunk that reports
    finish_reason, which is the chunk a gateway parses for completion.
  • The object uses vLLM's field names: time_to_first_token_ms,
    time_to_first_content_token_ms, generation_time_ms, queue_time_ms,
    mean_itl_ms, and tokens_per_second.

Measurement contract:

  • Every interval starts at HTTP admission. The HTTP layer has no earlier
    scheduler boundary, so queue_time_ms is always null, and tokens_per_second
    includes queue and prefill time; it is a response-body timing, not the engine's
    decode rate.
  • time_to_first_content_token_ms is streaming-only and null for non-streaming
    requests, matching vLLM.
  • mean_itl_ms and generation_time_ms are null for a single-token response;
    fields are null when the request produced no such interval.
  • With n choices, one object describes the whole response.
  • Anthropic Messages and OpenAI Responses are unchanged.
  • The cost is two clock reads per output token while the option is on, and none
    while it is off.

docs/api.md documents the fields and placement; docs/cli-design.md documents
the flag and the configuration key.

Claimed result

Opt-in per-request timing in OpenAI response bodies. With the option off, the
wire format, the runtime, and the cost are unchanged; no performance claim is
made for either state.

Evidence

cargo test --locked --release --manifest-path helios/Cargo.toml --target-dir ~/.cache/hyperflare-target --features runtime-core,server --lib -- per_request_metrics passes:

  • per_request_metrics_report_vllm_timings_when_enabled: a two-token
    non-streaming chat reports a positive first-token interval, a positive
    generation interval and throughput, a finite mean inter-token latency, and
    null content-token and queue fields; the same request without the option has
    no metrics key.
  • per_request_metrics_ride_the_final_stream_chunk: a streamed chat carries the
    object on a data chunk with the content-token interval present.

cargo test --locked --release --manifest-path flare/Cargo.toml --lib -- per_request_metrics passes the settings test over absent, true, and false
[server] per_request_metrics values, and
cargo test --locked --release --manifest-path flare-cli/Cargo.toml --bin flare -- per_request_metrics passes the flag test.

The whole server:: filter ran 325 tests with one failure,
messages_image_hints_end_after_expansion_for_text_and_segmented_templates,
which needs FLARE_TEST_TEMPLATE_DIR from tests/frontend/prepare_templates.py
and has no prepared templates on this host.

Live verification on the local two-GPU DeepSeek-V4.1-Flash setup: this branch
was cherry-picked onto the validation tree that serves that model (fork commit
5ffe4d54, which that model's core requires for its engram encoding, plus #548
and #549), the image was rebuilt, --enable-per-request-metrics was added to the
llama-swap entry, and llama-swap reloaded its configuration. A non-streaming chat
with a 35-token prompt and max_tokens 16 returned metrics with
time_to_first_token_ms 4925.84, generation_time_ms 67.72, mean_itl_ms 4.51,
tokens_per_second 3.20, and null time_to_first_content_token_ms and
queue_time_ms; llama-swap recorded 7.11 prompt tokens per second and 221.5
decode tokens per second for that request. A streaming chat with
stream_options.include_usage carried metrics on its final chunk with
time_to_first_token_ms 473.78, time_to_first_content_token_ms 473.79,
generation_time_ms 1920.16, and mean_itl_ms 128.01; llama-swap recorded 7.81
decode tokens per second for it.

That validation tree also carried an unpublished local timings object, so the
gateway's numbers are not attributable to this PR alone. Where the two objects
meet, their intervals agree: this state's time_to_first_token_ms equals the
local object's prompt_ms, and mean_itl_ms equals its predicted_per_token_ms.
One limitation follows from that local object rather than from this PR: it
reports prompt_n as the uncached prompt count while llama-swap subtracts
cache_n from it again, so a fully cached stream row shows a negative prompt
rate. A tree that emits only this PR's metrics object gives llama-swap the
prompt count from usage.

Not run here: Windows builds and the full helios and flare suites, which CI
covers.

This PR does not depend on #548 or #549 and changes no code they touch; it merges
in any order with them.

Discord: oystersmiles

@WynnD

WynnD commented Nov 27, 2026

Copy link
Copy Markdown
Contributor Author

@flare-autobot review the pushed changes. The per-request metrics PR is new; the other two carry review fixes.

@WynnD
WynnD force-pushed the metrics/per-request-metrics branch from fd5c67e to d6d7c7f Compare November 27, 2026 05:02
@flare-autobot

Copy link
Copy Markdown
Collaborator

A case file has been opened for 553.
Case File Intake Report

Future reviews will auto-run 90 minutes after code updates are pushed.

  • Re-review sooner: Call @flare-autobot after push.
  • Opt-out: Include #autobot:ignore in the PR or any comment.

@flare-autobot

Copy link
Copy Markdown
Collaborator

Review Stopped

Auto-Review halted due to finding: It is low value or does not align with the project charter.
Value Proposition Report

@flare-autobot

Copy link
Copy Markdown
Collaborator

Review Stopped

Auto-Review halted due to finding: It is low value or does not align with the project charter.
Value Proposition Report

OpenAI clients and proxy gateways that display per-request speed read timings
from the response body. Flare reports aggregates on `/metrics` but no
per-request timing, so a gateway cannot show prompt or decode tokens per second,
and billing or SLA tooling has to time the HTTP round trip itself, which mixes
queueing and client transfer into the number. vLLM answers this with a `metrics`
object on the response, enabled by `--enable-per-request-metrics`, and consumers
already read it. Matching its object and placement lets those consumers read
Flare responses unchanged. The HTTP layer already sees every offered token, so
the timings are recorded there without changing the runtime or prepared core.

The option is `--enable-per-request-metrics`, or `[server] per_request_metrics =
true` in the configuration file; both default to off. `generate` records the
instant of the first and last output token, the first non-empty answer or
reasoning frame on a stream, and the committed output-token count. A
non-streaming OpenAI Chat or Completions response carries a top-level `metrics`
object; a streaming response carries it only on the final usage chunk, so the
stream needs `"stream_options": {"include_usage": true}`, as vLLM requires. With
`n` choices the object is null, again as vLLM does, because one sequence's timing
cannot describe the whole response. The object uses vLLM's field names:
`time_to_first_token_ms`, `time_to_first_content_token_ms` (streaming only),
`generation_time_ms`, `queue_time_ms`, `mean_itl_ms`, and `tokens_per_second`.

Measurement contract: every interval starts at HTTP admission. vLLM measures from
scheduler admission, which the HTTP layer cannot observe, so
`time_to_first_token_ms` here includes admission-to-scheduler time and
`queue_time_ms` is null rather than a scheduler-queue value;
`tokens_per_second` covers admission to the last output token. Any interval the
request did not produce is null, and Anthropic Messages and OpenAI Responses are
unchanged. The cost is one clock read per output token while the option is on,
and none while it is off.

The claimed result is opt-in per-request timing in OpenAI response bodies, with
no change to serving behavior, the runtime, or the wire format when the option
is off.

Evidence: `cargo test --locked --release --manifest-path helios/Cargo.toml
--target-dir ~/.cache/hyperflare-target --features runtime-core,server --lib --
per_request_metrics` passes three tests. `per_request_metrics_report_vllm_timings_when_enabled`
checks a two-token non-streaming chat: positive first-token, generation, and
throughput values, a finite mean inter-token latency, null content-token and
queue fields, and no `metrics` key when the option is off.
`per_request_metrics_ride_the_final_usage_chunk` checks that the object arrives
on the usage chunk with an empty `choices` array and the content-token interval
present, and that a stream without usage reporting carries no object.
`per_request_metrics_suppress_multi_choice_responses` checks that two choices
return a null object with both choices present. The whole `server::` filter ran
326 tests with one failure,
`messages_image_hints_end_after_expansion_for_text_and_segmented_templates`,
which needs `FLARE_TEST_TEMPLATE_DIR` from `tests/frontend/prepare_templates.py`
and has no prepared templates on this host.
`cargo test --locked --release --manifest-path flare/Cargo.toml --lib --
per_request_metrics` passes the settings test over the absent, true, and false
`[server] per_request_metrics` values, and
`cargo test --locked --release --manifest-path flare-cli/Cargo.toml --bin flare --
per_request_metrics` passes the flag test. Windows builds and live gateway
verification are left to CI and to the caller.

Signed-off-by: Wynn Drahorad <wynn.drahorad@gmail.com>
Design-Signed-Off-By: Claude Opus 5.5 (omp agent); design-review=eb2f
@WynnD
WynnD force-pushed the metrics/per-request-metrics branch from d6d7c7f to 8ffcc72 Compare November 27, 2026 19:36
@WynnD
WynnD marked this pull request as draft November 27, 2026 20:31
@WynnD

WynnD commented Nov 28, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #560. #560 builds the same vLLM metrics object from the model worker's recorded instants instead of HTTP-layer clocks, so queue_time_ms is reported, time to first token starts at scheduling as vLLM defines it, and requests with stop strings are timed correctly.

@WynnD WynnD closed this Nov 28, 2026
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.

2 participants