← Back to Pull requests

Count the generation prompt in local LLM input token usage #866

Merged
Shizoqua merged 4 files (104 lines) on Nov 25, 2026 15:30 UTC
View on GitHub
Shizoqua Shizoqua commented on Nov 22, 2026 15:05 UTC
Author

Fixes #865

What was wrong

With the local LLM backends (--llm_backend transformers / mlx, and the ZeroGPU handler), usage.input_tokens in response.done is lower than the number of tokens the model actually reads, on every turn.

LanguageModelHandler._generate in src/speech_to_speech/LLM/language_model.py counted tokens with apply_chat_template(chat_messages, tokenize=True) but generated from apply_chat_template(chat_messages, tokenize=False, add_generation_prompt=True, enable_thinking=False). The count left out the assistant generation prompt, and for Qwen3 the empty think block.

ZeroGPULLMHandler._generate in src/speech_to_speech/LLM/zerogpu_handler.py counted without add_generation_prompt=True, while zerogpu/_worker.py renders the prompt with it.

Fix

Pass the same add_generation_prompt=True, enable_thinking=False to the tokenizing call, so the count matches the prompt the model receives. Nothing sent to the model changes.

Tests

  • tests/test_local_llm_token_usage.py (new): runs LanguageModelHandler on the transformers path with a stub ChatML-style tokenizer and a stub pipeline, and checks that TokenUsage.input_tokens equals the token count of the prompt the pipeline received.

  • tests/test_zerogpu_llm.py: checks that the ZeroGPU handler counts the generation prompt the worker sends.
  • Both fail before the fix (assert 3 == 6 and assert 1 == 4) and pass after.

    uv run pytest tests/test_local_llm_token_usage.py tests/test_zerogpu_llm.py tests/test_local_llm_latency.py -q
    19 passed
    
    uv run pytest tests/ -q
    4061 passed, 13 skipped, 1 failed
    

    The one failure is tests/openai_realtime/test_webrtc.py::TestWebRTCLoopback::test_close_awaits_pending_ice_checks. It fails the same way on unmodified main in my environment (no usable network interfaces for the aioice loopback check), so it is unrelated.

    uv run ruff check src/ tests/, uv run ruff format --check src/ tests/ and uv run mypy src/ pass.

    Open PR #806 edits the same apply_chat_template line to add tool kwargs. The changes are independent, but whichever lands second needs a small rebase

    Diff: 104 lines (+102 / -2) across 4 files Reviewer: andimarafioti Review duration: 4345m
    All Vouch review confidence checks passed
    Residual risk 0.34 = Change Risk (0.63) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.63 · 104 lines diff across 4 files
    0.63
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    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
    Shallow Review Depth shallow_review_depth
    0.344
    Diff Size diff_size
    0.278
    Sensitive Paths sensitive_paths
    0.063
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    1 item
    "Review active on #866"
    general weight: 0.35
    Reviewers
    Assignees
    Shizoqua Shizoqua
    Labels
    risk: low confidence: 46.7% 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: 63.4% (MEDIUM) · Higher = larger blast radius
    63.4%
    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: 46.7% · Lower = rushed or shallow review
    46.7%
    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: 33.8% · Safe to merge
    33.8%