[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"skill:01M6KVWBY70JBAHE7W6T7KK521":3,"skill-same:01M6KVWBY70JBAHE7W6T7KK521":73},{"skillNo":4,"name":5,"nameZh":6,"description":7,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":16,"categories":17,"keywords":18,"publishedAt":39,"linkOnly":40,"markdownContent":41,"markdownBody":42,"frontMatter":43,"refName":44,"commitSha":45,"skillDirectory":46,"forksCount":47,"repositoryHtmlUrl":48,"repositoryHomepageUrl":49,"ownerHtmlUrl":50,"ownerBlogUrl":51,"ownerDescription":6,"updatedAt":52,"install":53,"resources":66},"01M6KVWBY70JBAHE7W6T7KK521","claude-sdk-hooks",null,"Hooks in the Claude Agent SDK — PreToolUse, PostToolUse, UserPromptSubmit, Stop event types, the HookMatcher signature, hook function signatures, blocking vs auditing patterns, and when hooks beat permission callbacks. Read when adding deterministic guards around tool calls, when implementing audit logging, when injecting context dynamically, or when can_use_tool isn't enough.","Folken2\u002Fnuvel","Folken2","https:\u002F\u002Favatars.githubusercontent.com\u002Fu\u002F175213316?v=4","01M6KVPA04RKNPCC04H2G96FAS","Albert Folch","01M6KVPA43VNTA4PXSEB1TCD81",2,"MIT","https:\u002F\u002Fgithub.com\u002FFolken2\u002Fnuvel\u002Fblob\u002Fmain\u002Fnuvel\u002Fbackends\u002Fclaude_agent_sdk\u002Fskills\u002Fclaude-sdk-hooks\u002FSKILL.md",[],[19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38],"claude sdk hooks","hooks","sdk","permission","implementing","can","calls","hook","event","types","Callbacks","agent sdk","audit logging","signatures","deterministic","claude agent sdk","use","read","tool","audit","2026-11-10 10:12:30",false,"---\nname: claude-sdk-hooks\ndescription: Hooks in the Claude Agent SDK — PreToolUse, PostToolUse, UserPromptSubmit, Stop event types, the HookMatcher signature, hook function signatures, blocking vs auditing patterns, and when hooks beat permission callbacks. Read when adding deterministic guards around tool calls, when implementing audit logging, when injecting context dynamically, or when can_use_tool isn't enough.\n---\n\n# Hooks in the Claude Agent SDK\n\nHooks are deterministic Python functions that run at fixed points in the agent's lifecycle. They're independent from `can_use_tool` (which is a single callback for permissions) — hooks are a richer system with multiple event types, multiple registered handlers per event, and the ability to inject context, rewrite inputs, or block execution.\n\nUse hooks when you need: audit logging, deterministic safety checks, dynamic context injection, redaction before logging, or rate limiting at a layer below tool implementation.\n\n## The minimal example\n\n```python\nfrom claude_agent_sdk import ClaudeAgentOptions, ClaudeSDKClient\nfrom claude_agent_sdk.types import HookMatcher\n\nasync def audit(input_data, tool_use_id, context):\n    print(f\"[audit] {input_data['tool_name']}({input_data['tool_input']})\")\n    return {}\n\noptions = ClaudeAgentOptions(\n    hooks={\"PreToolUse\": [HookMatcher(matcher=None, hooks=[audit])]},\n)\n```\n\nThree pieces:\n\n1. **Event name** (`PreToolUse`) — the dict key in `hooks`.\n2. **`HookMatcher`** — pairs a regex `matcher` with one or more hook functions. `matcher=None` means \"every tool\"; `matcher=\"Bash\"` means \"only Bash\"; `matcher=\"Bash|Write\"` means either.\n3. **The hook function** — async, signature `(input_data, tool_use_id, context) -> dict`.\n\nReturning `{}` is \"do nothing, proceed.\" Other returns (below) can block, deny with a reason, or rewrite input.\n\n## Event types\n\n| Event | When it fires | Use for |\n|-------|--------------|---------|\n| `PreToolUse` | Before a tool call executes | Block, deny with reason, rewrite input, audit |\n| `PostToolUse` | After a tool call returns | Audit results, redact, post-process |\n| `UserPromptSubmit` | Each user message | Inject context, log, redact |\n| `Stop` | Agent ends a turn | Final cleanup, persist state |\n| `SubagentStop` | A subagent ends | Same as Stop, scoped to subagents |\n| `PreCompact` | Before context compaction | Save important context that's about to drop |\n\nUnlike `can_use_tool` (one callback, permissions only), each hook event can have multiple `HookMatcher` entries, each with different matchers and different handler lists. Order is preserved; first deny wins.\n\n## Blocking with a reason\n\n```python\nasync def block_dangerous(input_data, tool_use_id, context):\n    if input_data[\"tool_name\"] == \"Bash\":\n        cmd = input_data[\"tool_input\"].get(\"command\", \"\")\n        if \"rm -rf\" in cmd:\n            return {\n                \"hookSpecificOutput\": {\n                    \"hookEventName\": \"PreToolUse\",\n                    \"permissionDecision\": \"deny\",\n                    \"permissionDecisionReason\": f\"Blocked dangerous pattern: rm -rf\",\n                }\n            }\n    return {}\n```\n\n`permissionDecision` accepts `\"allow\"` (run it, even if `permission_mode` would have prompted) or `\"deny\"` (block it, surface the reason to Claude). `permissionDecisionReason` is shown to Claude, which usually causes it to apologize and try a different approach. Keep reasons short and actionable.\n\n## Hooks vs `can_use_tool`\n\n| You want… | Use hooks | Use `can_use_tool` |\n|-----------|-----------|--------------------|\n| Permission decisions only | ✓ (PreToolUse) | ✓ |\n| Audit every tool call | ✓ | ✗ (no post-call hook) |\n| Different policies per tool with regex | ✓ (`HookMatcher`) | ✗ (one global function) |\n| Inject context on user messages | ✓ (UserPromptSubmit) | ✗ |\n| Multiple stacked policies | ✓ (multiple matchers per event) | ✗ |\n\n`can_use_tool` is simpler when permissions are the only concern. Hooks scale better as the agent grows. Production agents usually have hooks; toy agents usually don't.\n\n## Auditing without blocking\n\nThe most common hook pattern: log every tool call without changing behavior.\n\n```python\nimport logging\nlogger = logging.getLogger(\"audit\")\n\nasync def audit_pre(input_data, tool_use_id, context):\n    logger.info(\"tool_call.start\", extra={\n        \"tool_name\": input_data[\"tool_name\"],\n        \"tool_input\": input_data[\"tool_input\"],\n        \"tool_use_id\": tool_use_id,\n    })\n    return {}\n\nasync def audit_post(input_data, tool_use_id, context):\n    logger.info(\"tool_call.end\", extra={\n        \"tool_name\": input_data[\"tool_name\"],\n        \"tool_use_id\": tool_use_id,\n    })\n    return {}\n\noptions = ClaudeAgentOptions(hooks={\n    \"PreToolUse\":  [HookMatcher(matcher=None, hooks=[audit_pre])],\n    \"PostToolUse\": [HookMatcher(matcher=None, hooks=[audit_post])],\n})\n```\n\nPair this with structured logging (`LOG_FORMAT=json`) and you have replayable agent traces — useful for both debugging and eval pipelines.\n\n## Common patterns\n\nFor deeper patterns — stacked hooks, dynamic context injection in UserPromptSubmit, rate limiting, redaction in PostToolUse, hook ordering and deny precedence — see `references\u002Fhook-patterns.md`.\n\n## Quick reference\n\n```python\nfrom claude_agent_sdk import ClaudeAgentOptions\nfrom claude_agent_sdk.types import HookMatcher\n\nasync def my_hook(input_data, tool_use_id, context):\n    return {}  # or block, or rewrite\n\noptions = ClaudeAgentOptions(\n    hooks={\n        \"PreToolUse\":  [HookMatcher(matcher=\"Bash\", hooks=[my_hook])],\n        \"PostToolUse\": [HookMatcher(matcher=None,   hooks=[my_hook])],\n    },\n)\n```\n\nBlock-with-reason return:\n```python\nreturn {\"hookSpecificOutput\": {\n    \"hookEventName\": \"PreToolUse\",\n    \"permissionDecision\": \"deny\",\n    \"permissionDecisionReason\": \"...\",\n}}\n```\n","# Hooks in the Claude Agent SDK\n\nHooks are deterministic Python functions that run at fixed points in the agent's lifecycle. They're independent from `can_use_tool` (which is a single callback for permissions) — hooks are a richer system with multiple event types, multiple registered handlers per event, and the ability to inject context, rewrite inputs, or block execution.\n\nUse hooks when you need: audit logging, deterministic safety checks, dynamic context injection, redaction before logging, or rate limiting at a layer below tool implementation.\n\n## The minimal example\n\n```python\nfrom claude_agent_sdk import ClaudeAgentOptions, ClaudeSDKClient\nfrom claude_agent_sdk.types import HookMatcher\n\nasync def audit(input_data, tool_use_id, context):\n    print(f\"[audit] {input_data['tool_name']}({input_data['tool_input']})\")\n    return {}\n\noptions = ClaudeAgentOptions(\n    hooks={\"PreToolUse\": [HookMatcher(matcher=None, hooks=[audit])]},\n)\n```\n\nThree pieces:\n\n1. **Event name** (`PreToolUse`) — the dict key in `hooks`.\n2. **`HookMatcher`** — pairs a regex `matcher` with one or more hook functions. `matcher=None` means \"every tool\"; `matcher=\"Bash\"` means \"only Bash\"; `matcher=\"Bash|Write\"` means either.\n3. **The hook function** — async, signature `(input_data, tool_use_id, context) -> dict`.\n\nReturning `{}` is \"do nothing, proceed.\" Other returns (below) can block, deny with a reason, or rewrite input.\n\n## Event types\n\n| Event | When it fires | Use for |\n|-------|--------------|---------|\n| `PreToolUse` | Before a tool call executes | Block, deny with reason, rewrite input, audit |\n| `PostToolUse` | After a tool call returns | Audit results, redact, post-process |\n| `UserPromptSubmit` | Each user message | Inject context, log, redact |\n| `Stop` | Agent ends a turn | Final cleanup, persist state |\n| `SubagentStop` | A subagent ends | Same as Stop, scoped to subagents |\n| `PreCompact` | Before context compaction | Save important context that's about to drop |\n\nUnlike `can_use_tool` (one callback, permissions only), each hook event can have multiple `HookMatcher` entries, each with different matchers and different handler lists. Order is preserved; first deny wins.\n\n## Blocking with a reason\n\n```python\nasync def block_dangerous(input_data, tool_use_id, context):\n    if input_data[\"tool_name\"] == \"Bash\":\n        cmd = input_data[\"tool_input\"].get(\"command\", \"\")\n        if \"rm -rf\" in cmd:\n            return {\n                \"hookSpecificOutput\": {\n                    \"hookEventName\": \"PreToolUse\",\n                    \"permissionDecision\": \"deny\",\n                    \"permissionDecisionReason\": f\"Blocked dangerous pattern: rm -rf\",\n                }\n            }\n    return {}\n```\n\n`permissionDecision` accepts `\"allow\"` (run it, even if `permission_mode` would have prompted) or `\"deny\"` (block it, surface the reason to Claude). `permissionDecisionReason` is shown to Claude, which usually causes it to apologize and try a different approach. Keep reasons short and actionable.\n\n## Hooks vs `can_use_tool`\n\n| You want… | Use hooks | Use `can_use_tool` |\n|-----------|-----------|--------------------|\n| Permission decisions only | ✓ (PreToolUse) | ✓ |\n| Audit every tool call | ✓ | ✗ (no post-call hook) |\n| Different policies per tool with regex | ✓ (`HookMatcher`) | ✗ (one global function) |\n| Inject context on user messages | ✓ (UserPromptSubmit) | ✗ |\n| Multiple stacked policies | ✓ (multiple matchers per event) | ✗ |\n\n`can_use_tool` is simpler when permissions are the only concern. Hooks scale better as the agent grows. Production agents usually have hooks; toy agents usually don't.\n\n## Auditing without blocking\n\nThe most common hook pattern: log every tool call without changing behavior.\n\n```python\nimport logging\nlogger = logging.getLogger(\"audit\")\n\nasync def audit_pre(input_data, tool_use_id, context):\n    logger.info(\"tool_call.start\", extra={\n        \"tool_name\": input_data[\"tool_name\"],\n        \"tool_input\": input_data[\"tool_input\"],\n        \"tool_use_id\": tool_use_id,\n    })\n    return {}\n\nasync def audit_post(input_data, tool_use_id, context):\n    logger.info(\"tool_call.end\", extra={\n        \"tool_name\": input_data[\"tool_name\"],\n        \"tool_use_id\": tool_use_id,\n    })\n    return {}\n\noptions = ClaudeAgentOptions(hooks={\n    \"PreToolUse\":  [HookMatcher(matcher=None, hooks=[audit_pre])],\n    \"PostToolUse\": [HookMatcher(matcher=None, hooks=[audit_post])],\n})\n```\n\nPair this with structured logging (`LOG_FORMAT=json`) and you have replayable agent traces — useful for both debugging and eval pipelines.\n\n## Common patterns\n\nFor deeper patterns — stacked hooks, dynamic context injection in UserPromptSubmit, rate limiting, redaction in PostToolUse, hook ordering and deny precedence — see `references\u002Fhook-patterns.md`.\n\n## Quick reference\n\n```python\nfrom claude_agent_sdk import ClaudeAgentOptions\nfrom claude_agent_sdk.types import HookMatcher\n\nasync def my_hook(input_data, tool_use_id, context):\n    return {}  # or block, or rewrite\n\noptions = ClaudeAgentOptions(\n    hooks={\n        \"PreToolUse\":  [HookMatcher(matcher=\"Bash\", hooks=[my_hook])],\n        \"PostToolUse\": [HookMatcher(matcher=None,   hooks=[my_hook])],\n    },\n)\n```\n\nBlock-with-reason return:\n```python\nreturn {\"hookSpecificOutput\": {\n    \"hookEventName\": \"PreToolUse\",\n    \"permissionDecision\": \"deny\",\n    \"permissionDecisionReason\": \"...\",\n}}\n```\n",{"name":5,"description":7},"main","41d246dd855999c6bc4be528bc8d0d1b10ac8c47","nuvel\u002Fbackends\u002Fclaude_agent_sdk\u002Fskills\u002Fclaude-sdk-hooks",1,"https:\u002F\u002Fgithub.com\u002FFolken2\u002Fnuvel","https:\u002F\u002Ffolken2.github.io\u002Fnuvel\u002F","https:\u002F\u002Fgithub.com\u002FFolken2","folch.ai","2026-11-05 22:40:38",{"source":8,"skillName":5,"commands":54,"prompt":64,"zipAvailable":65},[55,58,61],{"tool":56,"command":57},"npx","npx skills add Folken2\u002Fnuvel --skill claude-sdk-hooks",{"tool":59,"command":60},"bunx","bunx skills add Folken2\u002Fnuvel --skill claude-sdk-hooks",{"tool":62,"command":63},"pnpm","pnpm dlx skills add Folken2\u002Fnuvel --skill claude-sdk-hooks","安装技能，技能地址：https:\u002F\u002Fgithub.com\u002FFolken2\u002Fnuvel\u002Fblob\u002Fmain\u002Fnuvel\u002Fbackends\u002Fclaude_agent_sdk\u002Fskills\u002Fclaude-sdk-hooks\u002FSKILL.md",true,[67],{"path":68,"resourceType":69,"resourceTypeDesc":70,"sizeBytes":71,"sourceHtmlUrl":72},"references\u002Fhook-patterns.md","REFERENCE","参考文档",7807,"https:\u002F\u002Fgithub.com\u002FFolken2\u002Fnuvel\u002Fblob\u002Fmain\u002Fnuvel\u002Fbackends\u002Fclaude_agent_sdk\u002Fskills\u002Fclaude-sdk-hooks\u002Freferences\u002Fhook-patterns.md",[74,83,95,117,134,149],{"skillNo":75,"name":76,"nameZh":6,"description":77,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":78,"categories":79,"keywords":80,"publishedAt":39},"01M6KVZNJQ7AVMVBS205404CS7","greet","Greet a user warmly by name.","https:\u002F\u002Fgithub.com\u002FFolken2\u002Fnuvel\u002Fblob\u002Fmain\u002Ftests\u002Ftest_agent_plugins\u002Ffixtures\u002Fvalid-full\u002Fskills\u002Fgreet\u002FSKILL.md",[],[76,81,82],"user","name",{"skillNo":84,"name":85,"nameZh":6,"description":86,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":87,"categories":88,"keywords":89,"publishedAt":39},"01M6KVZHYK2C9S5GRM3PZAFDXG","hello","A simple hello skill that survives a broken mcp.json.","https:\u002F\u002Fgithub.com\u002FFolken2\u002Fnuvel\u002Fblob\u002Fmain\u002Ftests\u002Ftest_agent_plugins\u002Ffixtures\u002Fbad-mcp\u002Fskills\u002Fhello\u002FSKILL.md",[],[85,90,91,92,93,94],"MCP","hello skill","json","broken","simple",{"skillNo":96,"name":97,"nameZh":6,"description":98,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":99,"categories":100,"keywords":101,"publishedAt":39},"01M6KVZC94REQZ852EJ99ZXATK","grok-workflows","Prompt templates and workflows for using xAI GrokBot with Nuvel — code, design, research, and automated pipelines.","https:\u002F\u002Fgithub.com\u002FFolken2\u002Fnuvel\u002Fblob\u002Fmain\u002Fplugins\u002Fgrokbot-plugin\u002Fskills\u002Fgrok-workflows\u002FSKILL.md",[],[102,103,104,105,106,107,108,109,110,111,112,113,114,115,116],"workflows","grok","grok workflows","design","research","prompt","prompt templates","code","templates","xai","grokbot","pipelines","with","for","nuvel",{"skillNo":118,"name":119,"nameZh":6,"description":120,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":121,"categories":122,"keywords":123,"publishedAt":39},"01M6KVZ7BA5MJ1N614MTP2JXCQ","grok-setup","Configure xAI GrokBot with Nuvel API integration and project context for Nuvel-aligned development.","https:\u002F\u002Fgithub.com\u002FFolken2\u002Fnuvel\u002Fblob\u002Fmain\u002Fplugins\u002Fgrokbot-plugin\u002Fskills\u002Fgrok-setup\u002FSKILL.md",[],[103,124,125,115,114,126,116,127,128,129,112,130,131,132,111,133],"grok setup","setup","project","project context","api integration","configure","development","integration","api","context",{"skillNo":135,"name":136,"nameZh":6,"description":137,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":138,"categories":139,"keywords":140,"publishedAt":39},"01M6KVZ3WYQ6V6XVCRE77WT2WY","cursor-setup","Configure Cursor IDE with Nuvel rules, MCP server integration, and project settings for OrgMemory-aware development.","https:\u002F\u002Fgithub.com\u002FFolken2\u002Fnuvel\u002Fblob\u002Fmain\u002Fplugins\u002Fcursor-plugin\u002Fskills\u002Fcursor-setup\u002FSKILL.md",[],[141,142,125,131,90,143,130,144,145,129,146,147,126,114,115,148,116],"cursor","cursor setup","mcp server","server","ide","Rules","settings","project settings",{"skillNo":150,"name":151,"nameZh":6,"description":152,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":153,"categories":154,"keywords":155,"publishedAt":39},"01M6KVZ0GGSJM4AC8PK7G53H85","cursor-review","Use Cursor IDE with Nuvel OrgMemory to review changes — diff analysis, standards checking, and pre-commit review.","https:\u002F\u002Fgithub.com\u002FFolken2\u002Fnuvel\u002Fblob\u002Fmain\u002Fplugins\u002Fcursor-plugin\u002Fskills\u002Fcursor-review\u002FSKILL.md",[],[156,141,157,158,159,160,161,145,35,162,163,114,164,165,116],"cursor review","review","commit","review changes","pre-commit","analysis","diff","standards","changes","diff analysis"]