1.4 Workflow Enforcement and Handoff
What You Need to Know
Section titled “What You Need to Know”Some agent workflows have steps that must run in a set order. Prompt-based guidance sets that order with a system prompt instruction, such as “Always verify the customer’s identity before processing a refund”. Programmatic enforcement sets it in code: hooks, prerequisite gates or code-level checks that block downstream tools (the tools that run later) until their prerequisites complete. A prerequisite gate is a programmatic check that blocks a tool from executing until a prior condition is met. Task Statement 1.4 makes a strict distinction between the two.
This lesson uses one made-up example throughout: a tree surgeon whose agent answers client enquiries and books work. Before the agent books a tree felling, it must check whether the tree is protected. Before the agent refunds a deposit, it must verify the client’s identity. The agent also formats quote emails, where a mistake costs nothing.
Prompt Guidance Versus Programmatic Enforcement
Section titled “Prompt Guidance Versus Programmatic Enforcement”A prompt instruction works most of the time, perhaps in 90-95% of cases. It still has a non-zero failure rate, because the model is probabilistic. Sometimes the model skips steps, reorders them or reads the instruction loosely. A stronger system prompt, firmer instructions and few-shot examples (examples that show the correct workflow) all improve accuracy. None of them gives a deterministic guarantee. For low-stakes operations, that failure rate is fine.
A gate works every time. It is code, not a prompt instruction, so it is deterministic. Whatever the model decides, the gate prevents the wrong execution order. A prompt can tell the tree surgeon’s agent to check protection first, but the agent could still skip that check on an occasional job. The agent cannot skip a gate on the booking tool.
Which Operations Need a Gate
Section titled “Which Operations Need a Gate”The choice depends on what a single failure costs.
- Financial operations (refunds, transfers, payments): programmatic enforcement. A single unverified refund to the wrong account is a financial loss.
- Security operations (identity verification, access control): programmatic enforcement. A single bypass of identity verification is a security breach.
- Compliance operations (AML (anti-money laundering) checks, regulatory requirements): programmatic enforcement. A single missed compliance check can result in legal penalties.
- Low-stakes operations (formatting preferences, style guidelines, output ordering): prompt-based guidance is acceptable. A formatting inconsistency is not a business risk.
For the tree surgeon, the deposit refund is financial, the identity check is security and the protection check is compliance. All three have gates. Quote formatting is low stakes, so a prompt instruction is enough.
How a Prerequisite Gate Works
Section titled “How a Prerequisite Gate Works”Consider an agent with get_customer, lookup_order and process_refund tools. A gate on process_refund works like this.
- The gate checks whether
get_customerhas returned a verified customer ID for this session. - If yes,
process_refundexecutes normally. - If no,
process_refundreturns an error. The error says that the customer’s identity is not verified, and it tells the model to callget_customerfirst.
The model cannot bypass the gate by deciding to skip verification. The gate blocks even a direct call to process_refund, and the error forces the model to verify identity first.
Production data on such an agent shows that it processes refunds without verifying account ownership in 8% of cases. Its system prompt instructs: “Always verify the customer’s identity before processing any refund.” The prompt works 92% of the time and fails 8% of the time. That 8% has already resulted in refunds processed on wrong accounts. A refund is a financial operation with real monetary consequences.
The fix is the programmatic prerequisite gate above. The gate prevents the incorrect execution order. In this way, it eliminates the 8% failure rate entirely, without improving the prompt at all.
The tree surgeon’s booking tool works the same way. When the session has no protection check result, the booking tool returns an error that names the check. The agent runs the check, then retries the booking.
Subagent Lifecycle Hooks: SubagentStart and SubagentStop
Section titled “Subagent Lifecycle Hooks: SubagentStart and SubagentStop”The Claude Agent SDK provides lifecycle hook events to manage subagents. They complement the PreToolUse and PostToolUse hooks covered in Task Statement 1.5. They are useful background, but the exam does not test them. Suppose that the tree surgeon’s coordinator spawns a quote-drafting subagent. The coordinator is the agent that spawns subagents and receives their results.
SubagentStart fires when the coordinator spawns a subagent via the Task tool. Claude Code v2.1.63 renamed this tool to Agent. SubagentStart receives the subagent’s type and id, and it cannot block the spawn. It can add context. Return additionalContext in its JSON output, and that string is placed in the subagent’s context before its first prompt. This is the documented way to give a newly spawned subagent an instruction that applies to all its work. To enforce rules on the spawn itself, attach a PreToolUse hook to the Agent tool. Examples of such rules are rate limits or a check that the coordinator passed required context. That hook can deny or rewrite the outgoing invocation before the subagent starts.
SubagentStop fires when a subagent finishes and returns its results to the coordinator. It receives the subagent’s id and final message, so it can validate output and log completion to monitor performance. Validation can fail, for example because the output does not conform to the expected schema. The hook then exits with code 2, which prevents the subagent from stopping and makes it continue its work. Exit code 2, or a top-level decision: "block" with a reason, keeps the subagent running. A quote draft that is missing a required field would make its subagent continue its work.
SubagentStop does not transform the returned output. The hooks reference documents no field on any event that rewrites a tool result in place. The coordinator reshapes the output afterwards.
| SubagentStart | SubagentStop | |
|---|---|---|
| Fires | when the coordinator spawns a subagent via Task (Agent) | when the subagent finishes and returns its result |
| Receives | the subagent’s type and id | the subagent’s id and its final message |
| Can block | no | yes: exit code 2 sends the subagent back to work |
| Can add context | yes: additionalContext goes into the subagent’s context before its first prompt |
no |
| Can rewrite the result | no | no |
| Typical use | an audit log of spawns, or a standing instruction for every subagent | validate the result against a schema before the coordinator acts on it |
| To enforce instead | a PreToolUse hook on the Agent tool, which can deny or rewrite the spawn | a PostToolUse hook on the Agent tool, which acts on the returned result |
You configure both events like any other hook event, in settings.json or in a subagent’s frontmatter (the block of settings at the top of the subagent’s file):
{ "hooks": { "SubagentStart": [ { "hooks": [{ "type": "command", "command": "echo \"subagent started $(date)\" >> .claude/spawns.log" }] } ], "SubagentStop": [ { "hooks": [{ "type": "command", "command": ".claude/hooks/validate-subagent-output.sh" }] } ] }}The first script only appends a line. To add context instead, print JSON with an additionalContext field. The second script reads the result on stdin and exits 2 if the schema check fails. Exit code 2 is one of SubagentStop’s two ways to keep the subagent running.
Subagents can also define their own hooks in their frontmatter. The frontmatter supports all hook events, including PreToolUse and PostToolUse. These hooks are scoped to the component’s lifetime. So they only intercept tool calls made by that subagent, not by the coordinator or other subagents. This lets you enforce a separate policy for each subagent. A billing subagent might have a PreToolUse hook that blocks refunds above a threshold, while a technical support subagent has no such restriction.
Stop hooks in a subagent’s frontmatter are automatically converted to SubagentStop events, because SubagentStop is the event that fires when a subagent completes. You can therefore define cleanup or validation logic in the subagent’s own configuration and rely on it to run when the subagent completes.
Multi-Concern Request Handling
Section titled “Multi-Concern Request Handling”Customers often put several issues in one request, such as “I want to return my order, update my shipping address, and ask about my loyalty points”. Handle it in three steps.
- Decompose the request into distinct items (return, address update, loyalty enquiry).
- Investigate each in parallel using shared context (the customer’s account information is relevant to all three).
- Synthesise a unified resolution that addresses all items in a single response.
It is wrong to handle the items sequentially in separate conversations. It is also wrong to address only the first item and forget the rest. A tree surgeon’s client might ask to move a felling date, query an invoice and ask about stump grinding. The agent separates the three requests, checks each against the client’s account and sends one reply.
Structured Handoff Protocols
Section titled “Structured Handoff Protocols”When an agent cannot resolve an issue and must escalate to a human agent, the handoff must follow a structured protocol. The human agent does not have access to the conversation transcript. They cannot scroll through the chat history. The handoff summary is the only information they receive, so it must be self-contained. It must include these fields.
- Customer ID, so the human agent can open the account.
- Conversation summary: what the customer asked for and what the agent has attempted.
- Root cause analysis: the agent’s assessment of the underlying issue.
- Refund amount (if applicable): the specific financial figure, not a vague reference.
- Recommended action: what the agent believes the human agent should do.
If the summary is incomplete, the human agent must ask the customer to repeat everything. That is a poor experience. When the tree surgeon’s agent cannot resolve a disputed charge, its handoff contains all five fields, including the exact refund figure.
In the exam
Section titled “In the exam”The exam tests the distinction between prompt-based guidance and programmatic enforcement repeatedly. It uses one decision rule across multiple scenarios. A wrong answer on high-stakes scenarios will cost you marks. When the scenario involves money, security or compliance, the answer is always programmatic enforcement.
The exam also tests how agents should handle compound requests.
Exam Traps
Section titled “Exam Traps”Sources
Section titled “Sources”- Claude Agent SDK Overview — Anthropic
- Hooks Reference — Anthropic (source for the subagent lifecycle section)
- Building with Claude API, including the Customer Support Resolution Agent scenario (Skilljar) — Anthropic
- CCAR-F Exam Guide v1.0: Task Statement 1.4 — Anthropic
Exam Simulator
Section titled “Exam Simulator”Five exam-style multiple-choice questions on Workflow Enforcement and Handoff. Pick an answer, then open the explanation.
Question 1
Section titled “Question 1”Production data reveals that in 8% of cases, a customer support agent processes refunds without verifying account ownership, occasionally leading to refunds on wrong accounts. The system prompt clearly states “always verify customer identity before processing refunds.” What is the most appropriate fix?
- A. Implement a programmatic prerequisite gate that blocks process_refund until get_customer has returned a verified customer ID
- B. Add stronger instructions to the system prompt emphasising the critical importance of verification before any refund processing
- C. Add few-shot examples demonstrating the correct verification-then-refund workflow sequence
- D. Implement a routing classifier that sends all refund requests to a specialised verification-first pipeline
Answer & explanation
Correct: A
- A is correct because financial operations require deterministic enforcement. A prerequisite gate physically prevents the refund tool from executing until identity verification is complete, eliminating the 8% failure rate entirely. This is the only option that provides a 100% guarantee.
- B is wrong because the current prompt already instructs verification but fails 8% of the time. Enhanced prompts may reduce the rate to 3-4% but cannot eliminate it. Financial operations require deterministic guarantees, not probabilistic improvements.
- C is wrong because few-shot examples improve consistency but still produce a non-zero failure rate. For financial operations where a single failure means a refund to the wrong account, probabilistic improvements are insufficient.
- D is wrong because a routing classifier handles how requests reach agents, not how agents execute their internal workflow. The issue is that the agent sometimes skips verification within its own execution, which requires a per-agent enforcement mechanism.
Question 2
Section titled “Question 2”When an agent escalates to a human agent, the handoff summary must include specific fields because:
- A. The human agent prefers structured data over conversational summaries when triaging a queue of escalations
- B. Structured handoffs are required by the Claude API specification
- C. The human agent does NOT have access to the conversation transcript and needs a self-contained summary
- D. The monitoring system requires specific fields on every handoff for compliance tracking and audit reporting
Answer & explanation
Correct: C
- C is correct because the critical constraint is that human agents cannot scroll through the chat history. The handoff summary is the only information they receive. Without a complete, self-contained summary (customer ID, conversation summary, root cause analysis, refund amount, recommended action), the human agent must ask the customer to repeat everything.
- A is wrong because this is not about preference — it is about the human agent literally not having access to the prior conversation.
- B is wrong because handoff format is not an API specification requirement. It is an architectural design decision based on the reality that human agents lack conversation access.
- D is wrong because while monitoring may benefit from structured data, the primary reason is the human agent’s inability to access the conversation transcript.
Question 3
Section titled “Question 3”A compliance team requires that a certain workflow step occurs 100% of the time before a financial operation. Which approach provides this guarantee?
- A. Including the requirement in the system prompt with bold formatting and repeated emphasis
- B. Implementing a hook or prerequisite gate that programmatically blocks the operation until the step completes
- C. Using few-shot examples that demonstrate the correct sequence in 10 different scenarios
- D. Adding a separate validation agent that checks compliance before forwarding to the financial agent
Answer & explanation
Correct: B
- B is correct because hooks and prerequisite gates provide deterministic enforcement. The operation physically cannot execute until the prerequisite completes. This is the only mechanism that guarantees 100% compliance.
- A is wrong because prompt instructions, regardless of formatting or emphasis, are probabilistic. They improve the rate but cannot guarantee 100% compliance. A single failure in a financial operation has real consequences.
- C is wrong because few-shot examples improve accuracy but remain probabilistic. Ten examples may achieve 98% compliance, but 100% requires deterministic mechanisms.
- D is wrong because a validation agent is itself probabilistic — it might occasionally fail to catch violations. A programmatic gate is the only deterministic guarantee.
Question 4
Section titled “Question 4”A customer submits a request with three concerns: return an order, dispute a charge, and update their address. How should the agent handle this?
- A. Address the most urgent concern first and ask the customer to call back for the other two
- B. Forward all three concerns to a human agent because compound requests are too complex
- C. Handle each concern in a separate conversation to avoid confusion
- D. Decompose into three items, investigate them in parallel, and synthesise one resolution
Answer & explanation
Correct: D
- D is correct because multi-concern requests should be decomposed into distinct items, investigated in parallel using shared context (the customer’s account information is relevant to all three), and synthesised into a single response addressing all concerns.
- A is wrong because it fails to resolve all concerns and creates a poor customer experience by requiring the customer to call back.
- B is wrong because compound requests are a normal part of customer support. Escalating all of them to humans defeats the purpose of the agent.
- C is wrong because separate conversations lose shared context (the customer’s account information) and force the customer through multiple interaction cycles.
Question 5
Section titled “Question 5”For which of the following scenarios is prompt-based guidance (rather than programmatic enforcement) an acceptable approach?
- A. Ensuring refunds are processed only after identity verification
- B. Formatting agent responses in markdown with headers and bullet points
- C. Requiring anti-money laundering checks before international transfers
- D. Blocking account deletions without manager approval
Answer & explanation
Correct: B
- B is correct because formatting preferences are low-stakes. An occasional plain-text response instead of markdown is not a business risk. Prompt-based guidance is sufficient for style and formatting requirements.
- A is wrong because refund processing without verification is a financial risk. Programmatic enforcement is required for financial operations.
- C is wrong because AML checks are a regulatory requirement. A single missed check can result in legal penalties. Deterministic enforcement via hooks is required.
- D is wrong because account deletion is a high-stakes, irreversible operation. Programmatic enforcement (requiring manager approval token) is required to guarantee the approval step.