#10214: feat(compaction): Add Anthropic server-side compaction API support
docs
extensions: memory-lancedb
agents
stale
Cluster:
Compaction Enhancements and Features
## Summary
Adds opt-in support for Anthropic's server-side compaction API, which offloads context summarization to Anthropic's API for better performance and summarization quality.
Closes #10213
## Changes
- **Config**: Add `serverSide` option under `agents.defaults.compaction`
- `enabled`: Boolean to enable server-side compaction (default: false)
- `strategy`: Compaction strategy version (default: "compact_20260112")
- **API Integration**:
- Add `context_management.edits` parameter with compaction strategy
- Add `anthropic-beta: context-management-2025-06-27` header
- **Backward Compatibility**: Client-side compaction remains the default
- **Tests**: Add comprehensive test suite (8 tests covering all scenarios)
- **Documentation**: Update compaction docs with usage examples
## Configuration Example
```json5
{
agents: {
defaults: {
compaction: {
serverSide: {
enabled: true,
strategy: "compact_20260112", // optional, this is the default
},
},
},
},
}
```
## Technical Details
When enabled, the implementation:
1. Adds the `context-management-2025-06-27` beta header to Anthropic API requests
2. Includes `context_management.edits` in the request body with the specified strategy
3. Preserves existing beta headers and extraBody properties
4. Works alongside other extra params (temperature, maxTokens, etc.)
## Testing
- [x] All 8 new tests pass
- [x] Schema validation passes for new config options
- [x] No breaking changes to existing functionality
## References
- [Anthropic Compaction Docs](https://docs.anthropic.com/en/docs/build-with-claude/compaction)
- [Cookbook: Automatic Context Compaction](https://platform.claude.com/cookbook/tool-use-automatic-context-compaction)
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR adds opt-in Anthropic server-side compaction support by introducing `agents.defaults.compaction.serverSide` config, validating it in the agent-defaults zod schema, and wrapping the embedded runner stream function to inject the `anthropic-beta: context-management-2025-06-27` header plus a `context_management.edits` request body entry (defaulting to strategy `compact_20260112`). It also adds a dedicated vitest suite covering enable/disable behavior, header appending, and `extraBody` preservation, and documents how to configure the feature.
<h3>Confidence Score: 4/5</h3>
- Generally safe to merge, but has a couple of edge-case request-shaping regressions that should be fixed first.
- Core integration is small and well-tested, but the wrapper currently (1) concatenates an untyped existing header value into a string and (2) overwrites any existing `extraBody.context_management`, which can break users who already set these fields.
- src/agents/pi-embedded-runner/extra-params.ts
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#21547: feat: add compaction.announce config to notify users of compaction ...
by jlwestsr · 2026-02-20
81.2%
#8313: feat: auto-compaction support for spawned subagent sessions
by vishaltandale00 · 2026-02-03
80.8%
#19329: feat: add per-agent compaction and context pruning overrides
by curtismercier · 2026-02-17
80.5%
#11089: feat(compaction): support customInstructions and model override for...
by p697 · 2026-02-07
79.6%
#14887: feat(compaction): configurable auto-compaction notifications with o...
by seilk · 2026-02-12
79.5%
#14021: feat(compaction): optional memory flush before manual /compact
by phenomenoner · 2026-02-11
78.6%
#20038: (fix): Compaction: preserve recent context and sync session memory ...
by rodrigouroz · 2026-02-18
78.3%
#6353: fix(agents): detect Anthropic 'exceed context limit' error for auto...
by Glucksberg · 2026-02-01
78.3%
#4042: agents: add proactive compaction before request
by freedomzt · 2026-01-29
78.0%
#10505: feat(compaction): add timeout, model override, and diagnostic logging
by thebtf · 2026-02-06
77.9%