Repository navigation
fix(llm): keep the cached-token count the server reports - #4032
kovtcharov wants to merge 3 commits into
Conversation
Every token figure GAIA reports treats a cached input token as a fresh one, because the provider copied four fields out of the completion's usage and dropped prompt_tokens_details.cached_tokens. A benchmark measurement put the cache hit rate at about 95% within a task, so the cost shown for a run is an upper bound with no way to tell how far off it is. The count now rides with the rest of the usage, per step, and _sum_cached_tokens totals it for a turn. Both spellings are read: OpenAI's prompt_tokens_details.cached_tokens and a flat cached_tokens. A backend that does not cache reports zero.
Request changesThe PR captures the cached-token count from each completion, which is the right half of the fix — but nothing ever reads it back out. The per-turn total the description promises is written as a helper, tested, and then never called, so To finish it, the new per-turn sum needs to be called where the other token totals are computed at the end of a query, and its result carried in the returned totals alongside input and output tokens — otherwise the count stops at the provider and never reaches anyone. Two smaller things worth a look:
The test coverage for what is implemented is good, and the two response shapes plus the no-cache case are all handled. Real-world evidenceN/A — no evidence bundle was produced for this run, and no CLI/API output is attached to the PR. The three test-plan boxes are all unchecked, including the 🔍 Technical detailsIssues🟡
…then add 🟢
🟢 Streaming turns still report no cached prefix (
Strengths
|
|
🟡 The previous review's central concern is still open: The fix is two lines — call the function and include its result alongside the other totals. 🔍 Technical details
Then add to the result dict (~line 7522):
|
|
Refreshed this branch against current The update was a clean merge: nothing was resolved by hand, and no commit on this branch was rewritten. CI now runs against an up-to-date base. |
|
Not merging this — The PR description called this: "#3739 adds a function of the same name for the TUI's session cost line; if that lands first, the two should be merged into one." #3739 landed first, and it shipped the whole thing — the server-reported cached count is captured per call, summed per turn, and actually displayed. The two review rounds asking for the missing wiring were answered by that PR, not this one. Recommend closing as superseded. 🔍 Technical detailsVerified against Already on main, wired end to end:
Three ways merging this branch would regress main:
The only thing here with no counterpart on main is the flat top-level No eval run: the change is token-usage bookkeeping. It touches no system prompt, tool schema, error classification, default model, or tool-call parsing, so none of the CLAUDE.md eval triggers apply. Moot regardless, since the recommendation is to close. |
GAIA prices every input token as if it were fresh. The provider copied four fields out of each completion's usage and dropped the count of tokens the server served from its cache, so the cost and token totals users see are upper bounds with no way to tell how far off they are. A benchmark measurement put the cache hit rate at roughly 95% within a task, which makes the gap large. The count is now recorded per step and totalled per turn.
Test plan
python -m pytest tests/unit/test_agent_token_usage.pypasses. On main the 6 new tests fail.prompt_tokens_details.cached_tokens, one with a flatcached_tokens, and one with neither are all handled.gaia statsand any cost display still work for a backend that reports no cache (reads as 0, not as unknown).🔍 Technical details
LemonadeProvider._capture_usage()now holds the usage-capture logic that was inline, and addscached_tokens.get_performance_stats()already copies_last_usagewholesale, so the count reaches each step'sperformance_statsentry and from there the conversation._sum_cached_tokens(conversation)totals it alongside the existing_sum_conversation_tokens. #3739 adds a function of the same name for the TUI's session cost line; if that lands first, the two should be merged into one.