Repository navigation
feat(cpp): add LLM usage stats to JSONL event output - #1206
Conversation
The --json-events answer event now includes a usage object with prompt_tokens, completion_tokens, and total_tokens — accumulated across all LLM calls in a query. The TUI/WebUI can display token consumption without a separate stats endpoint.
itomek
left a comment
There was a problem hiding this comment.
Reviewed statically (no C++ toolchain here to build). The handler-level change is clean: UsageStats accumulates correctly via totalUsage += llmResult.usage, the answer event gains a well-formed usage object, and the two new gtests assert presence + correct sums and omission-on-zero. Approving. Two non-blocking follow-up notes left inline (a latent issue on the streaming usage path, and a coverage gap for the multi-step accumulation claim) — both fine to handle in a follow-up.
Generated by Claude Code
| // Streaming responses may include usage in the final chunk; | ||
| // attempt to extract from the raw bytes. | ||
| UsageStats usage; | ||
| try { | ||
| const json responseJson = json::parse(rawResponse); | ||
| usage = extractUsage(responseJson); | ||
| } catch (...) {} | ||
| return {accumulated, usage}; |
There was a problem hiding this comment.
Non-blocking follow-up: on the streaming path rawResponse is the concatenated SSE byte stream (data: {…}\n\n repeated, ending with data: [DONE]), not a single JSON document — so json::parse(rawResponse) throws and is swallowed by the catch (...) {} on 317, meaning streaming queries will always emit zero usage and the usage field is silently omitted. The OpenAI final usage chunk also carries an empty choices array, which the SSE parser discards. If --json-events always runs with streaming=false, this branch is effectively dead and worth dropping (or marking unreachable); otherwise usage needs to be extracted per-chunk inside the SSE parser. Fine as a follow-up.
Generated by Claude Code
| EXPECT_FALSE(ev.contains("usage")); | ||
| } | ||
|
|
||
| TEST(JsonEventHandlerTest, FinalAnswerWithUsage) { |
There was a problem hiding this comment.
Non-blocking follow-up: these two tests cover the leaf handler (printFinalAnswer), but the PR's headline behavior — usage accumulated across all LLM calls in a multi-step query (totalUsage += llmResult.usage in agent.cpp) — has no coverage. An agent-level test with a mock LLM returning usage on two steps, asserting result["usage"]["total_tokens"] equals the sum, would catch a future per-step reset/regression. Worth a follow-up.
Generated by Claude Code
The
--json-eventsanswer event was missing token usage data, so the TUI/WebUI had no visibility into how many tokens each query consumed. Now the answer event includes ausageobject withprompt_tokens,completion_tokens, andtotal_tokens— accumulated across all LLM calls in a multi-step query — so the frontend can render token consumption directly from the event stream.Test plan
tests_mock --gtest_filter="JsonEventHandlerTest.*"— all 23 tests pass (2 new:FinalAnswerWithUsage,FinalAnswerZeroUsageOmitted)gaia-bash.exe --json-events --query "what is 2+2?"— verifyanswerevent includesusagewhen Lemonade returns itgaia-bash.exe --json-events --query "hello"— verifyusagekey is omitted when server returns zero tokens (graceful degradation)Closes #1205