ByteCosts
mcp-tool-order-prompt-cache.mdx

AI Coding Agents

How MCP Tool Order Affects Prompt Cache Reuse

MCP tool order can cause a prompt cache miss when a client sends reordered tool definitions and the provider renders them before the cached boundary. Sort upfront tools by their final request names and keep the list stable for the conversation. On supported Claude models and platforms, tool search defers definitions so only the always-loaded tools enter the initial prefix.

MCP tool order can cause a prompt cache miss when a client sends reordered tool definitions and the provider renders them before the cached boundary. Sort upfront tools by their final request names and keep the list stable for the conversation. As of November 30, 2026, tool search on supported Claude models and platforms can defer definitions so only the always-loaded tools enter the initial prefix.

Why tool order is a cache problem

The Claude API renders tools before system and messages. OpenAI’s prompt caching guide lists tool names, descriptions, schemas, ordering, and tool-specific instructions among the settings that can change the rendered prefix. If the rendered tool order changes, later content stops matching the previously cached prefix from the point of divergence. Requests that still match the earlier entry can read it while it remains warm.

OpenClaw PR #58037 describes a client that called listTools() each turn and sent definitions in collection order. PrimusClaw PR #155 describes a client that used the order returned by each server. In both implementations, upstream ordering can reach the provider unless the client stabilizes the assembled list.

For what the definitions themselves cost before ordering even enters the picture, see MCP tool definition token cost.

Reported tool-order changes

Issue and pull request states are as of November 30, 2026.

ReportWhat goes wrongState
Claude Code issue #112901On Claude Code 2.1.331 with Opus 5.5 and a one-hour cache, the reporter saw a cached, non-deferred tool move when the first deferred MCP tool loaded. The report identifies tools_changed and attributes about 2.7 million rewritten tokens over 30 days to this behavior and media eviction combined, with 250,000 to 420,000 tokens per event.Open (filed Nov 28, 2026)
Claude Code issue #110258The reporter found that a fork or resume built while MCP servers were connecting used the deferred-tool order from an old tool-search result. The first request wrote 169,933 tokens. The next request, with the servers connected, read 209,475.Open (filed Nov 15, 2026)
Claude Code issue #110344On Claude Code 2.1.325 the reporter found deferred_tools_delta after the first user message. With different first prompts, sessions with 443 tools wrote about 8,000 more tokens than sessions with five tools. An identical repeat had no cache write. In a 20-query claude -p test on Sonnet 5.5, the reporter listed per-task costs of $0.040 versus $0.019.Open (filed Nov 16, 2026)
zeroclaw issue #12176The reporter traced the changing deferred-tools section to iteration over a Rust HashMap with the default RandomState. The deferred set was rebuilt per gateway WebSocket connection in their setup, so a client that opened one connection per turn saw a different system prompt nearly every turn.Closed (filed Nov 1, 2026, closed Nov 6, 2026)
MetaMCP issue #278A ten-pull probe of the gateway’s derived OpenAPI spec yielded several hashes of the same total size. The reporter’s diff showed a reordering of the aggregated tool operations. The reporter said Open WebUI 0.13.x then rendered the definitions in a different order and missed the backend cache.Closed (filed Nov 18, 2026, closed Nov 23, 2026)
OpenClaw PR #58037The PR identified a potential cache miss when listTools() returns a different order between turns. It sorts the assembled definitions by name after resolving collisions.Merged (Apr 4, 2026)
PrimusClaw PR #155connectPlatformMcp used each server’s tools/list order. In the PR’s test, the order was stable within a turn and changed across turns. The first request of each measured follow-up turn read no cached tokens before the fix.Merged (Oct 29, 2026)

A tool description can change the rendered prefix even when the names and order stay fixed. Check for timestamps, generated session IDs, or paths that change between requests.

The measured cost of a sort

The PrimusClaw PR includes a before-and-after measurement worth quoting, labeled as that project’s own numbers, taken against a real gateway with two default MCP servers (81 tools) on claude-sonnet-5-5:

Turn 2 first requestTurn 3 first request
Before the fixread 0 / write 21,400read 0 / write 21,426
After sorting by nameread 21,236 / write 172read 21,236 / write 198

To estimate the cache portion of each turn’s first request at committed Sonnet 5.5 rates, assume five-minute writes and apply (read tokens × $0.10 + write tokens × $2.50) ÷ 1,000,000. Turn 2 falls from an estimated $0.053500 to $0.002554, and turn 3 from $0.053565 to $0.002619. These estimates cover cache reads and writes in the measured prefix. See the provider pricing index for current rates.

How to stabilize the rendered tool list

Sort the tools array once, deterministically

