Skip to content

fix(providers/anthropic): report per-request usage at Finish - #50

Merged
ibetitsmike merged 1 commit into
coder_2_33from
mike/anthropic-per-request-usage
Aug 12, 2026
Merged

fix(providers/anthropic): report per-request usage at Finish#50
ibetitsmike merged 1 commit into
coder_2_33from
mike/anthropic-per-request-usage

Conversation

@ibetitsmike

Copy link
Copy Markdown

Problem

Anthropic requests on the Claude 5 family can run multiple internal iterations in one messages request. The final message_delta reports input_tokens, cache_read_input_tokens, and cache_creation_input_tokens summed across all iterations, and the coder/anthropic-sdk-go fork's Accumulate merges those fields into acc.Usage when present. The Finish stream part therefore carries cross-iteration sums.

Consumers (coder/coder chatd) read Finish usage as per-request context occupancy (input + cache_read + cache_creation), so a single multi-iteration response inflates the context estimate severalfold and falsely triggers automatic chat compaction. Verified production example: persisted cache_read = 287682 where the real context was 139956 + 7770 (287682 = 139956 + (139956 + 7770)).

Fix

Capture the message_start usage during streaming. At Finish, use its input and cache fields, falling back to the accumulator when message_start was never seen. Output tokens stay cumulative from the accumulator because all output was generated in this request.

Test

TestStream_FinishUsesMessageStartInputUsage streams a two-iteration fixture (message_start with iteration 1 usage, final message_delta with cross-iteration sums) and asserts Finish reports the per-request values. Red-green verified: without the provider change the test fails with the summed values.

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

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

Copy link
Copy Markdown

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: db67be9371

鈩癸笍 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 thread providers/anthropic/anthropic.go
@ibetitsmike

Copy link
Copy Markdown
Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: 1c2958f7a4

鈩癸笍 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".

Anthropic requests on the Claude 5 family can run multiple internal
iterations in one messages request. The final message_delta reports
input, cache_read, and cache_creation tokens summed across all
iterations, and the SDK accumulator merges those sums into acc.Usage.
Consumers treat Finish usage as per-request context occupancy, so the
summed values inflate context estimates and falsely trigger automatic
chat compaction.

Capture message_start usage and use its input and cache fields at
Finish, falling back to the accumulator when message_start was never
seen. Output tokens stay cumulative because all output was generated
in this request.
@ibetitsmike
ibetitsmike force-pushed the mike/anthropic-per-request-usage branch from 1c2958f to 02fe208 Compare August 11, 2026 10:56
@ibetitsmike

Copy link
Copy Markdown
Author

Rebased onto the v0.40.0 sync (bb10946). The grpc/x/text bump commit was dropped: the sync's own dependency updates (grpc v1.83.0, x/text v0.40.0) supersede it, so the PR is now just the usage fix.

Validated on the new base: red-green re-run (test fails on bb10946 without the provider change, passes with it), full go test ./... and go vet ./... green.

Note for reviewers: the v0.40.0 sync does not fix this bug itself; the SDK fork's delta-merge (coder/anthropic-sdk-go messageutil.go) is unchanged at the new pin, so this fix is still required.

Posted by Mux on Mike's behalf.

@ibetitsmike

Copy link
Copy Markdown
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: 02fe208857

鈩癸笍 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 6f8df37 into coder_2_33 Aug 12, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants