Skip to content

fix: report per-request Anthropic usage in chat token accounting - #27966

Merged
ibetitsmike merged 2 commits into
mainfrom
mike/fantasy-per-request-usage-pin
Aug 12, 2026
Merged

fix: report per-request Anthropic usage in chat token accounting#27966
ibetitsmike merged 2 commits into
mainfrom
mike/fantasy-per-request-usage-pin

Conversation

@ibetitsmike

@ibetitsmike ibetitsmike commented Aug 10, 2026

Copy link
Copy Markdown
Collaborator

Anthropic can report input and cache usage cumulatively in the final message_delta when one request runs multiple internal iterations. Pin charm.land/fantasy to coder/fantasy#50 so the Finish part uses the request's message_start input and cache values while retaining cumulative output tokens.

The v0.40.0 upstream sync (coder/fantasy#51, pinned on main by #27981) does not cover this: the SDK fork's delta-field merge is unchanged at the new pin, and TestActiveServer_AnthropicPersistsPerRequestUsage fails against main's pin with the inflated cumulative value (287682 instead of 139956).

Add a chatd regression test that streams distinct message_start and final message_delta usage and verifies that the persisted assistant message keeps the per-request values. The chattest fake now supports independent start and delta usage, and its tool-call stream emits usage in Anthropic's event shape.

The pin currently points at coder/fantasy#50's branch head (02fe208, the fix rebased onto the v0.40.0 sync) and will be re-pinned to the merged SHA once coder/fantasy#50 lands.

Mux acted on Mike's behalf to author this change.

@ibetitsmike
ibetitsmike marked this pull request as ready for review August 10, 2026 06:21
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: 982cb24c03

ℹ️ 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".

@ibetitsmike
ibetitsmike force-pushed the mike/fantasy-per-request-usage-pin branch from 982cb24 to 70b426e Compare August 11, 2026 11:01
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: 70b426e08f

ℹ️ 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".

@ibetitsmike
ibetitsmike merged commit 88e1135 into main Aug 12, 2026
28 checks passed
@ibetitsmike
ibetitsmike deleted the mike/fantasy-per-request-usage-pin branch August 12, 2026 10:52
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 12, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants