Buckets:
| <meta charset="utf-8" /><meta name="hf:doc:metadata" content="{"title":"Hook Events and the Agent Lifecycle","local":"hook-events-and-the-agent-lifecycle","sections":[{"title":"The Shared Lifecycle","local":"the-shared-lifecycle","sections":[],"depth":2},{"title":"Events by Platform","local":"events-by-platform","sections":[],"depth":2},{"title":"Event Input: What Your Hook Receives","local":"event-input-what-your-hook-receives","sections":[],"depth":2},{"title":"How Hooks Influence the Agent","local":"how-hooks-influence-the-agent","sections":[],"depth":2},{"title":"Choosing the Right Event","local":"choosing-the-right-event","sections":[],"depth":2},{"title":"Key Takeaways","local":"key-takeaways","sections":[],"depth":2}],"depth":1}"> | |
| <link href="/docs/context-course/pr_7/en/_app/immutable/assets/0.e3b0c442.css" rel="modulepreload"> | |
| <link rel="modulepreload" href="/docs/context-course/pr_7/en/_app/immutable/entry/start.55c3acaa.js"> | |
| <link rel="modulepreload" href="/docs/context-course/pr_7/en/_app/immutable/chunks/scheduler.4048030c.js"> | |
| <link rel="modulepreload" href="/docs/context-course/pr_7/en/_app/immutable/chunks/singletons.42509544.js"> | |
| <link rel="modulepreload" href="/docs/context-course/pr_7/en/_app/immutable/chunks/index.dd3f5310.js"> | |
| <link rel="modulepreload" href="/docs/context-course/pr_7/en/_app/immutable/chunks/paths.f6762227.js"> | |
| <link rel="modulepreload" href="/docs/context-course/pr_7/en/_app/immutable/entry/app.10ec5479.js"> | |
| <link rel="modulepreload" href="/docs/context-course/pr_7/en/_app/immutable/chunks/preload-helper.665302cf.js"> | |
| <link rel="modulepreload" href="/docs/context-course/pr_7/en/_app/immutable/chunks/index.5d0b9360.js"> | |
| <link rel="modulepreload" href="/docs/context-course/pr_7/en/_app/immutable/nodes/0.b63aa764.js"> | |
| <link rel="modulepreload" href="/docs/context-course/pr_7/en/_app/immutable/chunks/each.e59479a4.js"> | |
| <link rel="modulepreload" href="/docs/context-course/pr_7/en/_app/immutable/nodes/31.4643b140.js"> | |
| <link rel="modulepreload" href="/docs/context-course/pr_7/en/_app/immutable/chunks/MermaidChart.svelte_svelte_type_style_lang.99c2e6c8.js"> | |
| <link rel="modulepreload" href="/docs/context-course/pr_7/en/_app/immutable/chunks/CodeBlock.ff2a3276.js"> | |
| <link rel="modulepreload" href="/docs/context-course/pr_7/en/_app/immutable/chunks/HfOption.0fd5270b.js"> | |
| <link rel="modulepreload" href="/docs/context-course/pr_7/en/_app/immutable/chunks/stores.0a12db33.js"><!-- HEAD_svelte-u9bgzb_START --><meta name="hf:doc:metadata" content="{"title":"Hook Events and the Agent Lifecycle","local":"hook-events-and-the-agent-lifecycle","sections":[{"title":"The Shared Lifecycle","local":"the-shared-lifecycle","sections":[],"depth":2},{"title":"Events by Platform","local":"events-by-platform","sections":[],"depth":2},{"title":"Event Input: What Your Hook Receives","local":"event-input-what-your-hook-receives","sections":[],"depth":2},{"title":"How Hooks Influence the Agent","local":"how-hooks-influence-the-agent","sections":[],"depth":2},{"title":"Choosing the Right Event","local":"choosing-the-right-event","sections":[],"depth":2},{"title":"Key Takeaways","local":"key-takeaways","sections":[],"depth":2}],"depth":1}"><!-- HEAD_svelte-u9bgzb_END --> <p></p> <div class="items-center shrink-0 min-w-[100px] max-sm:min-w-[50px] justify-end ml-auto flex" style="float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"><div class="inline-flex rounded-md max-sm:rounded-sm"><button class="inline-flex items-center gap-1 h-7 max-sm:h-7 px-2 max-sm:px-1.5 text-sm font-medium text-gray-800 border border-r-0 rounded-l-md max-sm:rounded-l-sm border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-live="polite"><span class="inline-flex items-center justify-center rounded-md p-0.5 max-sm:p-0 hover:text-gray-800 dark:hover:text-gray-200"><svg class="sm:size-3.5 size-3" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg></span> <span>Copy page</span></button> <button class="inline-flex items-center justify-center w-6 max-sm:w-5 h-7 max-sm:h-7 disabled:pointer-events-none text-sm text-gray-500 hover:text-gray-700 dark:hover:text-white rounded-r-md max-sm:rounded-r-sm border border-l transition border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-haspopup="menu" aria-expanded="false" aria-label="Open copy menu"><svg class="transition-transform text-gray-400 overflow-visible sm:size-3.5 size-3 rotate-0" width="1em" height="1em" viewBox="0 0 12 7" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M1 1L6 6L11 1" stroke="currentColor"></path></svg></button></div> </div> <h1 class="relative group"><a id="hook-events-and-the-agent-lifecycle" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#hook-events-and-the-agent-lifecycle"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Hook Events and the Agent Lifecycle</span></h1> <p data-svelte-h="svelte-ax3cag">Before wiring anything up, it helps to know which events actually fire and when. The vocabulary is slightly different on each platform, but the lifecycle they describe is shared.</p> <iframe src="https://context-course-hook-lifecycle.static.hf.space" frameborder="0" width="850" height="450"></iframe> <h2 class="relative group"><a id="the-shared-lifecycle" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#the-shared-lifecycle"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>The Shared Lifecycle</span></h2> <p data-svelte-h="svelte-htvhmo">At the abstract level, every agent session moves through the same phases:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START -->┌─────────────────────────────────────────────┐ | |
| │ <span class="hljs-keyword">Session</span> starts │ ← SessionStart | |
| ├─────────────────────────────────────────────┤ | |
| │ Repeat <span class="hljs-keyword">for</span> <span class="hljs-keyword">each</span> turn: │ | |
| │ <span class="hljs-keyword">User</span> submits a prompt │ ← UserPromptSubmit | |
| │ Model reasons <span class="hljs-keyword">and</span> may <span class="hljs-keyword">call</span> tools │ | |
| │ <span class="hljs-keyword">Before</span> <span class="hljs-keyword">each</span> tool <span class="hljs-keyword">call</span> │ ← PreToolUse | |
| │ <span class="hljs-keyword">After</span> <span class="hljs-keyword">each</span> tool <span class="hljs-keyword">call</span> │ ← PostToolUse | |
| │ Turn ends │ ← Stop | |
| ├─────────────────────────────────────────────┤ | |
| │ <span class="hljs-keyword">Session</span> ends │ ← SessionEnd | |
| └─────────────────────────────────────────────┘<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-1ybppxi">This is the shared mental model. The platforms in this course map onto these moments with different names, different configuration surfaces, and a few gaps or extra events.</p> <h2 class="relative group"><a id="events-by-platform" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#events-by-platform"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Events by Platform</span></h2> <div class="flex space-x-2 items-center my-1.5 mr-8 h-7 !pl-0 -mx-3 md:mx-0"><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd border-gray-800 bg-black dark:bg-gray-700 text-white">Claude Code </div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">Codex </div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">OpenCode </div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">Pi </div></div> <div class="language-select"><p data-svelte-h="svelte-1qp0ci7">Claude Code has the richest event set of the four platforms. Every event uses <code>PascalCase</code> and is configured in <code>.claude/settings.json</code> (or inside a plugin’s <code>hooks/hooks.json</code>).</p> <p data-svelte-h="svelte-1qc384v"><strong>Core lifecycle events:</strong></p> <ul data-svelte-h="svelte-nfhqsy"><li><code>SessionStart</code> — New session begins (fresh, resume, or compaction continuation).</li> <li><code>InstructionsLoaded</code> — <code>CLAUDE.md</code> files have been loaded.</li> <li><code>UserPromptSubmit</code> — User submitted a prompt, before the model sees it.</li> <li><code>PreToolUse</code> — Before a tool call runs.</li> <li><code>PermissionRequest</code> — Permission prompt about to be shown.</li> <li><code>PermissionDenied</code> — A tool call was denied by the auto-mode classifier.</li> <li><code>PostToolUse</code> — After a tool call returns.</li> <li><code>PostToolUseFailure</code> — Tool call failed.</li> <li><code>Stop</code> — Turn finished.</li> <li><code>SessionEnd</code> — Session closed.</li></ul> <p data-svelte-h="svelte-5o593n"><strong>Subagent and task events:</strong></p> <ul data-svelte-h="svelte-1cli2q3"><li><code>SubagentStart</code>, <code>SubagentStop</code> — A delegated subagent starts or finishes.</li> <li><code>TaskCreated</code>, <code>TaskCompleted</code> — The TodoWrite task list changes.</li></ul> <p data-svelte-h="svelte-11gn61x"><strong>Environment events:</strong></p> <ul data-svelte-h="svelte-yvv7f9"><li><code>CwdChanged</code>, <code>FileChanged</code> — Working directory or a tracked file changed.</li> <li><code>WorktreeCreate</code>, <code>WorktreeRemove</code> — Git worktree created or removed.</li> <li><code>PreCompact</code>, <code>PostCompact</code> — Context compaction about to happen / just finished.</li> <li><code>ConfigChange</code>, <code>Notification</code> — Settings changed; Claude raised a notification.</li></ul> <p data-svelte-h="svelte-1qgx1au">Matcher groups filter on event-specific fields. Tool events match tool names (for example, <code>"matcher": "Bash"</code> or <code>"matcher": "Edit|Write"</code>), while handler-level <code>if</code> conditions can use permission-rule syntax to inspect tool arguments (for example, <code>"if": "Bash(rm *)"</code>).</p> </div> <h2 class="relative group"><a id="event-input-what-your-hook-receives" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#event-input-what-your-hook-receives"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Event Input: What Your Hook Receives</span></h2> <div class="flex space-x-2 items-center my-1.5 mr-8 h-7 !pl-0 -mx-3 md:mx-0"><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd border-gray-800 bg-black dark:bg-gray-700 text-white">Claude Code </div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">Codex </div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">OpenCode </div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">Pi </div></div> <div class="language-select"><p data-svelte-h="svelte-5s69be">Command hooks receive a JSON payload on <strong>stdin</strong>. HTTP hooks receive the same payload as the POST body. Common fields across events:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-punctuation">{</span> | |
| <span class="hljs-attr">"session_id"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"..."</span><span class="hljs-punctuation">,</span> | |
| <span class="hljs-attr">"transcript_path"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"/path/to/transcript.jsonl"</span><span class="hljs-punctuation">,</span> | |
| <span class="hljs-attr">"cwd"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"/path/to/project"</span><span class="hljs-punctuation">,</span> | |
| <span class="hljs-attr">"permission_mode"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"default"</span><span class="hljs-punctuation">,</span> | |
| <span class="hljs-attr">"hook_event_name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"PreToolUse"</span> | |
| <span class="hljs-punctuation">}</span><!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-rjvz1d">Tool-related events add <code>tool_name</code>, <code>tool_input</code>, and (on <code>PostToolUse</code>) <code>tool_response</code>. Subagent events add <code>agent_id</code> and <code>agent_type</code>. The project root is also available as an environment variable <code>CLAUDE_PROJECT_DIR</code>.</p> </div> <h2 class="relative group"><a id="how-hooks-influence-the-agent" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#how-hooks-influence-the-agent"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>How Hooks Influence the Agent</span></h2> <p data-svelte-h="svelte-1rxeadj">Hooks are not purely observational — they can change what happens next.</p> <div class="flex space-x-2 items-center my-1.5 mr-8 h-7 !pl-0 -mx-3 md:mx-0"><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd border-gray-800 bg-black dark:bg-gray-700 text-white">Claude Code </div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">Codex </div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">OpenCode </div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">Pi </div></div> <div class="language-select"><p data-svelte-h="svelte-1fzmtmh"><strong>Exit codes (command hooks):</strong></p> <ul data-svelte-h="svelte-f9asw9"><li>Exit <code>0</code> — allow, no change.</li> <li>Exit <code>2</code> with a message on stderr — block or continue per event semantics (e.g. block the tool call on <code>PreToolUse</code>, erase the prompt on <code>UserPromptSubmit</code>).</li></ul> <p data-svelte-h="svelte-9m8d3l"><strong>JSON on stdout</strong> for finer control:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-punctuation">{</span> | |
| <span class="hljs-attr">"hookSpecificOutput"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span> | |
| <span class="hljs-attr">"hookEventName"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"PreToolUse"</span><span class="hljs-punctuation">,</span> | |
| <span class="hljs-attr">"permissionDecision"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"deny"</span><span class="hljs-punctuation">,</span> | |
| <span class="hljs-attr">"permissionDecisionReason"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"No network access in this project"</span> | |
| <span class="hljs-punctuation">}</span> | |
| <span class="hljs-punctuation">}</span><!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-1qiqgin">Also supported: top-level <code>continue</code>, <code>stopReason</code>, <code>suppressOutput</code>, <code>systemMessage</code>, and the legacy <code>{ "decision": "block", "reason": "..." }</code> form.</p> <p data-svelte-h="svelte-1xms62"><strong>HTTP hooks</strong> return the same JSON as a 2xx response body. Non-2xx responses or timeouts are treated as non-blocking errors.</p> </div> <h2 class="relative group"><a id="choosing-the-right-event" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#choosing-the-right-event"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Choosing the Right Event</span></h2> <p data-svelte-h="svelte-bjxnxv">A quick reference for common goals:</p> <ul data-svelte-h="svelte-1qzcb79"><li><strong>Log every tool call</strong> → <code>PreToolUse</code> / <code>tool.execute.before</code> / <code>tool_call</code>.</li> <li><strong>Run a linter after an edit</strong> → <code>PostToolUse</code> (match <code>Edit|Write</code>) / <code>tool.execute.after</code> / <code>tool_result</code>.</li> <li><strong>Block dangerous commands</strong> → <code>PreToolUse</code> with exit code <code>2</code>, a thrown error, or <code>tool_call</code> with <code>{ block: true }</code>.</li> <li><strong>Inject repo context on each turn</strong> → <code>UserPromptSubmit</code> with <code>additionalContext</code> (Claude Code / Codex), the <code>event</code> callback on <code>message.updated</code> (OpenCode), or <code>before_agent_start</code> (Pi).</li> <li><strong>Persist conversation state</strong> → <code>Stop</code> / <code>SessionEnd</code>, <code>session.idle</code> on OpenCode, or <code>agent_end</code> / <code>session_shutdown</code> on Pi.</li> <li><strong>Add environment variables to shells</strong> → <code>shell.env</code> (OpenCode), or mutate <code>tool_call</code> / wrap user bash handling in an extension on Pi.</li></ul> <h2 class="relative group"><a id="key-takeaways" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#key-takeaways"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Key Takeaways</span></h2> <p data-svelte-h="svelte-ry6onu">The platforms expose the same underlying lifecycle with different vocabulary and different levels of granularity. Claude Code has the richest event set and supports four handler types. Codex has a smaller, tightly focused set behind a feature flag. OpenCode folds hooks into its plugin system as typed function keys rather than a JSON config. Pi uses extension events such as <code>before_agent_start</code>, <code>tool_call</code>, and <code>tool_result</code>. Once you pick an event for the goal at hand, the rest of the work is the same across them.</p> <p data-svelte-h="svelte-qkrs6j">Next, a quick quiz before we wire these events into a live Gradio dashboard.</p> <a class="!text-gray-400 !no-underline text-sm flex items-center not-prose mt-4" href="https://github.com/huggingface/context-course/blob/main/units/en/unit5/hook-events.mdx" target="_blank"><svg class="mr-1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M31,16l-7,7l-1.41-1.41L28.17,16l-5.58-5.59L24,9l7,7z"></path><path d="M1,16l7-7l1.41,1.41L3.83,16l5.58,5.59L8,23l-7-7z"></path><path d="M12.419,25.484L17.639,6.552l1.932,0.518L14.351,26.002z"></path></svg> <span data-svelte-h="svelte-zjs2n5"><span class="underline">Update</span> on GitHub</span></a> <p></p> | |
| <script> | |
| { | |
| __sveltekit_ck2jc = { | |
| assets: "/docs/context-course/pr_7/en", | |
| base: "/docs/context-course/pr_7/en", | |
| env: {} | |
| }; | |
| const element = document.currentScript.parentElement; | |
| const data = [null,null]; | |
| Promise.all([ | |
| import("/docs/context-course/pr_7/en/_app/immutable/entry/start.55c3acaa.js"), | |
| import("/docs/context-course/pr_7/en/_app/immutable/entry/app.10ec5479.js") | |
| ]).then(([kit, app]) => { | |
| kit.start(app, element, { | |
| node_ids: [0, 31], | |
| data, | |
| form: null, | |
| error: null | |
| }); | |
| }); | |
| } | |
| </script> | |
Xet Storage Details
- Size:
- 31.3 kB
- Xet hash:
- 4482b59b2b6c50b260ba9a8fc75ca006629c41d3b539bf4e2a83cdf058c33a7a
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.