agentwithhooks

June 25, 2026 ยท View on GitHub

Demonstrates every middleware hook point in the SDK with realistic transformations:

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

Hook activity is printed to stderr with a [hooks] prefix so you can see when each hook fires without mixing into the assistant reply.

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 alice@example.com. What is the return policy?"

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.