anomalyco / anomalyco/opencode

[FEATURE]: reduce per-turn token accumulation for OpenAI models on Bedrock Mantle

Open
#38,409 1 comment 0 reactions 1 assignee View on GitHub

@nexxeln is already working on this.

Since Jul 23, 2026.

Dominant language
TypeScript
Stars
209k
Forks
27.5k
PR merge metrics
PR metrics pending

Description

Feature hasn't been suggested before.
  • I have verified this feature I'm about to request hasn't been suggested before.
Describe the enhancement you want to request

Long agentic sessions using OpenAI models on Bedrock Mantle (amazon-bedrock/openai.gpt-5.6) accumulate per-turn token costs. In a 34-turn session, input tokens grew from 4K to 95K.

Two causes:

  1. Encrypted reasoning blocks are replayed on every turn. With store: false (hardcoded for Mantle at packages/opencode/src/provider/transform.ts:1099), every assistant reasoning block is serialized with its encrypted_content and replayed on every subsequent turn (packages/llm/src/protocols/openai-responses.ts, lowerMessages). Turn 34 replays encrypted reasoning from all 33 prior turns.

  2. Tool outputs are not truncated during normal turns. The truncateToolOutput helper in packages/opencode/src/session/message-v2.ts is only called during compaction, not during normal turn message building. Large tool results accumulate turn by turn.

Requested changes:

A. Keep encrypted reasoning only from the last N assistant messages** in lowerMessages when store: false. Replace older blocks with item_reference placeholders. (File: openai-responses.ts) --> See my comment below.

B. Pass toolOutputMaxChars to toModelMessagesEffect during normal turns, not just compaction. Thread a config option through the message building pipeline. (File: message-v2.ts and call sites)

Note: previous_response_id chaining was tested on Bedrock Mantle and does not save tokens (the server still processes and bills the full context). Issue #34133. Per-turn budget can already be controlled by lowering limit.input in opencode.json (overflow.ts:usable() minus compaction buffer), which is separate from A and B.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.