Skip to content

Report per-prompt usage from cumulative token totals - #367

Draft
alancyao wants to merge 1 commit into
agentclientprotocol:mainfrom
alancyao:acy/cumulative-prompt-usage
Draft

Report per-prompt usage from cumulative token totals#367
alancyao wants to merge 1 commit into
agentclientprotocol:mainfrom
alancyao:acy/cumulative-prompt-usage

Conversation

@alancyao

@alancyao alancyao commented Aug 5, 2026

Copy link
Copy Markdown

Summary

  • derive ACP prompt and quota usage by subtracting the cumulative token total captured at prompt start from the total at completion
  • cache the latest thread usage snapshot at the transport boundary so resumed sessions retain their historical baseline
  • keep context-window occupancy based on the latest completion and clamp counter decreases without producing negative or inconsistent totals

Why

Codex emits both a cumulative total and usage for the latest completion. Publishing the latest completion undercounts ACP prompts containing multiple completions, while publishing the cumulative total directly would re-report historical prompts. Taking a category-by-category delta reports only the usage attributable to the current ACP prompt.

The transport cache stores one overwritten snapshot per thread. It captures restored usage emitted immediately after thread resume, before prompt-specific notification handling begins, and is cleared with the thread handlers.

API contract question

ACP Usage is described as token usage for a prompt turn, while its field documentation refers to totals across a session. This change follows the per-prompt interpretation for PromptResponse usage. Maintainer confirmation on that intent would be helpful.

Test Plan

  • npm run typecheck
  • npm test (343 passed, 28 skipped)
  • npm run build
  • git diff --check

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.

1 participant