Lesson 33.5 · 5 of 6 · 6 min read · Reviewed 24 Nov 2026
Forcing tools to run in order
By the end, you can
- Explain why prompts can't guarantee tool order.
- Enforce a required order with staging, forcing, code checks or hooks.
Sometimes tools must run in a fixed order: verify a customer’s identity before reading their account, or run tests before opening a pull request. Asking nicely in the prompt helps, but it is not a guarantee.
Ways to enforce order
- Stage the tools. Only include
get_accountin thetoolslist afterverify_identityhas succeeded. A tool Claude cannot see cannot be called. - Force each step. For a fixed pipeline, use
tool_choicewith a named tool on each request, so step one must callverify_identity. (On the newest models, which reject forced choice, offer only that step’s tool instead: option 1.) - Check preconditions in code. When Claude calls
get_accounttoo early, return atool_resultwithis_error: trueand a message such as Identity not verified yet. Call verify_identity first. Claude can recover from that. - Use a hookHook: Code that runs automatically at a lifecycle event, such as before or after a tool call, to check, block or log it.. In the Claude Agent SDKClaude Agent SDK: Anthropic’s library for building agents on the same loop that powers Claude Code, with tools, hooks, subagents and MCP. and Claude Code, a PreToolUse hookPreToolUse hook: A hook that runs before a tool call and can allow, deny or ask for approval. can block a call that breaks a rule (chapter 44).
Rule of thumb
If getting the order wrong is merely inefficient, guide it with the prompt. If it is unsafe or breaks a business rule, enforce it in code. That distinction comes up often in architecture reviews and exam scenarios.
Key points
- Prompts express preferred order, but code enforces required order.
- Gate tools in your loop, expose them in stages, or force a named tool per step.
- Hooks can block a call that breaks a precondition.
Enforce tool order in code
Make a verify-before-read rule hold even when the model skips ahead.
You will have: A staged tools_for() and a precondition check, with a log showing the early call blocked.
Steps
- Run the starter: the early get_account call succeeds, which breaks the rule.
- Stage the tools so get_account only appears after verification.
- Add a precondition check inside the tool as a second line of defence.
- Run it and check the counts.
Done when
- The first get_account call returns an error.
- The script prints early get_account calls blocked: 1.
- It prints account reads before verification: 0.
Open in the Playground No API key needed.
Starter code (Python)
# Enforce "verify identity before reading the account" in code, against a model that sometimes skips ahead.
# The "model" here is a script of the calls it tries, so the run is repeatable.
attempts = [
("get_account", {"customer": "C-77"}), # skips ahead: the prompt said not to
("verify_identity", {"customer": "C-77", "code": "123456"}),
("get_account", {"customer": "C-77"}),
]
verified = set()
log = []
def tools_for(customer):
# TODO: only include "get_account" once the customer is in verified
return ["verify_identity", "get_account"]
def run_tool(name, args):
if name not in tools_for(args["customer"]):
return {"is_error": True, "content": "get_account is not available yet. Call verify_identity first."}
if name == "verify_identity":
verified.add(args["customer"])
return {"is_error": False, "content": "verified"}
# TODO: add a precondition check: return an error result if the customer is not verified
return {"is_error": False, "content": "balance: 120 credits"}
for name, args in attempts:
result = run_tool(name, args)
log.append((name, result["is_error"]))
print(f"{name:<16} -> {'ERROR ' if result['is_error'] else ''}{result['content']}")
blocked = sum(1 for n, err in log if n == "get_account" and err)
print("early get_account calls blocked:", blocked)
Show a solution
# Enforce "verify identity before reading the account" in code, against a model that sometimes skips ahead.
# The "model" here is a script of the calls it tries, so the run is repeatable.
attempts = [
("get_account", {"customer": "C-77"}), # skips ahead: the prompt said not to
("verify_identity", {"customer": "C-77", "code": "123456"}),
("get_account", {"customer": "C-77"}),
]
verified = set()
log = []
def tools_for(customer):
# Stage the tools: get_account only appears once verification has succeeded.
return ["verify_identity"] + (["get_account"] if customer in verified else [])
def run_tool(name, args):
if name not in tools_for(args["customer"]):
return {"is_error": True, "content": "get_account is not available yet. Call verify_identity first."}
if name == "verify_identity":
verified.add(args["customer"])
return {"is_error": False, "content": "verified"}
if args["customer"] not in verified: # precondition check, a second line of defence
return {"is_error": True, "content": "Identity not verified."}
return {"is_error": False, "content": "balance: 120 credits"}
for name, args in attempts:
result = run_tool(name, args)
log.append((name, result["is_error"]))
print(f"{name:<16} -> {'ERROR ' if result['is_error'] else ''}{result['content']}")
blocked = sum(1 for n, err in log if n == "get_account" and err)
reads_before_verify = sum(1 for i, (n, err) in enumerate(log) if n == "get_account" and not err and not any(m == "verify_identity" and not e for m, e in log[:i]))
print("early get_account calls blocked:", blocked)
print("account reads before verification:", reads_before_verify)
Do it Enforce a rule with a hook in the Playground. No API key needed.