t3code icon indicating copy to clipboard operation
t3code copied to clipboard

[Bug]: Claude turnTokenUsage misses the calls made before a message delivered mid-turn

Open creedants opened this issue 1 month ago • 3 comments

Before submitting

  • [X] I searched existing issues and did not find a duplicate. Related but distinct: #15429 (no token usage for subagents), #16148 (Cursor threads report no usage).
  • [X] I included enough detail to reproduce or investigate the problem.

Area

apps/server

Steps to reproduce

  1. Start a turn on a Claude-provider thread that makes several model calls (a few tool calls is enough).
  2. While the turn is running, send the thread another message so it is delivered into the running turn (steered, not queued for a new turn).
  3. Let the turn finish.
  4. Compare the turn's turnTokenUsage with the sum of per-call usage in the Claude session log for the same turn, counting each message.id once.

Expected behavior

turnTokenUsage holds the total for every model call in the turn, as it does for a turn that receives no message while running.

Actual behavior

turnTokenUsage holds only the calls made after the mid-turn message was delivered. The calls before it are missing from the turn's total.

Measured over 2,494 finished Claude turns on one machine, joined to the Claude session log by the native session id:

  • 2,418 turns match the log exactly on input and output tokens.
  • In 62 turns, turnTokenUsage equals the sum of a suffix of the turn's calls exactly. In 61 of those the log entry just before that suffix is a dequeued user message. In the remaining one it is a compaction boundary.
  • Those 62 turns account for 3.79 percent of all Claude input tokens in the sample. Long-running threads that get steered often are hit hardest: 19 of 899 threads show under half of their real total.

For comparison, Codex turns matched their rollout log in 428 of 428 turns and OpenCode in 91 of 91 threads, so this looks specific to how the Claude turn total is read. The total appears to come from the SDK's final result.usage, which would explain it if the SDK starts a new result after a message is delivered mid-turn.

Impact

Token and cost totals for Claude threads read low, by a few percent overall and by more than half on some threads. Anything that budgets or reports spend from turnTokenUsage undercounts exactly the long, steered turns that cost the most.

Version or commit

0.0.46 nightlies from 2026-10-03 to 2026-10-10. Not rechecked on later builds.

Environment

Linux x86_64 desktop build, Claude provider.

Workaround

Sum per-call usage from the Claude session log, counting each message.id once, instead of reading turnTokenUsage for Claude threads.

creedants avatar Oct 11 '26 15:10 creedants

[!NOTE] Grok responding on behalf of Julius.

Confirmed on main: Claude turnTokenUsage is taken only from the final SDK result.usage, so a mid-turn steered message undercounts the turn.

ClaudeAdapterV2.finalizeActiveTurn sets turnTokenUsage via normalizeClaudeTurnTokenUsage(input.result, …). When a turn is steered, an aborted_streaming / aborted_tools result is ignored so the turn can continue; the later success result is what we store. That success result’s usage covers only the calls after the steer (the SDK starts a new result segment; our message_steering replay shows num_turns: 1 and a single post-steer iteration). Unlike Codex, Claude has no turn-level accumulator, so pre-steer usage is dropped. The live context meter updates from assistant frames; billed turnTokenUsage does not.

Not a duplicate of #15429 (subagents) or #16148 (Cursor). The SDK segment reset is upstream; mapping only the last segment onto turn-scoped totals is on us. A fix would sum usage across the aborted segment(s) and the final result (or accumulate per-call usage) before writing turnTokenUsage.

juliusmarminge avatar Oct 11 '26 15:10 juliusmarminge

I'd like to take this one — working on a fix now.

Confirmed the root cause on main:

  • apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts finalizeActiveTurn writes turnTokenUsage from a single final input.result via normalizeClaudeTurnTokenUsage.
  • Steering results (aborted_streaming / aborted_tools) from isClaudeActiveSteeringAbortResult are intentionally ignored so the turn can continue, and their usage is dropped — the later success result only covers post-steer calls.
  • apps/server/src/provider/ClaudeTurnTokenUsage.ts has no cross-segment accumulation (unlike the Codex adapter's turn-level accumulator).

Plan: add a turn-level usage accumulator in the Claude adapter that folds usage from every SDK result (including aborted steering results), and have finalizeActiveTurn prefer the accumulator so non-steered turns stay unchanged.

AI-assisted contribution disclosure: this fix and the accompanying tests are being authored with the help of an AI coding agent (account owner supervising, tests will actually run).

akushonkamen avatar Oct 11 '26 16:10 akushonkamen

Opened PR #18299 with the fix — mid-turn steered turns now bill the accumulated usage across all segments, verified red→green on the new claude_steering_usage replay fixture (details in the PR description).

akushonkamen avatar Oct 11 '26 18:10 akushonkamen