fix: record streaming token usage in v2 profile stats#21
Open
himorishige wants to merge 1 commit into
Open
Conversation
Signed-off-by: Hiroshi Morishige <hiroshi.morishige@gmail.com>
Contributor
|
@himorishige Thanks for putting up the PR. Can you please rebase and resolve the merge conflicts once |
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
Adds a native v2 stream tap (
switchyard-components-v2/src/stats/stream_tap.rs) and wires it into all five v2 profiles (passthrough, random-routing, llm-routing, cascade, latency-service), so/v1/routing/statsrecords token usage for streaming responses. Fixes the108 calls / 0 tokensshape reported in #18.Root cause
All v2 profiles read usage from the buffered body only:
ChatResponse::body()returnsNonefor the three stream variants, so streaming usage silently defaulted to zero whilerecord_successstill counted the call.Approach
Per the guidance in #18, this does not wire in the v1
StatsResponseProcessor(it depends onProxyContextstate that v2 doesn't have). Instead:UsageAttributioncaptures what v2 already holds locally before the stream is consumed: the stats accumulator, model, tier, request-startInstant, and backend latency.record_usage_or_tap_stream(response, attribution)records buffered bodies immediately and wraps the three stream variants so usage is committed once when the stream completes.switchyard-components::statsparsers (openai_chat_usage_from_stream_event,openai_responses_usage_from_stream_event,AnthropicStreamUsage).record_usage_after_success_attribution, so the tier call counted byrecord_successis not double-counted.Profile wiring stays local: passthrough / random-routing / latency-service replace the inline usage read; llm-routing / cascade change
record_successto take the response by value and return the (possibly wrapped) response. The cascadeenable_stats=falseguard is preserved (no tap when stats are disabled).Semantics notes (for review)
stream_options.continuous_usage_stats) update every chunk, so committing the first frame would under-count.record_classifier_usagestill reads the buffered body).Testing
cargo fmt --all --check,cargo clippy --workspace --all-targets -- -D warnings, andcargo test --workspaceall pass (toolchain 1.94.0)./v1/routing/statsrecords zero token usage for streaming responses (v2 profile server) #18 reproduction (passthrough profile → OpenRouter, Linux aarch64):"stream": true"stream": true+stream_options.include_usage(deltas are
total_tokensin/v1/routing/statsper request; absolute counts vary with model output length)Closes #18