[PR #11136] fix(session): optimize system reminder to reduce token usage #13667

Open
opened 2026-02-16 18:18:30 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/11136

State: open
Merged: No


What does this PR do?

This PR fixes a token amplification issue caused by the watchdog reminder logic.

Previously, the session loop would wrap every queued user message in <system-reminder> tags and inline the user’s full text on every iteration when step > 1. As a result, the same user content was repeatedly duplicated in the model input, causing input tokens to grow with both the number of loop iterations and the length of queued messages (effectively O(steps × message_length)). This led to rapid context window exhaustion and unnecessarily high inference costs.

Changes in this PR

  • Refactored reminder handling
    Removed the logic that mutates user message parts. User messages remain intact and are no longer rewritten/expanded during processing.

  • Optimized prompt injection
    Replaced “copy user text into a reminder” with a concise system reminder that references the existence/count of queued messages (e.g., “There are X new user messages waiting…”), without duplicating user content.

  • Added throttling to prevent reminder spam
    Implemented exponential backoff for reminder injection (starting at 15s, doubling up to 5 minutes), so long-running tasks are not repeatedly interrupted by identical reminders.

  • Added reminder deduplication state
    Introduced queuedReminder state to track the latest queued user message ID and avoid reinjecting reminders for the same queued message on every loop cycle.

How did you verify your code works?

  • Token usage verification
    Simulated multi-turn conversations where the user sends multiple messages while the agent is busy. Confirmed input token size remains stable and does not grow explosively over repeated loop iterations.

  • Behavioral testing
    Verified the agent still detects queued user messages and prioritizes responding to them, but now respects the backoff window instead of emitting reminders every iteration.

  • Regression testing
    Exercised normal chat flow, compaction, and tool execution paths to confirm no behavioral regressions and that tasks still complete successfully.

  • Build check
    Ran bun run build locally to ensure the changes compile cleanly with no type errors.

Issue

Fixes https://github.com/anomalyco/opencode/issues/11142

**Original Pull Request:** https://github.com/anomalyco/opencode/pull/11136 **State:** open **Merged:** No --- ## What does this PR do? This PR fixes a token amplification issue caused by the watchdog reminder logic. Previously, the session loop would wrap every queued user message in `<system-reminder>` tags and inline the user’s full text on every iteration when `step > 1`. As a result, the same user content was repeatedly duplicated in the model input, causing input tokens to grow with both the number of loop iterations and the length of queued messages (effectively **O(steps × message_length)**). This led to rapid context window exhaustion and unnecessarily high inference costs. ### Changes in this PR - **Refactored reminder handling** Removed the logic that mutates user message parts. User messages remain intact and are no longer rewritten/expanded during processing. - **Optimized prompt injection** Replaced “copy user text into a reminder” with a concise system reminder that references the existence/count of queued messages (e.g., “There are X new user messages waiting…”), without duplicating user content. - **Added throttling to prevent reminder spam** Implemented exponential backoff for reminder injection (starting at 15s, doubling up to 5 minutes), so long-running tasks are not repeatedly interrupted by identical reminders. - **Added reminder deduplication state** Introduced `queuedReminder` state to track the latest queued user message ID and avoid reinjecting reminders for the same queued message on every loop cycle. ## How did you verify your code works? - **Token usage verification** Simulated multi-turn conversations where the user sends multiple messages while the agent is busy. Confirmed input token size remains stable and does not grow explosively over repeated loop iterations. - **Behavioral testing** Verified the agent still detects queued user messages and prioritizes responding to them, but now respects the backoff window instead of emitting reminders every iteration. - **Regression testing** Exercised normal chat flow, compaction, and tool execution paths to confirm no behavioral regressions and that tasks still complete successfully. - **Build check** Ran `bun run build` locally to ensure the changes compile cleanly with no type errors. ## Issue Fixes https://github.com/anomalyco/opencode/issues/11142
yindo added the pull-request label 2026-02-16 18:18:30 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#13667