Intercept and control agent behavior with hooks changedagent-sdk/hooks
Nearest release: v2.1.335, published 8 hours before upstream edited the page. Shown because the two are within 24 hours of each other. Nothing here says the release caused the edit.
Upstream edited this page at 3 Dec 2026 06:14 UTC, give or take a minute or two: the time comes from Anthropic’s own sitemap rather than from a commit. This site recorded the change at 3 Dec 2026 06:37 UTC.
Upstream edited
Recorded here
Lines+3added
Lines−3removed
From line
2
where the diff opens
First seen
14 Aug 2026
this site's first read of the page
Recorded edits34to this page, all time
The whole hunk
from line 2, old and new numbered
/
from line 2
22
33> Intercept and customize agent behavior at key execution points with hooks
44
5Hooks 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:
5Hooks are callback functions that run your code in response to agent events, like a tool being called or execution stopping. With hooks, you can:
66
77* **Block dangerous operations** before they execute, like destructive shell commands or unauthorized file access
88* **Log and audit** tool calls for compliance, debugging, or analytics
from line 794
794794* Check that your matcher pattern matches the tool name exactly
795795* Ensure the hook is under the correct event type in `options.hooks`
796796* For non-tool hooks that support matchers, like `Notification` and `SubagentStop`, matchers match against different fields, and `Stop` ignores matchers entirely (see [matcher patterns](https://code.claude.com/docs/en/hooks.md#matcher-patterns))
797* Hooks may not fire when the agent hits the [`max_turns`](https://code.claude.com/docs/en/agent-sdk/python#claudeagentoptions) limit because the session ends before hooks can execute
797* Hooks may not fire when the agent hits the [`max_turns`](https://code.claude.com/docs/en/agent-sdk/python#claudeagentoptions) limit
798798
799799### Matcher not filtering as expected
800800
from line 813
813813
814814### Hook timeout
815815
816Claude Code runs each callback with a timeout, which you set in seconds with the `timeout` field on its `HookMatcher`. When you don't set one, Claude Code uses the event's default: 600 seconds for most events, 30 seconds for `UserPromptSubmit`, `PreModelSwitch`, and `PostModelSwitch`, and 10 seconds for `MessageDisplay`. Claude Code runs `SessionEnd` callbacks during shutdown under the shorter [SessionEnd timeout budget](https://code.claude.com/docs/en/hooks.md#sessionend-input), 1.5 seconds by default.
816Claude Code runs each callback with a timeout, which you set in seconds with the `timeout` field on its `HookMatcher`. When you don't set one, Claude Code uses the event's default: 600 seconds for most events, 30 seconds for `UserPromptSubmit`, `PreModelSwitch`, and `PostModelSwitch`, and 10 seconds for `MessageDisplay`. Claude Code runs `SessionEnd` callbacks under the shorter [SessionEnd timeout budget](https://code.claude.com/docs/en/hooks.md#sessionend-input), 1.5 seconds by default.
817817
818818When a callback exceeds its timeout, Claude Code cancels it and discards its output. On the events below, Claude Code then blocks the action or continues without the callback:
819819
No line in this hunk matches that.