Prompt Guidance vs Programmatic Gates: Block the Refund Until the Customer Is Verified, Then Hand Off a Card That Stands Alone
A prompt that says "always verify first" works most of the time. gate.py blocks process_refund until get_customer has returned a verified id, across three call sequences. Then a request with two concerns is split, and "please help" is replaced by a four-field handoff card.
What you will learn
- Tell a rule that can live in the prompt from one that needs a programmatic gate
- Block process_refund until get_customer has returned a verified customer id, and test three call sequences
- Split a message with two requests into two items that share context and get one reply
- Write a four-field handoff card a human can act on without the transcript
Before you start
- Claude tool use: tool_use and tool_result
- Agent SDK hooks: PreToolUse and PostToolUse
A customer writes: refund order 1042 to me. Sounds easy. But picture the wrong order. The agent calls process_refund first, and the money leaves. To whom? Only then does it call get_customer and check who was asking. Too late.
The safe order is the other way round: get_customer first, then process_refund. Some steps must come first, every single time. There are two ways to enforce a rule like that. We can ask Claude nicely in the prompt, or we can build a gate in code. This article shows the difference, then two more things a support agent needs: splitting a request with two concerns, and handing a case to a person.
A sign asks, a lock decides
Think of a bank teller. On the wall there's a sign: please check ID first. Most days the teller follows it. One busy afternoon the teller forgets, and the drawer opens. Now think of a cash drawer with a lock that won't open until an ID is scanned. No scan, no cash. The teller can't forget, because the drawer doesn't allow it.
In an agent, the sign is a prompt instruction: always verify first. Claude follows it most of the time. Once in a while, one call slips through. The failure rate is small, but it isn't zero. The gate is code standing in front of the tool. It looks at every call, and the wrong one is blocked, with a reason, every time.
gate.py: a prerequisite gate in 26 lines
This file is a plain Python simulation, with no model and no API key. It prints what a gate placed before each tool call would do. The mock customer and order live in data.json: one customer, C-77 (Maya), and order 1042 for headphones at 49.0.
import json, pathlib
DATA = json.load(open(pathlib.Path(__file__).with_name("data.json")))
def gate(tool, state):
"""the hook: runs before a tool call and may say no"""
if tool == "process_refund" and not state["verified_id"]:
return "BLOCKED (call get_customer first)"
return "ALLOWED"
def run(label, calls):
state = {"verified_id": None}
for tool, arg in calls:
verdict = gate(tool, state)
print(f"{label}: {tool} {arg} -> {verdict}")
if verdict != "ALLOWED":
continue
if tool == "get_customer" and arg in DATA["customers"]:
state["verified_id"] = arg
run("A", [("process_refund", "1042")])
run("B", [("get_customer", "C-77"), ("process_refund", "1042")])
run("C", [("get_customer", "C-00"), ("process_refund", "1042")])
- The verified-id check. If the tool is
process_refundand no verified customer id exists yet, the rule fires. - The reason.
BLOCKED (call get_customer first)is the text that goes back to the model, so it knows what to do next instead of just failing. - Every call asks the gate.
runcallsgatebefore every tool call. There's no path around it. - Only a real customer verifies.
verified_idis set only whenget_customerruns with an id that exists in the data.
Three call sequences
A: process_refund 1042 -> BLOCKED (call get_customer first)
B: get_customer C-77 -> ALLOWED
B: process_refund 1042 -> ALLOWED
C: get_customer C-00 -> ALLOWED
C: process_refund 1042 -> BLOCKED (call get_customer first)
A asks for the refund straight away: blocked. B calls get_customer for C-77, then asks for the refund: both allowed. C is the interesting one. It looks up C-00, an unknown customer. The lookup itself is allowed, but nobody gets verified, so the refund is still blocked. The gate checks the outcome of the prerequisite. A call with the right name is not enough.
In the Claude Agent SDK, this same rule sits in a PreToolUse hook, which runs before the tool and can deny the call with a reason. How to write one is covered in Agent SDK hooks and hooks that block destructive actions.
Two requests in one message
A different problem. The customer writes: refund order 1042, and also update my email. Don't treat that as one blob. Split it into two items: item 1, refund order 1042; item 2, update the email. Investigate both at the same time with shared context, since both know the customer is C-77 and the order is 1042. Then combine the findings into one unified reply. Split, investigate in parallel, answer once. (The parallel part works the way parallel subagents do: several calls in one turn.)
A handoff card that stands alone
Sometimes the agent must hand the case to a person. By then the chat is long, and the human agent can't read the transcript; they only see what you send. So send a card with four fields: who the customer is, why it happened, how much, and what to do next.
import json, pathlib
DATA = json.load(open(pathlib.Path(__file__).with_name("data.json")))
def build_handoff(order_id, root_cause, action):
order = DATA["orders"][order_id]
return {
"customer_id": order["customer"],
"root_cause": root_cause,
"refund_amount": order["amount"],
"recommended_action": action,
}
print("weak : customer is upset, please help")
card = build_handoff("1042", "headphones arrived with a cracked band",
"approve the refund, send a return label")
for field, value in card.items():
print(f"{field}: {value}")
weak : customer is upset, please help
customer_id: C-77
root_cause: headphones arrived with a cracked band
refund_amount: 49.0
recommended_action: approve the refund, send a return label
The first line is the weak handoff: customer is upset, please help. The human has no customer, no cause and no amount, so they start from zero and ask the customer everything again. The card below it fixes that. customer_id comes from the order, root_cause is what the agent found out, refund_amount is read from the data so nobody has to guess it, and recommended_action is the next move. With those four lines, a person can start in seconds.
How this shows up in a question
A scenario says refunds must never go out before the customer is verified, the prompt already says to verify first, and yet a few refunds went out. Writing the rule in capital letters or adding examples is still a prompt, so it can still miss. Asking Claude to double-check after each refund acts after the money has left. The fix is a gate that blocks process_refund until an id is verified. Clue words that point to a gate: must always, never, before, compliance.
Common mistakes
- Strengthening the prompt for a must-hold rule. A louder sentence is still a sentence.
- Checking after the action. A post-refund review can't bring the money back. The gate runs before.
- Gating on the call, not the result. Sequence C shows why: a lookup of an unknown id must not count as verification.
- Blocking without a reason. Return what to do instead (call get_customer first), so the model can recover.
- Handing off "please help". Send customer id, root cause, amount and recommended action.
Quick recap
| Situation | Tool for the job |
|---|---|
| Rules that don't have to hold every single time | Prompt guidance (the rest stays in the prompt) |
| A step that must come first, every time | Programmatic gate before the tool (a PreToolUse hook in the Agent SDK) |
| Two requests in one message | Split, investigate in parallel with shared context, one reply |
| Escalating to a person | Four-field card: customer_id, root_cause, refund_amount, recommended_action |
Prompts guide. Gates guarantee. The project files for this lesson are in the community, free to join.
Try it yourself
Add a second rule to gate: block refunds above a limit. You'll need the amount, so look it up from DATA["orders"] with the order id. Add a sequence D that verifies C-77 and then asks for a refund that breaks the limit, and predict the printout before you run it. Then drop refund_amount from the handoff card and ask yourself what the human now has to go and find.
Related course on Udemy
Claude Certified Architect - Foundations (CCAR-F) Exam Prep
Agentic loops, tools, MCP, Claude Code, prompts, structured output and context.View on Udemy ↗: Claude Certified Architect - Foundations (CCAR-F) 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.
