Skip to main content

Module hooks

Module hooks 

Source
Expand description

Lifecycle hooks for the agent loop.

Hooks are callbacks invoked at well-defined points during an agent run. They allow consumers to observe, log, gate, or transform behaviour without modifying the agent loop itself.

§Hook points

  • SessionStart — at the top of Agent::run(), before any LLM call.
  • PreToolCall — before each tool dispatch (after the permission hook).
  • PostToolCall — after each tool returns.
  • PreCompact — before compaction fires.
  • PostCompact — after compaction completes.
  • SessionEnd — just before returning from Agent::run().

§Usage

use recursive::hooks::{Hook, HookEvent, HookAction, HookRegistry};

struct MyHook;
impl Hook for MyHook {
    fn on_event(&self, event: HookEvent) -> HookAction {
        match event {
            HookEvent::PreToolCall { name, .. } => {
                eprintln!("about to call {name}");
                HookAction::Continue
            }
            _ => HookAction::Continue,
        }
    }
}

let mut registry = HookRegistry::new();
registry.register(Arc::new(MyHook));

Structs§

HookRegistry
A registry of hooks that dispatches events to all registered hooks in order.
ToolTimingHook
A hook that prints tool call timing information to stderr.

Enums§

HookAction
Action a hook can request in response to an event.
HookEvent
Events emitted at lifecycle points during an agent run.

Traits§

Hook
A lifecycle hook that can observe and influence agent behaviour.