Skip to main content
Lifecycle hooks let you customize workflow behavior at execution boundaries. Use hooks for logging, validation, modifying inputs/outputs, or adding context. Workflows have two lifecycle hooks:
  • on_start - Before workflow execution begins
  • on_end - After workflow execution completes
Child workflows and agents also have their own on_start and on_end hooks.

Defining hooks

Create hooks using the @hook decorator:
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

Attaching hooks to workflows

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.