Skip to main content
Registers hook groups across LLM, tools, retrieval, and memory lifecycle points. Hook activity logs to stderr with a [hooks] prefix so you can see when each hook fires without mixing into the assistant reply. Source: examples/agent_with_hooks/

What it demonstrates

HookWhat this example does
BeforeLLM / AfterLLMRedact email addresses and SSNs from prompts and responses
BeforeTool / AfterToolScrub PII from tool args and results
BeforeRetrieve / AfterRetrievePrefix queries with kb:; drop documents containing SSNs
BeforeMemoryLoad / AfterMemoryLoadRequire tenant_id in scope; wrap recalled context with a scrubbed header
BeforeMemoryStore / AfterMemoryStoreScrub PII before persist; audit log after store
The sample uses WithHooks with named hook groups. See Hooks for every hook point and RunMeta fields.

Run

From examples/:
go run ./agent_with_hooks
Default: two-run demo (store memories + prefetch retrieval + tools, then recall).
go run ./agent_with_hooks "My email is [email protected]. What is the return policy?"
Watch stderr for [hooks] lines as each hook fires.

Expected output

stdout (assistant reply):
Our return policy allows returns within 30 days. I've noted your query.
stderr ([hooks] lines, interleaved):
[hooks] BeforeLLM: redacted 0 PII items
[hooks] BeforeRetrieve: prefixed query with "kb:"
[hooks] AfterRetrieve: kept 3 documents (0 dropped)
[hooks] AfterLLM: redacted 0 PII items
Sending "My email is [email protected]..." triggers PII redaction and you’ll see redacted 1 PII items in the hook output.

Temporal

Hooks are Go functions — they run in the process that executes activities (the worker). Register the same hook groups on both the agent starter and the worker via HookOptions() (or equivalent WithHooks calls). Group names are fingerprinted for drift detection; hook logic consistency is your responsibility.

Learn more

Hooks

Hook points and RunMeta

Observability

OTLP alongside hooks

Memory

Before/after memory hooks

Retrieval

Before/after retrieve hooks