Skip to content

feat(cpp): add LLM usage stats to JSONL event output - #1206

Merged
itomek merged 1 commit into
kalin/gaia-bash-agentfrom
kalin/cpp-usage-stats
May 28, 2026
Merged

itomek merged 1 commit into
kalin/gaia-bash-agentfrom
kalin/cpp-usage-stats

Conversation

@kovtcharov-amd

Copy link
Copy Markdown
Collaborator

The --json-events answer 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 a usage object with prompt_tokens, completion_tokens, and total_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?" — verify answer event includes usage when Lemonade returns it
  • gaia-bash.exe --json-events --query "hello" — verify usage key is omitted when server returns zero tokens (graceful degradation)

Closes #1205

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 itomek left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread cpp/src/agent.cpp
Comment on lines +311 to +318
// 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};

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@itomek
itomek merged commit 9567995 into kalin/gaia-bash-agent May 28, 2026
19 checks passed
@itomek
itomek deleted the kalin/cpp-usage-stats branch May 28, 2026 13:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants