Skip to content

fix: propagate token usage from ACP provider responses#8181

Closed
xr843 wants to merge 1 commit intoblock:mainfrom
xr843:fix/acp-provider-token-usage
Closed

fix: propagate token usage from ACP provider responses#8181
xr843 wants to merge 1 commit intoblock:mainfrom
xr843:fix/acp-provider-token-usage

Conversation

@xr843
Copy link
Copy Markdown

@xr843 xr843 commented Mar 29, 2026

Summary

Fixes #8132 — ACP client provider discards token usage data from PromptResponse, causing all-zero token counts for ACP-based providers (claude-acp, codex-acp). This breaks cost reporting and context window monitoring.

Changes

Three targeted changes in crates/goose/src/acp/provider.rs:

  1. Extend AcpUpdate::Complete to carry Option<AcpUsage> alongside StopReason
  2. Forward r.usage when sending the complete signal from the prompt response handler
  3. Map ACP Usage to ProviderUsage and yield it as the final stream item before breaking

Graceful degradation

For ACP agents that don't yet populate PromptResponse.usage (e.g. codex-acp, goose-acp server), r.usage is None, no ProviderUsage is yielded, and the existing "unknown" fallback in collect_stream() applies unchanged. As those adapters add usage reporting, it starts working automatically.

Notes

  • AcpUsage fields are u64, Goose's Usage uses Option<i32> — the as i32 cast is safe for practical token counts
  • AcpUsage also has optional thought_tokens, cached_read_tokens, cached_write_tokens which Goose's Usage doesn't model yet — these could be surfaced in a follow-up

Credit to the issue reporter for the thorough root cause analysis and proposed fix.

Generated with Claude Code

The ACP client provider was discarding token usage data from
PromptResponse, causing all-zero token counts for ACP-based providers.
This broke cost reporting and context window monitoring.

Three targeted changes:
1. Extend AcpUpdate::Complete to carry Option<AcpUsage>
2. Forward r.usage when sending the complete signal
3. Map ACP Usage to ProviderUsage and yield it on stream completion

Agents that don't populate PromptResponse.usage (like codex-acp)
degrade gracefully — r.usage is None and the existing fallback applies.

Closes block#8132

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Signed-off-by: Tim Ren <[email protected]>
@xr843 xr843 force-pushed the fix/acp-provider-token-usage branch from bae1624 to f7102fe Compare March 29, 2026 15:11
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f7102fe3be

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +718 to +720
input_tokens: Some(u.input_tokens as i32),
output_tokens: Some(u.output_tokens as i32),
total_tokens: Some(u.total_tokens as i32),
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Clamp ACP token counts before downcasting to i32

Casting ACP u64 usage values with as i32 can silently wrap for large counts, producing negative or otherwise corrupted token metrics. This affects downstream cost/context accounting whenever an ACP adapter (or a misbehaving endpoint) reports values above i32::MAX, and the bad values are then persisted and displayed as if valid. Use a checked/saturating conversion (for example, clamp to i32::MAX) before constructing Usage.

Useful? React with 👍 / 👎.

@xr843
Copy link
Copy Markdown
Author

xr843 commented Mar 30, 2026

Closing this per @DOsinga's feedback — focusing on #8182 first as suggested. Will revisit this after #8182 lands. Thanks for the guidance!

@xr843 xr843 closed this Mar 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(acp): ACP client provider discards token usage from PromptResponse

1 participant