Skip to content

fix(opencode): correct Bedrock Luna token usage - #47354

Open
Rocklis wants to merge 1 commit into
anomalyco:devfrom
Rocklis:bedrock-usage
Open

fix(opencode): correct Bedrock Luna token usage#47354
Rocklis wants to merge 1 commit into
anomalyco:devfrom
Rocklis:bedrock-usage

Conversation

@Rocklis

@Rocklis Rocklis commented Sep 4, 2026

Copy link
Copy Markdown

Issue for this PR

Closes #47296

Type of change

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

What does this PR do?

Bedrock's GPT-5.6 Luna response already includes cached tokens in the input count. The AI SDK adds the cache read/write fields again while normalizing it, so OpenCode was storing roughly twice the real context usage and compacting after each message.

This removes that extra cache amount before OpenCode splits cached and non-cached input. The workaround is limited to the Bedrock Luna model; other Bedrock and Anthropic models keep the existing path.

How did you verify your code works?

Added a regression case with the numbers from the issue. It now records 152,050 total tokens instead of 303,459 and stays below the compaction limit.

  • bun test test/session/compaction.test.ts
  • bun typecheck
  • repository pre-push typecheck (30 packages)

Screenshots / recordings

Not applicable.

Checklist

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bedrock GPT-5.6: usage total counts cached input twice, so auto-compaction fires after every message

1 participant