← Claude Docs

Intercept and control agent behavior with hooks - Claude Code Docs

Claude Docs · April 22, 2026
Hooks are callback functions that run your code in response to agent events, like a tool being called, a session starting, or execution stopping. With hooks, you can: Block dangerous operations before they execute, like destructive shell commands or

Detailed Analysis

Anthropic's Claude Code SDK introduces a hooks system that provides developers with deterministic, event-driven control over AI agent behavior at critical points in the agentic execution loop. Hooks are callback functions registered against named lifecycle events — such as `PreToolUse`, `PostToolUse`, `Stop`, `SessionStart`, and `Notification` — and fire automatically when those events occur, independent of any decision-making by the underlying model. Each hook receives a typed input payload containing session metadata, tool names, and tool inputs, and can return structured outputs that allow, block, modify, or redirect the operation in question. The `PreToolUse` hook, for instance, can return a `permissionDecision` of `"deny"` to halt a file write before it executes, while `PostToolUse` can append additional context back into the conversation after a tool succeeds. The system supports both Python and TypeScript SDKs, with a broader set of TypeScript-only events — including `SessionStart`, `SessionEnd`, `Setup`, `TeammateIdle`, and `WorktreeCreate` — reflecting the more mature state of the TypeScript implementation.

The architectural significance of this system lies in its inversion of control relative to how AI models typically interact with tools. Rather than allowing the model to reason its way around safety constraints — which remains a persistent vulnerability in purely prompt-based guardrails — hooks enforce rules at the infrastructure level, firing deterministically whenever their trigger conditions are met regardless of what the model decides or remembers within its context window. This distinction makes hooks structurally closer to middleware or policy enforcement layers in traditional software systems than to any AI-native construct. Matchers, implemented as regex patterns, allow developers to scope callbacks to specific tools (e.g., targeting only `Write|Edit` operations or `Bash` commands) without incurring the overhead of running validation logic on every agent event, though path-level and argument-level filtering still requires logic inside the callback itself.

The practical applications documented span a spectrum from security hardening to operational reliability. On the safety side, hooks enable blocking of destructive shell commands like `rm -rf`, preventing unauthorized `.env` file access, and requiring human approval before database writes or external API calls execute. On the operational side, hooks support automatic git checkpointing before risky operations, audit logging of every tool call for compliance purposes, post-execution linting and test enforcement, and routing of agent status notifications to external systems like Slack or PagerDuty. The `SubagentStart` and `SubagentStop` events extend this control surface into parallel and multi-agent architectures, allowing orchestrators to track spawned subagents and aggregate their outputs systematically. The `PreCompact` hook addresses a more subtle operational concern — preserving full conversation transcripts before the SDK's context compaction mechanism summarizes and truncates them.

This hooks system reflects a broader industry movement toward what might be called "agentic infrastructure" — the recognition that deploying AI agents in production environments requires the same categories of reliability engineering applied to conventional software: audit trails, policy enforcement, resource lifecycle management, and failure handling. The challenge with earlier agentic frameworks was that safety and compliance properties were often expressed as natural language instructions to the model, making them probabilistic rather than guaranteed. By embedding enforcement at the SDK level rather than the prompt level, Anthropic is positioning Claude Code as suitable for regulated or high-stakes environments where deterministic behavior is a prerequisite, not a preference. The explicit TypeScript-versus-Python feature gap also signals a staged rollout strategy, with the TypeScript SDK serving as the leading edge for newer event types while the Python SDK maintains parity on core functionality.

The hooks documentation further illustrates how Anthropic is thinking about the boundary between model autonomy and operator control in agentic deployments. The availability of a `"defer"` permission decision in TypeScript — which suspends a query and allows it to be resumed later — points toward asynchronous human-in-the-loop workflows where sensitive operations pause for approval rather than either proceeding or failing outright. Combined with the `PermissionRequest` hook, which intercepts permission dialogs before they surface to end users, this creates a programmable approval layer that enterprises can route through existing governance workflows. As AI agents move from experimental tools to production infrastructure handling real data and real operations, this kind of composable, auditable control architecture becomes less a differentiating feature and more a baseline expectation.

Read original article →