feat: add anthropic-server-compaction hook for Claude 4.6 server-side context compaction#2269
Open
janghoon-ju wants to merge 1 commit intocode-yeongyu:devfrom
Open
Conversation
… context compaction
Contributor
|
All contributors have signed the CLA. Thank you! ✅ |
Author
|
I have read the CLA Document and I hereby sign the CLA |
There was a problem hiding this comment.
No issues found across 10 files
Confidence score: 5/5
- Automated review surfaced no issues in the provided summaries.
- No files require special attention.
Auto-approved: The feature is opt-in via an experimental flag, targeted specifically at Claude 4.6 models, and includes defensive checks to avoid interfering with existing configurations.
Contributor
|
This is really useful thanks! |
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
Add server-side compaction support for Claude 4.6 models (Opus and Sonnet) via Anthropic's
compact_20260112API. This hook injectscontext_managementinto the API request through thechat.paramshook, enabling the Anthropic API to handle context compression automatically — no client-side compaction logic needed.Problem
Long-running sessions with Claude 4.6 currently rely on client-side compaction (
preemptive-compactionhook), which:Anthropic released server-side compaction as a beta feature with Claude 4.6 (
compact-2026-01-12), which handles context management automatically with minimal integration work. This is the same approach used by Claude Code internally.Solution
New
anthropic-server-compactionhook that:anthropic,google-vertex-anthropic,opencode,github-copilot)contextManagementintooutput.optionsviachat.paramshookcompact-2026-01-12,context-management-2025-06-27)compactioncontent blocks with conversation summaryHow it works
Configuration
Opt-in via
experimental.server_compaction:Coexistence with existing hooks
preemptive-compaction: Both can be enabled. Server-side compaction triggers at the API level, while preemptive-compaction triggers client-side. In practice, server-side fires first, making preemptive-compaction redundant. Users may want to disable preemptive-compaction when using server-side.compaction-context-injector: Still works — it handles opencode's client-side compaction eventscompaction-todo-preserver: Still works — watches for compaction eventsProvider compatibility
anthropicgoogle-vertex-anthropicAnthropicMessagesLanguageModelfrom@ai-sdk/anthropic/internalopencodegithub-copilot(Claude models)What's included
src/hooks/anthropic-server-compaction/hook.tssrc/hooks/anthropic-server-compaction/index.tssrc/hooks/anthropic-server-compaction/index.test.tssrc/config/schema/experimental.tsserver_compactionconfig schemasrc/config/schema/hooks.tssrc/plugin/chat-params.tssrc/plugin/hooks/create-session-hooks.tssrc/plugin-interface.tsTesting
Current limitations
compact-2026-01-12beta header (handled automatically by AI SDK)claude-opus-4-6andclaude-sonnet-4-6— older models are not supportedReferences
Summary by cubic
Adds server-side context compaction for Claude 4.6 (Opus, Sonnet) using Anthropic’s compact_20260112 to reduce extra calls and keep more context. The new hook injects contextManagement via chat.params and activates only for supported Claude models.
New Features
Migration
Written for commit 78cec1a. Summary will update on new commits.