Sort by the final names sent to the provider. Keep collision resolution deterministic too, because OpenClaw selects the winning definition before sorting. PrimusClaw PR #155 sorts schemas by name across servers in code-unit order to avoid depending on the environment’s ICU data. The zeroclaw reporter suggested sorting at the source so all callers receive the same order.

Move per-request values from tool descriptions into messages so the definitions stay stable after sorting.

To test your assembler, feed it the same definitions twice with the server responses in different orders. Compare both the final names and the serialized definitions, including input_schema. A name-only check can pass even when a duplicate name resolves to another server’s schema. If the assembled definitions differ, check how the client resolves collisions first. Then check schema serialization if the same definition wins on both runs. This isolates changes your client can fix before you spend tokens on provider requests.

Freeze the list for the conversation

When tool search defers the session’s MCP tools, Claude Code documents keeping the tool list from the first request for the whole conversation. A server that finishes connecting later supplies its tools as deferred definitions loaded on demand.

Claude Code documents a separate resume path for tools loaded into the prefix. If the transcript recorded a still-connecting server’s tool definitions, the first resumed request includes those definitions as recorded. As of November 30, 2026, open issue #110258 reports a different order for deferred tools on a fork or resume in v2.1.322. Issue #112901 reports a reorder on first deferred load. If you own the client, snapshot the assembled definitions sent on the first request and compare them with the first request after a resume.

Anthropic’s tool search excludes deferred definitions from the initial system-prompt prefix. When a search discovers a deferred tool, the result contains a tool_reference block that the API expands to the full definition. The discovered definition enters the conversation at that point, leaving the earlier prefix available for reuse.

The documented constraints to design around:

  • For client-supplied tools, send the full definitions in tools on every request, including deferred definitions. With Anthropic’s MCP connector, configure defer_loading in the mcp_toolset entry’s default_config or per-tool configs.
  • At least one tool must stay non-deferred, normally the tool search tool itself. Up to 10,000 tools can be deferred per request.
  • A tool with both defer_loading: true and cache_control returns HTTP 400. Place the breakpoint on the last non-deferred tool you want to cache. For an upfront MCP toolset, mark the mcp_toolset entry and the API applies the breakpoint to its final expanded tool.
  • Tool definitions loaded by search count as input tokens. Anthropic charges no separate fee for tool search.

When replaying a turn after tool search, preserve the assistant’s server_tool_use and tool_search_tool_result blocks. The API expands the saved references on later requests, so the model can use those tools again without repeating the search. If a request changes a loaded definition while replaying the same reference, include that definition in your comparison alongside the order of upfront tools.

Use deferral when the upfront catalog is large. If your application starts a fresh Claude Code session for each distinct task, measure first-request writes separately from later-turn reads.

A five-step audit procedure

  1. Capture two consecutive requests with a logging proxy, your SDK’s request log, or Claude Code’s session transcripts, and diff the tools arrays: same set, same order, same descriptions?
  2. On supported Claude API requests, set diagnostics.previous_message_id to null on the first request and to the first response’s id on the second. Check diagnostics.cache_miss_reason.type for tools_changed and read usage.cache_read_input_tokens alongside it. An earlier eligible entry can still hit when the comparison finds a later change. In Claude Code v2.1.260 and later, /usage can show likely cause: tool definitions changed for the main conversation. For OpenAI Responses, compare usage.input_tokens_details.cached_tokens.
  3. Sort the collected tools once at the boundary, by name in code-unit order, before they enter the request.
  4. Freeze that serialization for the life of the conversation, and remove per-request values from descriptions.
  5. Hold the tools, system instructions, model, and cache settings fixed. Write an eligible stable prefix, then send a different user suffix after the first response starts and within the TTL. Check whether reads cover the intended prefix. If the reads stay low and the comparison shows no divergence, check the model’s minimum cacheable length and the breakpoint location.

For this test, mark the stable prefix on both requests when your endpoint supports explicit placement. On GPT-5.6 and later, a prior implicit write through the whole first request can end after the changing suffix. The shorter stable portion may then have no cache entry of its own. Compare writes as well as reads so you can tell when the follow-up wrote a new prefix despite having matching upfront definitions.

What to do next

Sources

Interactive report

  • The Cache Breakers: The agent sends another ordinary-looking request. A timestamp, a shuffled tool list, or a model switch just changed what the API can reuse. See what each edit puts on the invoice.

How this note was produced

Drafted with AI assistance from committed ByteCosts pricing snapshots and deterministic cost formulas. Figures are grounded in the linked sources (last updated 2026-11-30). Read our editorial & automation policy, inspect the methodology, or report a discrepancy.

How MCP Tool Order Affects Prompt Cache Reuse. ByteCosts. Updated 2026-11-30. https://bytecosts.com/blog/mcp-tool-order-prompt-cache/

Sources