API Features & Capabilities

Context Editing: Surgically Removing or Replacing Messages Mid-Conversation

The Messages API is stateless — you send the whole conversation every turn, which means you can also change it every turn. Done right, that trims cost and redacts sensitive content; done wrong, it produces 400 errors and cache misses.

Claude 3P 101 · Updated July 2026 · Unofficial guide

Because your application owns the messages array, "editing context" comes in two flavors: changes you make yourself on the client, and a server-side beta feature — officially named context editing — where the API prunes for you. Most teams end up using both.

Client-side editing: what's safe and what isn't

Between turns you are free to drop old exchanges, shorten verbose tool results, or redact content that shouldn't persist (a customer's card number that appeared in a tool result has no business riding along for fifty more turns). But three rules are enforced by the API:

Server-side context editing: let the API prune for you

The managed alternative ships two strategies under context_management.edits, with beta header anthropic-beta: context-management-2025-06-27, and works on all supported Claude models:

StrategyWhat it clearsKey defaults
clear_tool_uses_20250919Old tool results (optionally tool inputs too)Triggers at 100,000 input tokens; keeps the 3 most recent tool uses
clear_thinking_20251015Older thinking blocksModel-specific; newer models keep all thinking by default
context_management={"edits": [{
    "type": "clear_tool_uses_20250919",
    "trigger": {"type": "input_tokens", "value": 80000},
    "keep": {"type": "tool_uses", "value": 5},
    "exclude_tools": ["read_policy_doc"],   # never cleared
    "clear_at_least": {"type": "input_tokens", "value": 10000},
}]}

The crucial property: editing happens server-side before the prompt reaches Claude. Your client keeps its full, unmodified history and re-sends it as usual — no synchronization logic, no risk of corrupting your own records. Responses report what happened in context_management.applied_edits (fields like cleared_tool_uses and cleared_input_tokens; in streaming this arrives in the final message_delta). Two configuration details: when combining both strategies, clear_thinking_20251015 must be listed first in the array, and clear_at_least exists because clearing invalidates the cached prefix at the clearing point — make each invalidation clear enough tokens to be worth it.

You can also preview the effect: the count_tokens endpoint accepts context_management (with the beta flag) and returns both the post-edit input_tokens and context_management.original_input_tokens.

Choosing between the approaches

Client-side editing gives you exact control and is the only way to redact — server-side clearing reduces what the model sees, but your compliance story may require the sensitive content gone from your own stores too. Server-side editing wins for agentic loops that accumulate bulky tool results, where "keep the last N tool uses" is exactly the policy you want and you'd rather not maintain pruning code. For summarize-rather-than-delete, see compaction — the two are separate features and separate betas. Context editing is beta on all five platforms (Claude API, Claude Platform on AWS, Bedrock, Vertex AI, Foundry).

Rule of thumb: trim stale bulk with server-side context editing, redact sensitive data client-side, and never edit anything the current turn's signature covers.

Where to go next

Pair this with context compaction and the memory tool for long-running agents, and see prompt cache mechanics to understand the invalidation costs of every edit.

Sources