[PR #11492] feat: add experimental compaction prompt and preserve prefix support #13813

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

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

State: open
Merged: No


Fixes #11497

Summary

  • Add OPENCODE_EXPERIMENTAL_COMPACTION_PRESERVE_PREFIX flag to preserve agent prefix cache during compaction
  • Add OPENCODE_EXPERIMENTAL_COMPACTION_PROMPT environment variable for customizable compaction prompts
  • Update compaction logic to use detailed session summary format when no custom prompt is provided

Why Prefix Preservation Matters

When enabled, OPENCODE_EXPERIMENTAL_COMPACTION_PRESERVE_PREFIX reuses the original agent's prefix cache (tools + system prompts) instead of switching to the generic compaction agent. This provides significant benefits for providers that implement prefix caching:

  1. Cache Hit Optimization: Providers like Anthropic Claude and OpenAI GPT-4 use prefix caching to reduce token usage cost and latency. By preserving the agent prefix during compaction, context generates 99% cache hits instead of 100% cache miss tokens, preventing new cache build up, improving performance (especially with slower local LLM) and reducing costs up to 90%. (https://platform.openai.com/docs/guides/prompt-caching).

  2. Consistent Tool Context: Maintaining the same tool definitions and system prompts ensures the LLM continues with the exact same capabilities and behavior context, avoiding context-switching overhead.

  3. Seamless Continuation: The session continues with identical agent characteristics, preserving specialized instructions or model-specific optimizations from the original session.

Changes

  1. Flag additions: Two new experimental flags for compaction configuration
  2. Prefix cache preservation: When enabled, compaction reuses the original agent's prefix cache (tools + system prompts)
  3. Customizable default prompt: Option for individual session summary prompts adapted to project workflow
  4. API export: Made SessionPrompt.resolveTools exportable for reuse in compaction

Testing

Enable with:

export OPENCODE_EXPERIMENTAL_COMPACTION_PRESERVE_PREFIX=true
export OPENCODE_EXPERIMENTAL_COMPACTION_PROMPT="Custom prompt if needed"

Note

This is an experimental feature behind feature flags, allowing for gradual rollout and user feedback before potential stabilization.

**Original Pull Request:** https://github.com/anomalyco/opencode/pull/11492 **State:** open **Merged:** No --- Fixes #11497 ## Summary - Add `OPENCODE_EXPERIMENTAL_COMPACTION_PRESERVE_PREFIX` flag to preserve agent prefix cache during compaction - Add `OPENCODE_EXPERIMENTAL_COMPACTION_PROMPT` environment variable for customizable compaction prompts - Update compaction logic to use detailed session summary format when no custom prompt is provided ## Why Prefix Preservation Matters When enabled, `OPENCODE_EXPERIMENTAL_COMPACTION_PRESERVE_PREFIX` reuses the original agent's prefix cache (tools + system prompts) instead of switching to the generic compaction agent. This provides significant benefits for providers that implement prefix caching: 1. **Cache Hit Optimization**: Providers like Anthropic Claude and OpenAI GPT-4 use prefix caching to reduce token usage cost and latency. By preserving the agent prefix during compaction, context generates 99% cache hits instead of 100% cache miss tokens, preventing new cache build up, improving performance (especially with slower local LLM) and reducing costs up to 90%. (https://platform.openai.com/docs/guides/prompt-caching). 2. **Consistent Tool Context**: Maintaining the same tool definitions and system prompts ensures the LLM continues with the exact same capabilities and behavior context, avoiding context-switching overhead. 3. **Seamless Continuation**: The session continues with identical agent characteristics, preserving specialized instructions or model-specific optimizations from the original session. ## Changes 1. **Flag additions**: Two new experimental flags for compaction configuration 2. **Prefix cache preservation**: When enabled, compaction reuses the original agent's prefix cache (tools + system prompts) 3. **Customizable default prompt**: Option for individual session summary prompts adapted to project workflow 4. **API export**: Made `SessionPrompt.resolveTools` exportable for reuse in compaction ## Testing Enable with: ```bash export OPENCODE_EXPERIMENTAL_COMPACTION_PRESERVE_PREFIX=true export OPENCODE_EXPERIMENTAL_COMPACTION_PROMPT="Custom prompt if needed" ``` ## Note This is an experimental feature behind feature flags, allowing for gradual rollout and user feedback before potential stabilization.
yindo added the pull-request label 2026-02-16 18:18:38 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#13813