Skip to content

fix: pin fixed anthropic/fantasy forks for streaming token accounting#24077

Merged
ibetitsmike merged 8 commits intomainfrom
mike/fix-anthropic-streaming-token-accounting
Apr 8, 2026
Merged

fix: pin fixed anthropic/fantasy forks for streaming token accounting#24077
ibetitsmike merged 8 commits intomainfrom
mike/fix-anthropic-streaming-token-accounting

Conversation

@ibetitsmike
Copy link
Copy Markdown
Collaborator

@ibetitsmike ibetitsmike commented Apr 7, 2026

Mux working on behalf of Mike.

Summary

Fixes Anthropic streaming token accounting so input, output, and cache token counts match what the Anthropic console reports.

Changes

Streaming token fix (dependency layer)

Pins coder/anthropic-sdk-go to a fork commit that fixes the SDK's Accumulate method. The original SDK was clobbering input_tokens, cache_creation_input_tokens, and cache_read_input_tokens when processing message_delta SSE events because Anthropic only sends output_tokens in those deltas. The fix uses JSON presence checks (Raw() != respjson.Omitted) to merge only fields actually present in the delta, preserving the values from message_start.

Regression tests (Coder layer)

  • coderd/x/chatd/chattest/anthropic.go: Added cache token fields to AnthropicUsage and a new AnthropicTextChunksWithCacheUsage helper for realistic SSE fixtures.
  • coderd/x/chatd/chattest/anthropic_test.go: Integration test proving cache tokens survive accumulation.
  • coderd/x/chatd/chatloop/chatloop_test.go: Pipeline test asserting processStepStream captures the correct final usage from Anthropic streams.

Title generation accounting (removed)

Reverted automatic title generation token recording. These tokens use lightweight models (Haiku, GPT-4o Mini) with negligible cost, and recording them as hidden messages skewed per-chat usage metrics. Manual title regeneration still records usage as before.

Usage disclaimer

  • Added an info note to the Agents usage page (ChatCostSummaryView.tsx) stating that title generation is not counted towards usage limits.
  • Added a note to docs/ai-coder/agents/platform-controls/usage-insights.md documenting the exclusion.

@ibetitsmike ibetitsmike changed the title fix(chatd): pin fixed anthropic/fantasy forks for streaming token accounting fix: pin fixed anthropic/fantasy forks for streaming token accounting Apr 7, 2026
@ibetitsmike ibetitsmike force-pushed the mike/fix-anthropic-streaming-token-accounting branch 4 times, most recently from 77b0fa0 to e24093f Compare April 8, 2026 11:24
Pin coder/anthropic-sdk-go with explicit JSON presence checks in
Accumulate() so input and cache tokens from message_start are
preserved when message_delta omits them.

Also wire async title generation to record its token usage (it
was previously discarded via underscore) using the same recording
path already used by manual title regeneration, and resolve the
title model's DB config so usage appears under the correct model
on the usage page.
@ibetitsmike ibetitsmike force-pushed the mike/fix-anthropic-streaming-token-accounting branch from e24093f to 96049a0 Compare April 8, 2026 11:40
…tern

Remove the Alert component wrapper and use the same inline
icon-plus-text pattern as the unpriced messages warning.
Position the note directly below that warning.
@ibetitsmike ibetitsmike marked this pull request as ready for review April 8, 2026 16:32
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: 478a83f6b3

ℹ️ 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 go.mod Outdated
Comment thread site/src/pages/AgentsPage/components/ChatCostSummaryView.tsx Outdated
Manual title regeneration still records usage via
recordManualTitleUsage, so the disclaimer should clarify that
only automatic title generation is excluded.
@ibetitsmike
Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector
Copy link
Copy Markdown

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

ℹ️ 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
Copy link
Copy Markdown
Collaborator Author

@codex review

@ibetitsmike ibetitsmike enabled auto-merge (squash) April 8, 2026 17:00
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: 20e510a797

ℹ️ 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 go.mod
// 2) Explicit usage-field merging in Accumulate using JSON presence checks
// (preserves input and cache tokens from message_start when message_delta
// omits them).
replace github.com/charmbracelet/anthropic-sdk-go => github.com/coder/anthropic-sdk-go v0.0.0-20260408163834-8345653c189a
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Use a publicly resolvable anthropic-sdk-go replacement

Pinning github.com/charmbracelet/anthropic-sdk-go to github.com/coder/anthropic-sdk-go makes dependency resolution fail in a default Go environment (GOPROXY=https://proxy.golang.org,direct), which blocks go test/go mod download for affected packages; in this review environment, fetching that pseudo-version returns HTTP 403 from the proxy. This is a build-breaking regression for contributors or CI jobs that do not have GOPRIVATE and credentials configured for github.com/coder, so the replacement should point to a proxy-resolvable module/version (or otherwise avoid introducing a private-only dependency in the default build path).

Useful? React with 👍 / 👎.

@ibetitsmike ibetitsmike merged commit 5902351 into main Apr 8, 2026
29 of 31 checks passed
@ibetitsmike ibetitsmike deleted the mike/fix-anthropic-streaming-token-accounting branch April 8, 2026 17:07
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 8, 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.

3 participants