Workflow Enforcement and Handoff
Core principle (design guidance)
Prompt instructions are probabilistic: the model usually follows them, but not always. When a failure is costly (money, security, compliance, ordering of steps), enforce the rule deterministically outside the model.
| Need | Use |
|---|---|
| Style, tone, preferences, judgement calls | Prompt instructions |
| "Step B must never run before step A succeeded" | Programmatic gate (code or a PreToolUse hook) |
| "Refunds over a limit need approval" | Hook or application check that blocks and routes to a human |
| Normalising or redacting tool data | PostToolUse hook (updatedToolOutput, additionalContext) |
Enforcement mechanisms
- A
PreToolUsehook can returnpermissionDecisionofallow,deny,askordefer, withpermissionDecisionReason. Adenyblocks the call and the reason tells the model why, so it does not keep retrying. - Returning
deferends the turn withstop_reason: "tool_deferred", so the call can be resumed later, for example after a human decides. - Tool restriction (
toolson a subagent) removes capability entirely; a missing tool cannot be misused. - See 1.5 for hook mechanics.
Prerequisite gating pattern (design guidance)
Make the order structural: the later tool (for example process_refund) is only exposed or only allowed once the earlier step (for example verify_customer) has produced a validated result. Do not rely on "always verify first" in the system prompt.
Handoff (design guidance)
When an agent escalates to a human or another agent, the receiver has none of the conversation. Hand off a self-contained, structured summary:
- who or what the case is about (ids)
- what was asked and what was already done (actions, results)
- current state and the specific blocker or reason for escalation
- the recommended next step
Exam traps
- "Add a stronger instruction" as the fix for a rule that must never be broken. The exam answer is a programmatic gate.
- Hooks that mutate shared state in a fixed order. Matching hooks run in parallel; completion order is non-deterministic, and when decisions conflict the most restrictive wins (
denyoverdeferoveraskoverallow). - Handoff as "see the transcript".
References
- Hooks in the SDK — checked Oct 16, 2026
Check your understanding
A refund over a threshold must never be auto-approved. Prompt or code?Show answerHide answer
Answer
Code or hook that blocks and escalates.Two
PreToolUsehooks disagree (allowvsdeny). Result?Show answerHide answerAnswer
denywins.What must an escalation summary contain?Show answerHide answer
Answer
Identifiers, actions taken, current state and blocker, recommended next step.