Intercept the agent loop
Use hooks to reject a tool call or request another model step.
Start from Control tool permissions and its configuration. Use hooks to reject a tool call or request another model step.
Intercept calls with hooks
Section titled “Intercept calls with hooks”Use hooks to add instructions at the start, refuse write_file calls after step 20 and ask for a test report before the agent finishes. Pass the exported list to createHarness({ hooks }).
Pass the list to createHarness({ hooks }). A hook that returns nothing leaves the loop unchanged.
API reference: HarnessHookPhase, HarnessHookEvents, HarnessHookDecisions and HarnessHookContext.
Order of checks and hooks
Section titled “Order of checks and hooks”- Validate input and permissions. A refusal skips the before-tool hooks.
- Run before-tool hooks in order. A hook can refuse the call or rewrite its input. Outpost revalidates rewritten input and permissions.
- Execute the allowed tool in the sandbox. Hooks cannot bypass permissions.
- Run after-tool hooks and answer the model. These hooks also run for denied or failed calls and may replace the result.
Hooks of the same phase run in declaration order. For stop, the first hook that returns { continue } wins, and the extra step still counts toward limits.maxSteps.
Lifecycle phases
Section titled “Lifecycle phases”Hooks also cover prompts, model calls, compaction and subagents. The API describes the 13 phases and their decisions.
Add this hook to your list to append an instruction to each model request. Response transformations through after-model preserve accounted usage and permissions; they cannot change streaming deltas already delivered.
Callbacks receive frozen data and must honor signal. For a script, replace run with command: JSON context on stdin, a JSON decision or empty stdout. See HarnessHookOptions for deadlines and limits.
To share these hooks, declare them in defineAgentProfile({ hooks }). The harness selects its profile section before appending local hooks.
Run the example
Section titled “Run the example”After building the package, run node examples/74-lifecycle-hooks/index.ts. The complete example checks transformations and usage without an account or paid call. For Claude, Codex and the other CLIs, see native hooks.

