Repository navigation
Closed
Labels
area:architectureArchitecture and code structure changespriority:lowLow prioritystatus:doneCompletedtype:bugBug fixes, error corrections, or issue resolutions
Description
Problem / Background
The disaggregated router's /v1/chat/completions path never emits a usage object, diverging from the single-node chat route which reports usage in both streaming and non-streaming responses. PR #392 (issue #387) made the router carry the worker's authoritative generated-token count over the wire and fixed the chat finish_reason flip for byte-fallback tokenizers, but it intentionally scoped the chat change to finish_reason only. The authoritative count is now resolved in the chat path and then discarded, so chat clients still get no token accounting from the router.
Current gap (verified against main, 0add095)
- Non-streaming chat:
route_chatresolves the authoritative count viaresolve_completion_tokensatsrc/server/router_front.rs:804, but only forfinish_reason. It then builds the response withchat_completion_json(src/server/router_front.rs:1469), which returns{id, object, model, choices}with nousagefield. - Streaming chat: the streaming branch of
route_chat(starting atsrc/server/router_front.rs:676) never consultsstream_options.include_usageand never emits a usage chunk, unlike the router's own/v1/completionsstreaming path (include_usageatsrc/server/router_front.rs:933, usage chunk emission gated at:1005). - The prompt token count is already available but unused:
start_handoffreturns it as_prompt_tokensatsrc/server/router_front.rs:673.
Single-node reference (parity target)
src/server/routes/chat.rsreportsusagewithprompt_tokensin the non-streaming response (around:491-515) and emits a usage chunk gated byinclude_usagein the streaming path (:883-893, viaChatCompletionChunk::usage_with_cache).
Proposed direction
- Non-streaming: extend
chat_completion_json(or its caller) to include ausageobject withprompt_tokens(from the now-unused_prompt_tokens),completion_tokens(from the already-resolvedresolve_completion_tokensvalue), andtotal_tokens, matching the single-nodeChatCompletionResponseshape. - Streaming: honor
stream_options.include_usagein the chat streaming branch and emit a final usage chunk on success, mirroring both the router's existing/v1/completionsstreaming usage emission and single-node chat. - Reuse the existing
resolve_completion_tokens(src/server/router_front.rs:1249) socompletion_tokensstays authoritative (correct for byte-fallback tokenizers like Gemma) and clamped tomax_tokens.
Acceptance Criteria
- The router non-streaming
/v1/chat/completionsresponse includes ausageobject matching the single-node shape (prompt_tokens,completion_tokens,total_tokens). - The router streaming
/v1/chat/completionshonorsstream_options.include_usageand emits a final usage chunk when requested, with no usage chunk on a handoff failure (matching the existing completions-path behavior). -
usage.completion_tokensuses the authoritative resolved count and matches single-node for a byte-fallback tokenizer. - An E2E test asserts chat
usageparity between the router and single-node on a byte-fallback tokenizer (extend the existingtests/disaggregated_router_e2e.rsharness; it may be gated behind the same checkpoint-presence guard as the other real-model tests).
References
- Surfaced in the PR fix(router): use worker's authoritative token count for usage #392 review of issue fix(router): carry the worker's true generated-token count over the disaggregated wire protocol #387 (MEDIUM finding: router chat omits
usage). - Code:
src/server/router_front.rs(route_chat,chat_completion_json,resolve_completion_tokens, and the/v1/completionsusage emission for reference);src/server/routes/chat.rs(single-node usage reference).
Metadata
Metadata
Assignees
Labels
area:architectureArchitecture and code structure changespriority:lowLow prioritystatus:doneCompletedtype:bugBug fixes, error corrections, or issue resolutions
Activity
test(router): cover the chat streaming include_usage default gate
fix(router): emit usage on the disaggregated /v1/chat/completions res…
fix(router): emit usage on the disaggregated /v1/chat/completions res…