Cost Optimization & FinOps

Multi-Turn Conversation Cost Control

The Messages API is stateless: every turn resends the whole conversation as billable input. Left unmanaged, per-session cost grows quadratically with conversation length.

Claude 3P 101 · Updated July 2026 · Unofficial guide

A chat or agent session doesn't pay for "the new message." It pays for everything: the system prompt, all prior user and assistant turns, tool definitions, tool results, images, and documents are resent — and billed as input tokens — on every request. Turn 50 of a conversation carries 49 turns of baggage. With Claude's 1M-token context windows (billed at standard per-token rates with no long-context premium on the Claude API), a session can run a very long time before hitting a technical wall. The wall it hits first is financial.

First line of defense: caching, not cutting

Before removing anything, make the repeated history cheap. Prompt caching charges 0.1x the base input price for cache reads, so a conversation whose prefix is cached costs roughly a tenth as much per turn on the history portion. Automatic caching (a single top-level cache_control field) moves the breakpoint forward as the conversation grows, which is exactly the multi-turn pattern. Caching changes what you pay, not whether tokens count — a cached 200k-token history still occupies the context window — but for cost purposes it is the biggest and easiest win.

Three ways to shrink the history itself

Truncate. Drop the oldest turns once the conversation passes a token threshold, keeping the system prompt and the most recent exchange intact. Cheap and predictable; loses information. Best for support-style chats where old turns rarely matter. A gentler variant is server-side context editing: the clear_tool_uses_20250919 strategy (beta header context-management-2025-06-27) clears old tool results server-side once input passes a trigger (default 100,000 tokens) while your client keeps its full history. Tool results are usually the bulkiest, least-referenced content in an agent session, so this targets exactly the right tokens.

Summarize. Replace old turns with a compact summary. You can roll your own, or use server-side compaction (beta header compact-2026-01-12, supported on Fable 5, Opus 4.8/4.7/4.6, Sonnet 5, Sonnet 4.6): the API summarizes older content when input crosses a trigger (default 150,000 tokens, minimum 50,000) and returns a compaction block; on the next request the API automatically drops everything before it. Note the billing detail: compaction iterations are reported separately in usage.iterations, and the top-level token counts do not include them — sum all iterations for the true bill.

Externalize. Move long reference material out of the transcript entirely. Store documents once (the Files API on platforms that support it, or your own retrieval store) and inject only the relevant excerpt per turn, instead of letting a 100-page PDF ride along in every request.

Mind the cache when you cut

Cutting history and caching history are in tension: caching is a strict prefix match, so editing anything mid-conversation invalidates every breakpoint after the edit. Two mitigations from the official docs: use clear_at_least with context editing so each invalidation removes enough tokens to be worth the re-write, and put cache_control on compaction blocks (with the system prompt cached separately) so a new compaction doesn't wipe the whole cache.

Rule of thumb: cap sessions with a budget, not a turn count. Track cumulative input tokens per session (input_tokens + cache_read_input_tokens + cache_creation_input_tokens); when a session passes your threshold, compact or truncate. Turn counts lie — one turn with a big tool result can outweigh thirty chat turns.

One more lever: thinking blocks

On newer models (Opus 4.5+, Sonnet 4.6+, Fable 5), previous thinking blocks are kept in context by default and billed as input tokens; the clear_thinking_20251015 context-editing strategy can trim them. And if you switch models mid-conversation, strip prior thinking blocks — other models ignore them but still charge you for the tokens.

Where to go next

Pair this with the caching ROI walkthrough and context strategy. For where these betas run, check the feature matrix.

Sources