Cache-Safe Instruction Changes and Large Tool Catalogs: Append the New Rule at the End, Defer 57 of 60 Tools With Tool Search
A manager wants a formal tone at turn 3, and the tool list has grown to sixty. Editing the system prompt reads only 187 cached tokens and rewrites 887; appending a system message reads 1,011 and writes 45. And with tool search, a refund question starts at 230 tokens in context instead of 6,052.
What you will learn
- Explain why editing the system prompt mid-chat rewrites the cache while appending a system message keeps it
- Read cache_read_input_tokens and cache_creation_input_tokens to check whether a change was cheap
- Keep a 60-tool catalog out of the context with the tool search tool and defer_loading
- Place the cache breakpoint on the last tool that is not deferred
Before you start
- Claude cache breakpoints and cache_control
- Claude tool use: tool_use and tool_result
A support agent is in the middle of a long chat. A manager asks for a formal tone from now on. And the tool list has quietly grown to sixty tools. Two quick fixes look harmless here: edit the system prompt, and keep sending every tool. Both cost money on every call after that, and both show up in two usage numbers: cache_read_input_tokens and cache_creation_input_tokens.
The rule for this article is short: change the end of the prompt, not the start, and fetch a tool instead of carrying them all.
A binder and a card catalogue
Think of a binder you've already read. You know pages one to twenty by heart. If someone rewrites page two, you reread from page two on. If they stick a note on the last page, you read the note and nothing else.
Now think of a library with sixty books. Putting them all on your desk leaves no room to work. A card catalogue lets you look up one book and fetch it, while the three books you use all day stay on the desk.
The binder is the prompt cache. The catalogue is tool search.
The cache reads a prefix: tools, then system, then messages
Claude builds the request in a fixed order: tools first, then the system text, then the messages. The cache matches a prefix in that order, and a change invalidates its own level and every level after it.
Breakpoint placement itself (where cache_control goes and why reads can stay at zero) is covered in Claude cache breakpoints. Here we use that rule on two changes that happen in a running system.
Part A: a new rule at turn 3
The project runs on a stand-in model (the Anthropic SDK on an httpx mock transport with a prompt cache that follows the tools, system, messages order), so no API key is needed and the replies are test replies. Its token counts use one token per four characters, so they are the stand-in's numbers, not Claude prices or limits.
instructions.py sets three breakpoints: one on the last tool, one on the system text, and one on the newest user message, which moves forward every turn. Then it changes the rule at turn 3 in two ways (an excerpt; the full file also records each turn in the history):
CACHE = {"type": "ephemeral"}
TOOLS = [dict(t) for t in CORE]
TOOLS[-1]["cache_control"] = CACHE
RULE = ("From now on, reply in a formal tone "
"and address the customer as Mr or Ms.")
def block(text):
return [dict(type="text", text=text, cache_control=CACHE)]
def send(client, system, history, user, rule=None):
msgs = history + [dict(role="user", content=block(user))]
if rule:
msgs.append(dict(role="system", content=rule))
r = client.messages.create(
model=MODEL, max_tokens=256, tools=TOOLS,
system=block(system), messages=msgs)
# ... the turn is added to history, and r.usage is returned
def bad_edit(client):
system, history, usage = POLICY, [], []
for turn, text in enumerate(CHAT, 1):
if turn == 3:
system = system + " " + RULE
usage.append(send(client, system, history, text))
return usage
def safe_append(client):
history, usage = [], []
for turn, text in enumerate(CHAT, 1):
rule = RULE if turn == 3 else None
usage.append(send(client, POLICY, history, text, rule))
return usage
bad_editadds the rule to the system text at turn 3.safe_appendleaves the system text alone and adds the rule as a message withrole: "system"at the end ofmessages.
A system message inside messages is only accepted by the models the docs list; Claude Sonnet 5.5 is one of them, and the README notes that Claude Sonnet 5 uses the top-level system field instead. Check the docs for your model before you rely on it.
A. a new rule at turn 3
edit the system prompt
turn 1 cache_read 0 cache_creation 964 input 0
turn 2 cache_read 964 cache_creation 47 input 0
turn 3 cache_read 187 cache_creation 887 input 0
append a system message
turn 1 cache_read 0 cache_creation 964 input 0
turn 2 cache_read 964 cache_creation 47 input 0
turn 3 cache_read 1011 cache_creation 45 input 32
- Turns 1 and 2 are identical in both: turn 1 writes 964 tokens, turn 2 reads all 964 and writes 47 for the new turn.
- At turn 3 the edit reads only 187 tokens (the tools) and rewrites 887, because the system text changed and everything after it went with it.
- The append reads 1,011 and writes just 45. Same chat, same new rule. Only the place it went differs.
Part B: sixty tools, one refund
The catalog has 3 core tools and 57 more. Sending all 60 definitions on every call fills the context before any work is done. The tool search tool lets Claude look a tool up when it needs one:
SEARCH = {"type": "tool_search_tool_regex_20251119",
"name": "tool_search_tool_regex"}
CACHE = {"type": "ephemeral"}
def everything():
"""all 60 definitions in context, on every call"""
tools = [{k: v for k, v in t.items() if k != "defer_loading"}
for t in ALL_TOOLS]
tools[-1]["cache_control"] = CACHE
return tools
def deferred():
"""57 stay defer_loading: true, 3 load up front"""
tools = [SEARCH] + [dict(t) for t in ALL_TOOLS]
# the breakpoint goes on the last tool that is not deferred
tools[len(CORE)]["cache_control"] = CACHE
return tools
- The regex variant of tool search is generally available, not a beta. There's also a BM25 variant,
tool_search_tool_bm25_20251119. deferred()sends the search tool and the whole catalog, but the 57 extra tools carrydefer_loading: true, so their definitions stay out of the context.- The cache breakpoint sits on the last tool that is loaded. A deferred tool can't carry one.
- When you send the next turn, pass the assistant's content back unchanged, so the search results stay in the history.
The same refund question ("Please issue a refund for order A-204.") goes through both setups, two calls each:
B. sixty tools, one refund
all 60 loaded call 1 in context 6052 cache_read 0 cache_creation 6028
all 60 loaded call 2 in context 6111 cache_read 6028 cache_creation 0
tool search call 1 in context 230 cache_read 0 cache_creation 206
tool search call 2 in context 476 cache_read 206 cache_creation 0
- With all 60 loaded, the context holds 6,052 tokens on the first call and 6,111 on the second.
- With tool search it starts at 230, and after the search finds the refund tool it holds 476.
- The cached prefix still works: 206 tokens are read on the second call.
The catalog still has sixty tools. The context holds the ones in use.
Why a search doesn't break the cache
The search result comes back with a tool_reference, here to billing_issue_refund. That reference is appended inline in the messages and the tool's definition expands right there. Nothing earlier in the prefix changes. You can also write a small router tool of your own that returns tool_reference blocks, and the same rule holds.
Common mistakes
- Editing the system prompt in place mid-chat. Everything after the tools is written again, as turn 3 of the edit showed.
- Swapping the tool list each turn. Tools render first, so a changed list invalidates the whole cache.
- Loading all sixty tools and trusting the model to cope. You pay for every definition on every call.
- Putting the breakpoint on a deferred tool. A deferred tool can't carry one; use the last loaded tool.
- Rebuilding the assistant turn instead of passing it back. The search results have to stay in the history.
- Guessing instead of reading usage.
cache_read_input_tokenstells you whether a change was cheap.
Quick recap
| Change | Cache-safe move | In the run |
|---|---|---|
| A new rule mid-chat | Append a system message at the end | Turn 3 reads 1,011, writes 45 (edit: 187 and 887) |
| A large tool catalog | Tool search plus defer_loading: true | 230 in context, not 6,052 |
| Breakpoint with deferred tools | On the last loaded tool | 206 read on call 2 |
| A found tool | Arrives inline as a tool_reference | Tools array unchanged |
Try it yourself
In bad_edit, change the rule at turn 2 instead of turn 3. Before you run it, predict the turn 2 cache_read, and whether turn 3 gets its cache back. Then do the same for a change to one tool's description.
Related course on Udemy
Claude Certified Developer - Professional (CCDV-P) Exam Prep
Claude agents in production: reliability, observability, evals, cost, guardrails and fleets.View on Udemy ↗: Claude Certified Developer - Professional (CCDV-P) Exam Prep (paid course, opens in a new tab)Related posts
The Claude Messages API: Your First Call, Field by Field
One request in, one Message out. The four fields of a Messages API call (model, max_tokens, system, messages), how to read content blocks, stop_reason and usage, and why every turn resends the history.
Claude Tool Use: How tool_use and tool_result Work in the Messages API
Follow one order question through Claude's tool round trip: define a tool, read the tool_use block, run your own function and send a matching tool_result back, including parallel calls and is_error.
Claude Client SDKs Wrap One REST API: Raw HTTP, Python, TypeScript and Async Calls Compared
Every Claude client SDK wraps the same REST request: a POST to /v1/messages with a JSON body. See it built by hand, through the Python and TypeScript SDKs, as typed errors, and as async calls that wait together.
Anthropic SDK Timeouts, Retries and Error Classes: Configure the Client Once, Then Give 429, 529, 400 and Timeouts Their Own Move
Configure the Anthropic SDK client (timeout, max_retries, with_options, async, pagination), see which errors it retries by itself, then repair a retry wrapper so a 429, a 529, a 400 and a timeout each get the move the docs support.
