← Back to Pull requests

Build token timeline from returned token ids #1085

Closed
unknown closed this pull request without merging on Nov 26, 2026 21:32 UTC
View on GitHub
unknown unknown commented on Nov 13, 2026 18:08 UTC
Author

Part of #917

The token timeline of a streamed response is built by re-tokenizing the text of each chunk. Tokens the server's tool parser swallows never reach the client as text, so they are missing from the timeline. The client/server count check on tool turns runs with a tolerance for that reason.

With api.return_token_ids: true the client sends return_token_ids and builds the timeline from the ids each chunk carries. Every generated token gets an arrival time, swallowed ones included. The count check on those responses is exact, tool turns included. A response that carries no ids falls back to the re-tokenized timeline. output_len is unchanged.

Changes:
Add api.return_token_ids, default false. Streaming chat and completion only. Other APIs and unary are rejected at config load.
Send return_token_ids in the request body when the flag is on.
Record one arrival time per returned token id in the SSE parser.
Use the id timeline for output_token_times in the chat, completion and replay graph session paths when ids came back. Mark the response with token_ids_returned.
Skip re-tokenizing and the tool-turn tolerance in the count check when ids came back.
Add requests_with_token_ids to the summary, so a zero mismatch count says which check produced it.
Document the flag in docs/config.md and docs/metrics.md.

Other changes:
The fake OpenAI server in the integration tests can stream tool calls, chunks with no text, and token ids.

  • The mocked api config in test_openai_client.py sets return_token_ids = False. A MagicMock attribute is truthy, which sent the field on the embeddings and rerank round trips.
  • vLLM puts the ids of the whole prompt on the first chunk when the field is set. The first chunk grows with prompt length.

    pdm run validate, pdm run test and pdm run check:cov ran clean. e2e/tests/test_vllm_cpu_token_ids.py ran clean against vLLM v0.33.0 on CPU for chat and completion. Its tool-call case ran clean against the same release serving Qwen/Qwen3-0.6B with the hermes tool parser. That case needs a tool-capable server, so it skips in CI.

    This PR was written in part with the assistance of generative AI.

    Diff: 274 lines (+205 / -69) across 3 files Reviewer: rishabhsinha17 Review duration: 18924m
    All Vouch review confidence checks passed
    Residual risk 0.25 = Change Risk (0.63) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.63 · 274 lines diff across 3 files
    0.63
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.336
    Shallow Review Depth shallow_review_depth
    0.289
    Sensitive Paths sensitive_paths
    0.063
    Real PR Conversation Timeline
    1 comments
    "Review active on #1085"
    general weight: 0.35
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed sensitive_module
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 62.7% (MEDIUM) · Higher = larger blast radius
    62.7%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 24.8% · Safe to merge
    24.8%