Skip to main content
Hooks let you run custom logic around tool execution — validate inputs, transform outputs, log calls, enforce permissions, or trigger side effects.

Pre-Hooks and Post-Hooks

Use pre_hook and post_hook on the @tool decorator:

Pre-Hook Signature

Pre-hooks receive the tool name and arguments before execution. They can:
  • Log the call
  • Validate arguments beyond schema checks
  • Raise an exception to prevent execution

Post-Hook Signature

Post-hooks receive the tool name, arguments, and the result after execution. They can:
  • Log or audit the result
  • Send notifications
  • Update metrics

Hook Chains

Use tool_hooks to attach a list of hooks that run in order:
All hooks in the list run before execution. If any hook raises an exception, the tool is not called.

Practical Examples

Permission Check

Result Sanitization

Timing

Async Hooks

Hooks can be async functions when used with async agent execution: