Enforce workflow rules and hand off cleanly
Judge whether a workflow rule needs programmatic enforcement or prompt guidance, and judge a handoff summary for what a human without the transcript would miss.
Scenario
Your Customer Support Resolution Agent's system prompt says, in bold: "Always verify the customer with get_customer before calling process_refund."
An audit of 6,000 refunds finds 9 issued to a caller who only gave an order number. Nine is a small number. It is also nine refunds to people who may not own the account, and the prompt can't promise the tenth won't happen.
Explanation
Two ways to make an agent follow a rule.
- Prompt guidance: the rule is in the system prompt. Claude follows it nearly always, and handles cases the rule never named. But "nearly always" is a non-zero failure rate.
- Programmatic enforcement: code makes the wrong order impossible. A prerequisite gate in your tool layer refuses
process_refunduntilget_customerhas returned a verified customer ID for this conversation; an Agent SDK PreToolUse hook does the same by returningpermissionDecision: "deny"with a reason Claude can act on ("verify the customer first"). Hooks give deterministic control: the check runs every time, not when the model remembers it.
How to judge a rule. Ask what one failure costs.
| Rule | Enforce with |
|---|---|
| Identity verified before any refund or account change | Code: money or access moves |
| Refunds above a set amount go to a human | Code: a policy limit |
| Greet by first name, keep replies short | Prompt: a miss is harmless |
| Prefer store credit when the customer has no preference | Prompt: it needs judgment |
Rules where one miss means money, access, legal or safety exposure need code. Style, tone and judgment calls stay in the prompt, where Claude can adapt them. Many designs use both: the prompt explains the rule so Claude plans for it, and the gate guarantees it.
Several concerns in one request. "My blender arrived broken, I was charged twice, and my promo code didn't apply" is three items. List them, investigate each (independent ones in parallel) with the shared customer context, then write one reply that resolves all three.
Handoff to a human. When the agent calls escalate_to_human, the person who picks it up usually doesn't have the transcript. The summary must stand alone:
- Customer: verified customer ID (and whether verification succeeded)
- Issue: what the customer asked for, each concern listed
- Findings and root cause: what the tools showed, and why it went wrong
- Amounts and done so far: order numbers, amounts, any refund already issued
- Why escalated and the recommended action
Worked example
Before: the only control is the bold sentence in the system prompt.
After: a gate the model can't talk its way past (an Agent SDK hook; verified is filled when get_customer confirms identity):
const refundGate: HookCallback = async (input) => {
const pre = input as PreToolUseHookInput;
const args = pre.tool_input as { customer_id: string };
if (verified.has(args.customer_id)) return {};
return {
hookSpecificOutput: {
hookEventName: pre.hook_event_name,
permissionDecision: "deny",
permissionDecisionReason:
"Verify the customer with get_customer first.",
},
};
};
Registered on PreToolUse with a matcher for the refund tool, it runs before every call. The reason goes back to Claude, which then verifies and retries. The prompt sentence stays, so Claude usually verifies first anyway.
Handoff, before: "Customer upset about a charge. Please help."
Handoff, after: "Customer C-4471 (verified). Charged twice for order 5530 (EUR 129.00 each, 3 and 4 March). Root cause: payment retry after a gateway timeout created a second capture. No refund issued: EUR 129.00 is above the agent's EUR 100 limit. Recommended: refund the second capture, confirm by email."