Repository navigation
fix(ai): preserve context size across Anthropic fallback - #67758
Conversation
Preserve summed billing tokens while recording the answering attempt's effective contextTokens on Anthropic server-side fallback (fallback_message), projecting it onto SessionMessage.Assistant, and preferring it in compaction prompt sizing and context-tier pricing. Fixes #60792 Supersedes #60807 Co-authored-by: yangguan-xue <[email protected]>
There was a problem hiding this comment.
The change looks good. I copied the new tests onto the base: all 7 fail there, matching #60792. The Anthropic fallback usage leaves contextTokens unset, it isn't stored, and compaction and the context-tier price use the summed billed input. With this PR they pass, along with the nearby ai, core session, schema and client tests. tsc reports no errors for core, schema and ai, the OpenAPI check passes, and the branch merges cleanly onto the latest dev. I couldn't test a real Anthropic fallback end to end, since that needs a live provider.
Public API: adds an optional contextTokens (number) to SessionMessage.Assistant, SessionEvent.Step.Ended and SessionEvent.Step.Failed. That shows up in the OpenAPI spec, the generated SDK types, the plugin .d.ts and the Rust API types. It is additive and backwards compatible; existing clients and plugins can ignore it. Nothing is removed or renamed.
No blocking concerns. Billing stays summed across attempts, and only prompt sizing and the context-tier choice use the last attempt's figure, which matches Anthropic's guidance. Note that Google Interactions sessions also switch to the provider-reported context size for compaction and tier selection.
Related: #67166
Issue for this PR
Closes #60792
Supersedes #60807 (which was auto-closed after 1 month). Credit to @yangguan-xue for the original fix in #60807 and @dhaern for the detailed reproduction in #60792.
Type of change
What does this PR do?
When Anthropic's server-side refusal fallback (
server-side-fallback-2026-07-01) triggers,usage.iterationscontains both the declined attempt and the answeringfallback_message.mapUsagesums all iterations for billing, which is right for cost, but previously only setcontextTokenswhen the last iteration hadtype === "message"— andpackages/coredroppedUsage.contextTokensanyway. As a result, compaction, background precompute, and next-stepoutputLimitsaw roughly double the real prompt size.Changes:
packages/ai: populateUsage.contextTokenswhen the final Anthropic iteration isfallback_message(in addition tomessage), keeping summed billing fields intact.packages/schema& generatedpackages/client/packages/protocol/packages/cli-rs: add optionalcontextTokenstoSessionMessage.Assistant,SessionEvent.Step.Ended, andSessionEvent.Step.Failed, and mirror it in live TypeScript (packages/client/src/solid/data.ts) and Rust (packages/cli-rs/crates/store/src/messages.rs) projections.packages/core: storecontextTokenson step settlement while keeping billedtokensand non-step usage events (UsageRecorded,Compaction.*,SessionGenerate) billing-only; prefercontextTokensover summedtokensinSessionCompaction(estimatePrompt,hasMeasuredPrompt,precomputecycleStart) and inSessionUsage.calculateCostcontext-tier selection.Notes:
Usage.contextTokens(raw_prompt_token), so its compaction sizing and context-tier selection now use that figure as well.tokensand can be updated separately to prefercontextTokens.How did you verify your code works?
packages/ai:bun test --only-failures(2353 pass, 0 fail), including newfallback_messageand streamingmessage_start->message_deltafixtures inanthropic-compaction.test.ts.packages/schema:bun test(98 pass), including round-trip andundefined-omission tests insession-terminal.test.ts.packages/protocol:bun test(17 pass) andbun run check:generated.packages/client:bun test --only-failures(294 pass).packages/core: targeted session tests (session-usage,session-step,session-compaction,session-projector,session-runner,session-generate,session-title,models— 447 pass).packages/cli-rs:cargo test -p opencode-api-types -p opencode-store(128 pass),bun run check:drift, andbun run check:plugin-types.bun run lintandbun run typecheck:tsc(34/34 packages).Checklist
Added by the opencode bot (AI-generated).
Related: #67166
Public API: adds optional
contextTokensto SessionMessage.Assistant and the Step.Ended/Step.Failed events (backwards compatible)