Skip to main content

Overview

Hooks intercept agent lifecycle events to observe or modify state. Each hook receives a HookContext and returns a (potentially modified) HookContext. Hooks can:
  • Observe: Log events, collect metrics without changing state
  • Modify: Inject metadata, adjust system prompts, transform messages
  • Block: Prevent tool execution (for BeforeToolUse hooks)
Key concepts:
  • CallableHook: Wraps a closure as a HookInterface
  • HookTriggers: Specifies when the hook fires (e.g., beforeStep(), afterStep())
  • HookContext: Carries AgentState, tool call info, and trigger type
  • HookStack: Registers hooks directly on AgentLoop via withInterceptor()

Example