In this categoryTroubleshooting · 27
- How to Fix an OpenAI 401 'Invalid API Key' ErrorStart
- How to Fix OpenAI 429 Rate Limit Errors With Backoff
- How to Fix Anthropic Claude API 401 Authentication Errors
- How to Fix Google Gemini 'API Key Not Valid' Errors
- How to Fix an API Key That Loads as Undefined
- How to Handle Anthropic 529 'Overloaded' Errors
- How to Fix Rate Limit Errors from an AI API
- How to Fix CORS Errors When Calling an AI API From the Browser
- How to Fix 'Model Not Found' and Deprecated Model Errors
- How to Rotate a Leaked API Key Without Downtime
- How to Fix SSL Certificate Errors When Calling AI APIs
- How to count tokens before sending a prompt to Claude
- How to Fix 'Context Length Exceeded' Token Limit Errors
- How to fix a context length exceeded error in the Claude API
- How to fix a Claude response that gets cut off mid-sentence
- How to reduce Claude hallucinations by grounding answers in your documents
- How to keep a long Claude conversation under the context limit
- How to stop Claude from calling tools when it should not
- How to handle a Claude Fable 5 refusal with a fallback model
- How to choose the right Claude model for cost and quality
- How to cut Claude API costs with prompt caching
- How to halve Claude costs for bulk jobs with the Batch API
- How to cap spend on a Claude agent with a task budget
- How to Debug an MCP Server That Will Not Connect
- How to Ask an Agent to Explain a Bug Before Fixing It
- How to Roll Back a Bad Deploy Quickly
- How to Fix Cursor Not Indexing Your Codebase
How to keep a long Claude conversation under the context limit
Turn on server-side compaction so a multi-turn chat summarizes its own history before it overflows the window.
A chat that runs for many turns keeps growing, because you resend the full history on every request. Eventually it approaches the context window and either errors or gets expensive. Server-side compaction lets the API summarize earlier turns automatically before you hit the limit, so the conversation keeps going. This guide shows how to enable it and handle the one tricky part correctly.
- The Anthropic SDK and an API key
- A model that supports compaction (Opus 4.6+, Sonnet 4.6, or Fable 5)
- A conversation loop that resends message history
Step 1: Enable compaction on the beta endpoint
Compaction is a beta feature. Call the beta messages endpoint, pass the compact beta header, and add a compact edit to context_management. The API will summarize old context when it nears the trigger threshold.
from anthropic import Anthropic
client = Anthropic()
messages = []
def chat(text):
messages.append({"role": "user", "content": text})
resp = client.beta.messages.create(
betas=["compact-2026-01-12"],
model="claude-opus-4-8",
max_tokens=4000,
messages=messages,
context_management={"edits": [{"type": "compact_20260112"}]},
)
# Append the FULL content, not just text (see step 2)
messages.append({"role": "assistant", "content": resp.content})
return respStep 2: Confirm compaction is firing
Watch the usage object across turns. As the conversation grows, input_tokens should level off rather than climb forever once compaction kicks in, because the older turns are now represented by a compact summary.
Step 3: Know the difference from context editing
Compaction summarizes old turns into a compact block. Context editing instead clears stale tool results or thinking blocks entirely. They are separate features with separate headers. Use compaction for long chats; use context editing for agents that pile up large tool outputs you no longer need.
| Feature | What it does | Beta header |
|---|---|---|
| Compaction | Summarizes earlier history | compact-2026-01-12 |
| Context editing | Clears old tool results or thinking | context-management-2025-06-27 |
Result: a 20 turn support session that previously crept toward the window stabilized around 46,800 input tokens once compaction started summarizing the early turns, so the chat kept running without an overflow and without resending the entire transcript at full price.
Watch related tutorials
1:42:18
28:14
41:09
9:47
8:23
52:31New guides in your inbox
Fresh step-by-step how-to guides as we publish them. One email a week, no more.