WithHooks — the SDK calls your functions automatically across Run and Stream.
Lifecycle hooks inspect, mutate, or abort an operation. They are not error-control. For model fallback after LLM retries, extra iterations, or skipping a looping tool, use WithErrorControl.
Every hook can inspect, mutate, or abort the operation it wraps. Return an error from any hook to abort the run immediately.
Register hooks
WithHooks multiple times to register independent groups. Each group runs under its own name in RunMeta.HooksGroup:
Hook points
Every hook receives
RunMeta with RunID, Iteration, and HooksGroup for correlation.
Use cases
Guardrails and safety
Block unsafe inputs before they reach the LLM and filter toxic outputs before they reach the user:BeforeLLM, AfterLLM
PII and data privacy
Scrub sensitive data before it leaves your system or gets persisted:BeforeLLM, AfterLLM, BeforeTool, AfterTool, BeforeRetrieve, AfterRetrieve, BeforeMemoryStore, AfterMemoryLoad
Token budget and cost tracking
For a per-run token or cost cap, useWithBudget — the SDK stops or pauses the run without hook code. Use AfterLLM for custom accounting that spans users, tenants, or multiple runs:
BeforeLLM (cache read / short-circuit), AfterLLM (per-call tracking, cache write). Model fallback after a failed Generate is OnLLMFailure, not AfterLLM.
Tenant isolation for memory
Enforce that each tenant only reads and writes their own memories:BeforeMemoryLoad, BeforeMemoryStore
Audit logging
Log every operation for observability without touching business logic:BeforeLLM, AfterLLM, BeforeTool, AfterTool, BeforeRetrieve, AfterRetrieve, AfterMemoryLoad, AfterMemoryStore
Retrieval query rewriting and re-ranking
Modify the query before search and filter or re-rank results after:BeforeRetrieve (query rewriting), AfterRetrieve (filtering, re-ranking)
Use case reference
Example
Hooks
PII scrubbing, retrieval filtering, memory tenant isolation — every hook point registered
Error Control
Fallback, extend iterations, breaker — separate from WithHooks
Related
Memory
BeforeMemoryLoad and BeforeMemoryStore hooks
Retrieval
BeforeRetrieve and AfterRetrieve hooks
Budget
Built-in token and cost limits without AfterLLM
Error Control
Fallback, extend iterations, and circuit breaker — after retries