Skip to content

acp_thread: Forward token usage from PromptResponse to thread UI - #53693

Closed
hahahuy wants to merge 2 commits into
zed-industries:mainfrom
hahahuy:acp-token-usage-display
Closed

acp_thread: Forward token usage from PromptResponse to thread UI#53693
hahahuy wants to merge 2 commits into
zed-industries:mainfrom
hahahuy:acp-token-usage-display

Conversation

@hahahuy

@hahahuy hahahuy commented Apr 11, 2026

Copy link
Copy Markdown

Fixes #53130

Summary

External ACP agents (e.g. Claude Code CLI) return token usage in PromptResponse via the unstable_session_usage feature, which Zed already enables. Previously this data was discarded — token_usage stayed None on the AcpThread, so render_token_usage() returned early and showed nothing.

Changes:

  • acp_thread.rs: Extract r.usage in the Ok(r) branch of run_turn and call update_token_usage() each turn.
  • thread_view.rs: Add a max_tokens == 0 branch in render_token_usage — since the ACP protocol does not expose context window size, render a plain "N tokens" label instead of a circular progress ring (which would show meaningless "0% • N / 0").
  • Two new #[gpui::test] tests: one asserting usage is populated after a turn with usage data, one asserting token_usage stays None when PromptResponse has no usage.

Test plan

  • Connect an external ACP agent (e.g. claude CLI) and send a message
  • Confirm a token count label (e.g. "1.5k tokens") appears in the thread header after the first response
  • Confirm built-in Zed agent token display (circular ring with percentage) is unchanged
  • Run cargo test -p acp_thread test_token_usage

Release Notes:

  • Fixed missing token usage display for external ACP agents (e.g. Claude Code CLI) in the agent panel

@cla-bot

cla-bot Bot commented Apr 11, 2026

Copy link
Copy Markdown

Thank you for your pull request and welcome to our community. We could not parse the GitHub identity of the following contributors: hahuy.
This is most likely caused by a git client misconfiguration; please make sure to:

  1. check if your git client is configured with an email to sign commits git config --list | grep email
  2. If not, set it up using git config --global user.email email@example.com
  3. Make sure that the git commit email is configured in your GitHub account settings, see https://github.com/settings/emails

@zed-community-bot zed-community-bot Bot added the first contribution the author's first pull request to Zed. NOTE: the label application is automated via github actions label Apr 11, 2026
@hahahuy
hahahuy force-pushed the acp-token-usage-display branch from 96b2b66 to ca80822 Compare April 11, 2026 12:59
@cla-bot

cla-bot Bot commented Apr 11, 2026

Copy link
Copy Markdown

We require contributors to sign our Contributor License Agreement, and we don't have @hahahuy on file. You can sign our CLA at https://zed.dev/cla. Once you've signed, post a comment here that says '@cla-bot check'.

@hahahuy

hahahuy commented Apr 11, 2026

Copy link
Copy Markdown
Author

@cla-bot check

@cla-bot

cla-bot Bot commented Apr 11, 2026

Copy link
Copy Markdown

We require contributors to sign our Contributor License Agreement, and we don't have @hahahuy on file. You can sign our CLA at https://zed.dev/cla. Once you've signed, post a comment here that says '@cla-bot check'.

@cla-bot

cla-bot Bot commented Apr 11, 2026

Copy link
Copy Markdown

The cla-bot has been summoned, and re-checked this pull request!

@hahahuy

hahahuy commented Apr 11, 2026

Copy link
Copy Markdown
Author

@cla-bot check

@cla-bot

cla-bot Bot commented Apr 11, 2026

Copy link
Copy Markdown

We require contributors to sign our Contributor License Agreement, and we don't have @hahahuy on file. You can sign our CLA at https://zed.dev/cla. Once you've signed, post a comment here that says '@cla-bot check'.

@cla-bot

cla-bot Bot commented Apr 11, 2026

Copy link
Copy Markdown

The cla-bot has been summoned, and re-checked this pull request!

@hahahuy

hahahuy commented Apr 11, 2026

Copy link
Copy Markdown
Author

@cla-bot check

@cla-bot cla-bot Bot added the cla-signed The user has signed the Contributor License Agreement label Apr 11, 2026
@cla-bot

cla-bot Bot commented Apr 11, 2026

Copy link
Copy Markdown

The cla-bot has been summoned, and re-checked this pull request!

@hahahuy
hahahuy marked this pull request as ready for review April 11, 2026 14:40
@zed-codeowner-coordinator
zed-codeowner-coordinator Bot requested review from a team, benbrandt and osyvokon and removed request for a team April 11, 2026 14:40
Extract r.usage from each PromptResponse turn and call update_token_usage()
so the token count ring in the agent panel shows live data.

Guard against max_tokens == 0 (ACP protocol has no context-window field)
by rendering a plain "N tokens" label instead of a broken "0% • N / 0" ring.

Add two gpui tests: usage populated and usage absent.
@hahahuy
hahahuy force-pushed the acp-token-usage-display branch from b82b1e6 to 052e81a Compare April 11, 2026 15:32
@hahahuy

hahahuy commented Apr 11, 2026

Copy link
Copy Markdown
Author

Inspired by my Claude CLI's terminal output, which displays per-turn token usage, context window consumption, model name, and relevant context percentage:

Screenshot_20260412_021356

Before:

Screenshot_20260412_021212

After:

Screenshot_20260412_021307

A small change — but knowing you're at 30K/200K context without leaving the editor is genuinely useful when working with long conversations.

Once this lands, I plan to extend it with the remaining indicators:

  • Tokens consumed this turn
  • Context window fill (e.g. 30K / 200K)
  • Model name
  • Relevant context %

@benbrandt

Copy link
Copy Markdown
Member

I think what we need is instead the context update notifications? I dont think we should be keying the ui off of 0 max tokens

@benbrandt benbrandt closed this Apr 13, 2026
@hahahuy
hahahuy deleted the acp-token-usage-display branch April 20, 2026 04:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla-signed The user has signed the Contributor License Agreement first contribution the author's first pull request to Zed. NOTE: the label application is automated via github actions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants