feat(accounting): report deduplicated SDK result token usage - #954
Open
ran411285752 wants to merge 5 commits into
Open
feat(accounting): report deduplicated SDK result token usage#954ran411285752 wants to merge 5 commits into
ran411285752 wants to merge 5 commits into
Conversation
Why: - SDK result modelUsage is cumulative and cannot be summed directly. What: - Normalize provider token buckets, aggregate model rows, and compute result deltas. - Deduplicate result UUIDs and expose reset/fallback semantics. Impact: - No ACP wire behavior changes until the negotiated integration is enabled.
Why: - PromptResponse cannot carry usage for failed or autonomous SDK results. What: - Advertise a versioned Claude accounting extension. - Emit one deduplicated accounting notification before result lifecycle exits. Impact: - Standard PromptResponse and usage_update behavior remains unchanged. - Hosts must opt in through client capabilities.
Why: - Hosts need an explicit additive contract that avoids double counting standard turn usage. - Review exposed fallback reconciliation and extension transport edge cases. What: - Document version negotiation, additive fields, deduplication, and compaction limits. - Reconcile fallback usage with later cumulative snapshots and reject partial snapshots. - Keep standard result handling alive when optional accounting delivery fails. - Add lifecycle and fallback-debt regression coverage. Impact: - PromptResponse.usage and standard usage_update semantics remain unchanged. - Local evidence and raw session data remain uncommitted.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Purpose
Preserve provider token accounting for failed, cancelled-result, autonomous, background, and subagent work without changing existing ACP turn-usage semantics.
Closes #953.
Related to #927 and #914.
Protocol impact
_claude/accountingUsageversion 1 extension notification.agentCapabilities._meta.claudeCode.accountingUsage.clientCapabilities._meta.claudeCode.accountingUsage.version = 1.usage_update.PromptResponse.usage.Accounting semantics
resultIdis the stable deduplication key.params.usageis additive.modelUsageis a cumulative SDK audit snapshot and must not be summed directly.resultUsageis diagnostic top-level result usage and must not be added toparams.usage.PromptResponse.usageand accounting events must not be summed together.Lifecycle coverage
Accounting is attempted before:
Optional extension delivery failures are logged without changing the standard result lifecycle.
Evidence
Built-Adapter ACP subprocess scenarios covered:
A-H and J:
Compaction scenario I:
The
/compactresult had zeroresultUsageand a 32,903-token accounting delta. Public transcript APIs and JSONL omit that compact-call usage, so they cannot be used as a complete post-compaction accounting ledger.Verification
npm run buildnpm run lintorigin/mainRelated context