anthropics / anthropics/claude-agent-sdk-python

Support context_management.edits with pause_after_compaction

Đang mở
#570 1 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
Python
Star
8.1k
Fork
1.3k
Merge trung bình
2 ngày 31 phút
Pull request đã merge (30 ngày)
1

Mô tả

## Feature Request

The Messages API now supports server-side compaction via `context_management.edits` (beta `compact-2026-01-12`), including a `pause_after_compaction` parameter that pauses the API after generating a compaction summary (returning `stop_reason: "compaction"`).

This is extremely valuable for SDK users building persistent agent sessions (chat bridges, long-running assistants) because it allows:

1. **Memory persistence** — save conversation notes/state to disk before context is compacted
2. **Content injection** — preserve recent messages or inject instructions after compaction
3. **Token budget enforcement** — track compaction count and gracefully wrap up when budget exceeded

### Current behavior

The SDK does its own client-side auto-compact at ~95% context usage. There's no way to hook into the compaction lifecycle to run custom logic (like saving state).

### Requested behavior

1. Allow passing `context_management` parameter through to the Messages API
2. Handle `stop_reason: "compaction"` in the SDK's message loop
3. Expose a callback/hook so SDK consumers can run custom logic when compaction pauses (e.g., save memory, inject messages)
4. Optionally: expose the `instructions` parameter for custom summarization prompts

### Related issues

- #23 — non-interactive `/compact` (same underlying need for programmatic compaction control)
- #531 — no mid-loop compaction for custom MCP tools

### API reference

https://platform.claude.com/docs/en/build-with-claude/compaction

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.