Skip to main content
Lifecycle hooks let you customize agent behavior at key execution points. Use hooks for logging, validation, or modifying inputs/outputs.

What are lifecycle hooks?

Hooks are functions that run at specific stages of agent execution:
  • on_start - Before agent execution begins
  • on_end - After agent execution completes
  • on_agent_step_start - Before each agent reasoning step
  • on_agent_step_end - After each agent reasoning step
  • on_tool_start - Before each tool execution
  • on_tool_end - After each tool execution

Defining hooks

Create hooks using the @hook decorator in Python or the defineHook function in TypeScript:
Hook signature:
  • ctx - WorkflowContext with execution metadata
  • hook_context - HookContext with current execution state
  • Returns - HookResult indicating what action to take

Hook results

Hooks return HookResult with three options:

1. Continue without changes

2. Continue with modifications

3. Fail and stop execution

Using hooks

Attach hooks to agents:

Hook context

Hooks receive HookContext with execution state:

Multiple hooks

Hooks run in order. If any hook fails, execution stops:
If validate_input_hook fails, redact_pii_hook and add_metadata_hook never run.

Hook execution flow

Complete agent execution with hooks: