Reproduce four context management techniques from Claude Code using the plain Anthropic Python API.
claude-sonnet-4-6 used throughout.
Send conversation history to Claude with a summarization prompt. Replace message history with the summary.
Track timestamp of last assistant message. If gap > 60 minutes, replace old tool result content with [Old tool result content cleared] before sending, keeping the N most recent.
Send context-management-2025-06-27 beta header with clear_thinking_20251015 strategy to have the API automatically drop old thinking blocks.
Send context-management-2025-06-27 beta header with clear_tool_uses_20250919 strategy to have the API automatically drop old tool results above a token threshold.
| # | Test | Pass Condition |
|---|---|---|
| 1 | Full compaction produces a summary | Response contains a non-empty summary string covering the original conversation |
| 2 | Summary injected as new context works | Follow-up question answered correctly using only the summary (not original messages) |
| 3 | Time-based MC clears old tool results | Tool result content replaced with [Old tool result content cleared] for old results; recent ones intact |
| 4 | Time-based MC keeps N most recent | Exactly keep_recent tool results untouched |
| 5 | API thinking clearing accepted by API | Request with clear_thinking_20251015 succeeds (no 400) |
| 6 | API tool clearing accepted by API | Request with clear_tool_uses_20250919 succeeds (no 400) |
| 7 | API tool clearing triggers at threshold | Response usage shows reduced input tokens when threshold crossed |
| Test | Status |
|---|---|
| test_full_compaction_produces_summary | π’ GREEN |
| test_compacted_context_answers_followup | π’ GREEN |
| test_time_based_mc_clears_old_results | π’ GREEN |
| test_time_based_mc_keeps_recent | π’ GREEN |
| test_time_based_mc_does_not_trigger_when_recent | π’ GREEN |
| test_api_thinking_clearing_reduces_tokens | π’ GREEN |
| test_api_tool_clearing_reduces_tokens | π’ GREEN |
| test_agent_loop_cumulative_tokens_lower_with_clearing | π’ GREEN |
| test_agent_loop_late_turn_tokens_lower_with_clearing | π’ GREEN |
| test_thinking_loop_cumulative_tokens_lower_with_clearing | π’ GREEN |
| test_thinking_loop_late_turn_tokens_lower_with_clearing | π’ GREEN |
| test_combined_clearing_beats_individual | π’ GREEN |