Skip to content

Claude Code context usage is only emitted at final result, leaving the meter stale during tool loops #3220

Description

@dfaivre-pcs

Summary

Claude Code context usage remains unavailable or stale throughout a multi-step agent turn, even while assistant responses and tool calls complete. bb already extracts per-request context counts from assistant messages, but only emits the context-window snapshot when it processes the final result. Emit updates during the turn when those counts arrive.

Versions and environment

  • bb CLI 0.42.1; local npm-installed bb-app on Linux/WSL.
  • Provider: claude-code; installed Claude CLI 2.1.263 (the CLI version does not establish the bundled SDK version).
  • Source inspected at 06aeaa994942ae7527dc49d2268c1f801e8542a0.
  • Observed in an active personal-workspace thread and an active managed-worktree thread. A local stats plugin reads bb's public thread-event API; the missing data is confirmed directly through that API.
  • Existing live threads were inspected; a fresh controlled reproduction on this source commit has not been run.

Steps to reproduce

Suggested minimal reproduction (not separately executed):

  1. Start a Claude Code thread with a task requiring several sequential model/tool cycles, for example: Inspect this repository, run its checks, investigate failures, and summarize what you find.

  2. While assistant messages and tool calls are completing, query the events on the running bb server:

    curl -sS 'http://127.0.0.1:38886/api/v1/threads/<thread-id>/events?types=thread%2FcontextWindowUsage%2Fupdated,thread%2FtokenUsage%2Fupdated,turn%2Fcompleted&order=desc&limit=20'
  3. Compare with turn/started and item/completed events. Check again after the provider's final result.

Expected vs actual

Observed on thr_j3r5kg9m79 while active:

  • One turn/started at sequence 9.
  • The newest 100 events included 18 item/completed, 17 item/started, and 8 item/agentMessage/delta events.
  • The filtered context/token-usage/turn-completed query above returned exactly [].
  • A second active Claude thread, thr_ra6tsbrfyy, likewise had no context or token-usage events during its first ongoing turn.

Expected: context-window updates during the turn when the SDK provides a request usage sample, without waiting for the entire agent run to finish. This need not be a token-by-token counter. Missing readings should remain unknown; cumulative tokens must not be substituted for context occupancy.

Evidence

The source suggests the reporting boundary is the cause:

Suggested direction: emit a context-window delta when a valid top-level assistant request sample arrives, preserving model capacity, estimated status, and turn attribution. Do not count subagent context as the parent context. Add a regression test that observes context events before the final result.

Investigation thread (reporter's local bb instance): http://127.0.0.1:38886/threads/thr_68imcxrdav . All relevant observations are included above so access to that instance is unnecessary.

What was ruled out

  • Not merely a rendering or polling problem: the usage events themselves are absent from the public event API.
  • Completed assistant messages/tool calls are not being mistaken for a persisted turn/completed: the latter was explicitly queried and absent.
  • Searched open and closed issues. Pi context-window usage stays stale during multi-tool turns #2023 describes the same reporting cadence problem for Pi and is closed; this report concerns Claude Code's separate adapter.
  • No claim that the provider lacks intermediate usage: the adapter already extracts it from assistant messages.

Suggested priority and effort

Medium: context pressure is invisible during long Claude tool loops, especially a first run or one extended by steering. Waiting for a final result is a workaround; no data loss was established.

AGENT GENERATED

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-reproBug reproduced again from a clean trusted checkout; see linked reportprovider-claude-codeBuilt-in plugin: provider-claude-codeprovidersCross-provider bridges, models, login

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions