feat(compaction): hoist system messages into a stable cache-friendly prefix - #124
Open
tishachawla-jg wants to merge 1 commit into
Open
feat(compaction): hoist system messages into a stable cache-friendly prefix#124tishachawla-jg wants to merge 1 commit into
tishachawla-jg wants to merge 1 commit into
Conversation
…prefix
Cost compaction's biggest lever is the prompt-cache discount: cached
prefix tokens bill far cheaper than fresh ones, and a single volatile
value ahead of the static instructions busts the whole discount.
_compact_messages already deduped non-system messages but pinned
system messages in place. Now it hoists all system messages into a
stable leading prefix (static instructions first, volatile
conversation after), preserving relative order within the system
block and the tail. No-op when system is already first, which is the
common case.
Closes the gap between the context_compaction policy's stated
technique ("move volatile values below the static prefix") and the
live rewrite. Adds unit tests for the hoist and the no-op path; full
suite green.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Improves cost compaction by attacking the real cost driver: the prompt-cache discount. Cached prefix tokens bill far cheaper than fresh ones, and a volatile value ahead of the static instructions busts the whole discount.
_compact_messages(insrc/tokenops/control/integration.py) already deduped non-system messages but pinned system messages in place. It now hoists all system messages into a stable leading prefix — static instructions first, volatile conversation after — preserving relative order within the system block and within the tail.This closes the gap between what the
context_compactionpolicy already documents ("move volatile values below the static prefix") and what the live rewrite actually did.Behavior
Tests
test_compact_hoists_system_into_stable_prefix,test_compact_is_noop_when_system_already_first.Not in this PR (follow-ups)
cache_guardidea in Add a policy: the contributor path, and what is worth adding next #66.🤖 Generated with Claude Code