Skip to content
Claude Certified Architect – Foundations (CCAR-F) Exam Prep

Lesson 5 of 30 · 7 min

x
5/30

Agent SDK Hooks

Video lesson: CCAR-F 05/30: Agent SDK Hooks

What hooks are

Callbacks (or shell commands from settings files) that run when an event fires. They can block operations, log and audit, transform inputs and outputs, require approval, and track the session lifecycle.

Key events

EventFires whenTypical use
PreToolUseBefore a tool call; can block or modifyBlock dangerous commands, rewrite arguments
PostToolUseAfter a tool returnsAudit trail, normalise or redact output
PostToolUseFailureTool failedHandle or log errors
UserPromptSubmitA prompt is submittedInject context
StopAgent execution stopsSave state
SubagentStart / SubagentStopSubagent starts or finishesTrack and aggregate parallel work
PreCompactBefore conversation compactionArchive the transcript
PermissionRequestA tool call needs a decisionCustom permission handling

Several events (for example SessionStart, SessionEnd) are TypeScript-only as SDK callbacks; in Python they exist only as shell hooks in settings files.

Matchers

  • matcher filters by event target. For tool hooks it is the tool name: "Bash", "Write|Edit", or a regex such as ^mcp__. MCP tools are named mcp__<server>__<action>.
  • Omit the matcher to run for every event of that type.
  • Matchers match tool names, not file paths. To filter by path, inspect tool_input.file_path inside the callback.

Outputs

  • Return {} to allow with no change.
  • Control the current operation through hookSpecificOutput: for PreToolUse, permissionDecision (allow / deny / ask / defer), permissionDecisionReason, updatedInput; for PostToolUse, additionalContext and updatedToolOutput.
  • updatedInput must be inside hookSpecificOutput, and should be a new object, not a mutation. With defer, updatedInput is ignored.
  • Top-level systemMessage is shown to the user, not the model. To give the model context, use additionalContext.
  • {"async": true} lets the agent continue without waiting, but an async hook cannot block, modify or inject context. Use it for logging only.

Behaviour to remember

  • Multiple matching hooks run in parallel; precedence is deny > defer > ask > allow.
  • A hook that times out is cancelled and its output discarded. For PreToolUse, the tool call does not run and Claude is told the hook did not respond.
  • Hooks may not fire when the agent hits max_turns, because the session ends first.
  • Catch errors inside a hook instead of letting them propagate.

Exam traps

  • Using a hook for something a prompt line can do, or a prompt line for something a hook must guarantee.
  • Matching on a file path in matcher.
  • Putting updatedInput at the top level, so it is ignored.
  • Expecting an async hook to block.
  • Relying on hook order.

References

  1. Hooks in the SDK — checked Oct 16, 2026

Check your understanding

  • Block writes to .env. Event, matcher, return?Show answer

    Answer

    PreToolUse, Write|Edit, inspect tool_input.file_path, return permissionDecision: "deny" with a reason.
  • You want Claude, not the user, to see extra info after a tool call. Field?Show answer

    Answer

    additionalContext in PostToolUse hookSpecificOutput.
  • Which wins when one hook says allow and another deny?Show answer

    Answer

    deny.
Previous

Progress is saved in this browser.

Next Lesson