Count the generation prompt in local LLM input token usage #866
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
andimarafioti
Shizoqua
Fixes #865
What was wrong
With the local LLM backends (
--llm_backend transformers/mlx, and the ZeroGPU handler),usage.input_tokensinresponse.doneis lower than the number of tokens the model actually reads, on every turn.LanguageModelHandler._generateinsrc/speech_to_speech/LLM/language_model.pycounted tokens withapply_chat_template(chat_messages, tokenize=True)but generated fromapply_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._generateinsrc/speech_to_speech/LLM/zerogpu_handler.pycounted withoutadd_generation_prompt=True, whilezerogpu/_worker.pyrenders the prompt with it.Fix
Pass the same
add_generation_prompt=True, enable_thinking=Falseto 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): runsLanguageModelHandleron the transformers path with a stub ChatML-style tokenizer and a stub pipeline, and checks thatTokenUsage.input_tokensequals 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 == 6andassert 1 == 4) and pass after.The one failure is
tests/openai_realtime/test_webrtc.py::TestWebRTCLoopback::test_close_awaits_pending_ice_checks. It fails the same way on unmodifiedmainin 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/anduv run mypy src/pass.Open PR #806 edits the same
apply_chat_templateline to add tool kwargs. The changes are independent, but whichever lands second needs a small rebase