Skip to content

fix(router): emit usage on the disaggregated /v1/chat/completions responses (streaming and non-streaming) #398

Closed

Description

@inureyes

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_chat resolves the authoritative count via resolve_completion_tokens at src/server/router_front.rs:804, but only for finish_reason. It then builds the response with chat_completion_json (src/server/router_front.rs:1469), which returns {id, object, model, choices} with no usage field.
  • Streaming chat: the streaming branch of route_chat (starting at src/server/router_front.rs:676) never consults stream_options.include_usage and never emits a usage chunk, unlike the router's own /v1/completions streaming path (include_usage at src/server/router_front.rs:933, usage chunk emission gated at :1005).
  • The prompt token count is already available but unused: start_handoff returns it as _prompt_tokens at src/server/router_front.rs:673.

Single-node reference (parity target)

  • src/server/routes/chat.rs reports usage with prompt_tokens in the non-streaming response (around :491-515) and emits a usage chunk gated by include_usage in the streaming path (:883-893, via ChatCompletionChunk::usage_with_cache).

Proposed direction

  • Non-streaming: extend chat_completion_json (or its caller) to include a usage object with prompt_tokens (from the now-unused _prompt_tokens), completion_tokens (from the already-resolved resolve_completion_tokens value), and total_tokens, matching the single-node ChatCompletionResponse shape.
  • Streaming: honor stream_options.include_usage in the chat streaming branch and emit a final usage chunk on success, mirroring both the router's existing /v1/completions streaming usage emission and single-node chat.
  • Reuse the existing resolve_completion_tokens (src/server/router_front.rs:1249) so completion_tokens stays authoritative (correct for byte-fallback tokenizers like Gemma) and clamped to max_tokens.

Acceptance Criteria

  • The router non-streaming /v1/chat/completions response includes a usage object matching the single-node shape (prompt_tokens, completion_tokens, total_tokens).
  • The router streaming /v1/chat/completions honors stream_options.include_usage and emits a final usage chunk when requested, with no usage chunk on a handoff failure (matching the existing completions-path behavior).
  • usage.completion_tokens uses the authoritative resolved count and matches single-node for a byte-fallback tokenizer.
  • An E2E test asserts chat usage parity between the router and single-node on a byte-fallback tokenizer (extend the existing tests/disaggregated_router_e2e.rs harness; it may be gated behind the same checkpoint-presence guard as the other real-model tests).

References

Activity

  1. added
    type:bug
    priority:low
    area:architecture
    status:backlog
    on Jun 22, 2026
  2. added
    status:in-progress
    status:review
    and removed
    status:backlog
    status:in-progress
    on Jul 9, 2026
  3. added
    status:done
    and removed
    status:review
    on Jul 9, 2026
  4. added a commit that references this issue on Jul 9, 2026
    ad08899
  5. self-assigned this
    on Aug 31, 2026
  6. added a commit that references this issue on Oct 27, 2026
    53e8273
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area:architecturepriority:lowstatus:donetype:bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions