Skip to content

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
janghoon-ju:feat/anthropic-server-compaction
Open

feat: add anthropic-server-compaction hook for Claude 4.6 server-side context compaction#2269
janghoon-ju wants to merge 1 commit intocode-yeongyu:devfrom
janghoon-ju:feat/anthropic-server-compaction

Conversation

@janghoon-ju
Copy link

@janghoon-ju janghoon-ju commented Mar 3, 2026

Summary

Add server-side compaction support for Claude 4.6 models (Opus and Sonnet) via Anthropic's compact_20260112 API. This hook injects context_management into the API request through the chat.params hook, 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-compaction hook), 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-compaction hook that:

  1. Detects Claude 4.6 models (Opus + Sonnet) on Anthropic-compatible providers (anthropic, google-vertex-anthropic, opencode, github-copilot)
  2. Injects contextManagement into output.options via chat.params hook
  3. AI SDK automatically adds required beta headers (compact-2026-01-12, context-management-2025-06-27)
  4. Anthropic API handles compaction server-side — returns compaction content blocks with conversation summary

How it works

chat.params hook → output.options.contextManagement = { edits: [{ type: "compact_20260112", ... }] }
         ↓
opencode ProviderTransform.providerOptions() → { anthropic: { contextManagement: { ... } } }
         ↓
AI SDK @ai-sdk/anthropic → API request with context_management + beta headers
         ↓
Anthropic API → auto-compacts when input tokens exceed trigger threshold

Configuration

Opt-in via experimental.server_compaction:

// oh-my-opencode.jsonc
{
  "experimental": {
    // Simple: enable with defaults (128K token trigger)
    "server_compaction": true,

    // Advanced: custom trigger threshold and instructions
    "server_compaction": {
      "enabled": true,
      "trigger_tokens": 100000,
      "instructions": "Preserve code snippets, file paths, and technical decisions."
    }
  }
}

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 events
  • compaction-todo-preserver: Still works — watches for compaction events

Provider compatibility

Provider Supported Notes
anthropic Direct API
google-vertex-anthropic Uses same AnthropicMessagesLanguageModel from @ai-sdk/anthropic/internal
opencode Anthropic via OpenCode proxy
github-copilot (Claude models) Claude models on Copilot
Non-Claude models ⏭️ Skipped Hook is a no-op for non-4.6 models

What's included

Path Description
src/hooks/anthropic-server-compaction/hook.ts Hook implementation (74 LOC)
src/hooks/anthropic-server-compaction/index.ts Barrel export
src/hooks/anthropic-server-compaction/index.test.ts 15 tests covering positive/negative/edge cases
src/config/schema/experimental.ts server_compaction config schema
src/config/schema/hooks.ts Hook name registration
src/plugin/chat-params.ts Handler wiring
src/plugin/hooks/create-session-hooks.ts Hook instantiation
src/plugin-interface.ts Hook passing to handler

Testing

bun test src/hooks/anthropic-server-compaction/index.test.ts
# 15 pass, 0 fail

bun run typecheck  #
bun run build      #

Current limitations

  • Beta API: Requires compact-2026-01-12 beta header (handled automatically by AI SDK)
  • Same model for summarization: Anthropic uses the same model for the compaction summary — no option to use a cheaper model
  • Claude 4.6 only: claude-opus-4-6 and claude-sonnet-4-6 — older models are not supported

References


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

    • New anthropic-server-compaction hook injects contextManagement for Claude 4.6 on Anthropic, Google Vertex Anthropic, OpenCode, and GitHub Copilot (Claude).
    • Opt-in config: experimental.server_compaction true or object with trigger_tokens (default 128k) and optional instructions.
    • No-op for non-4.6 or non-Claude models; preserves existing options and won’t overwrite existing contextManagement.
    • SDK sends required beta headers automatically; tests added to cover positive/negative cases.
  • Migration

    • Enable with experimental.server_compaction; optionally set trigger_tokens and instructions.
    • Consider disabling preemptive-compaction; server-side compaction typically triggers first.

Written for commit 78cec1a. Summary will update on new commits.

@github-actions
Copy link
Contributor

github-actions bot commented Mar 3, 2026

All contributors have signed the CLA. Thank you! ✅
Posted by the CLA Assistant Lite bot.

@janghoon-ju
Copy link
Author

I have read the CLA Document and I hereby sign the CLA

github-actions bot added a commit that referenced this pull request Mar 3, 2026
Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@ualtinok
Copy link
Contributor

ualtinok commented Mar 4, 2026

This is really useful thanks!
How is the speed difference between server side compaction and current, were you able to test?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants