Skip to content

fix(ai): preserve context size across Anthropic fallback - #67758

Merged
gsimone merged 1 commit into
devfrom
anthropic-fallback-context
Dec 2, 2026
Merged

gsimone merged 1 commit into
devfrom
anthropic-fallback-context

Conversation

@gsimone

@gsimone gsimone commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

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

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

When Anthropic's server-side refusal fallback (server-side-fallback-2026-07-01) triggers, usage.iterations contains both the declined attempt and the answering fallback_message. mapUsage sums all iterations for billing, which is right for cost, but previously only set contextTokens when the last iteration had type === "message" — and packages/core dropped Usage.contextTokens anyway. As a result, compaction, background precompute, and next-step outputLimit saw roughly double the real prompt size.

Changes:

  • packages/ai: populate Usage.contextTokens when the final Anthropic iteration is fallback_message (in addition to message), keeping summed billing fields intact.
  • packages/schema & generated packages/client / packages/protocol / packages/cli-rs: add optional contextTokens to SessionMessage.Assistant, SessionEvent.Step.Ended, and SessionEvent.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: store contextTokens on step settlement while keeping billed tokens and non-step usage events (UsageRecorded, Compaction.*, SessionGenerate) billing-only; prefer contextTokens over summed tokens in SessionCompaction (estimatePrompt, hasMeasuredPrompt, precompute cycleStart) and in SessionUsage.calculateCost context-tier selection.

Notes:

  • Google Interactions also populates Usage.contextTokens (raw_prompt_token), so its compaction sizing and context-tier selection now use that figure as well.
  • No feature flag needed (bug fix with backwards-compatible optional field).
  • Follow-up: TUI and app context meters still sum tokens and can be updated separately to prefer contextTokens.

How did you verify your code works?

  • packages/ai: bun test --only-failures (2353 pass, 0 fail), including new fallback_message and streaming message_start -> message_delta fixtures in anthropic-compaction.test.ts.
  • packages/schema: bun test (98 pass), including round-trip and undefined-omission tests in session-terminal.test.ts.
  • packages/protocol: bun test (17 pass) and bun 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, and bun run check:plugin-types.
  • Root bun run lint and bun run typecheck:tsc (34/34 packages).

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Added by the opencode bot (AI-generated).

Related: #67166

Public API: adds optional contextTokens to SessionMessage.Assistant and the Step.Ended/Step.Failed events (backwards compatible)

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]>

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

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.

Anthropic server-side fallback: summed iteration usage makes the next step see about twice the real prompt size

1 participant