Quick Start
User Interaction Flow
HookEvent
Events that hooks can subscribe to.| Event | Description |
|---|---|
BeforeTool | Before tool execution |
AfterTool | After tool execution |
BeforeAgent | Before agent processes message |
AfterAgent | After agent processes message |
BeforeLLM | Before LLM call |
AfterLLM | After LLM call |
SessionStart | New session begins |
SessionEnd | Session ends |
OnError | Error occurred |
OnRetry | Retry triggered |
OnInit | Initialization |
OnShutdown | Shutdown |
HookInput
Data provided to hooks.Builder Methods
| Method | Signature | Description |
|---|---|---|
new(event, session_id) | fn new(HookEvent, impl Into<String>) -> Self | Create input |
with_agent(name) | fn with_agent(self, impl Into<String>) -> Self | Set agent name |
with_tool(name, args) | fn with_tool(self, impl Into<String>, Value) -> Self | Set tool info |
with_message(msg) | fn with_message(self, impl Into<String>) -> Self | Set message |
with_error(err) | fn with_error(self, impl Into<String>) -> Self | Set error |
with_extra(key, value) | fn with_extra(self, impl Into<String>, Value) -> Self | Add extra data |
HookResult
Result returned from hook execution.Factory Methods
| Method | Signature | Description |
|---|---|---|
allow() | fn allow() -> Self | Allow execution |
allow_with_reason(r) | fn allow_with_reason(impl Into<String>) -> Self | Allow with reason |
deny(reason) | fn deny(impl Into<String>) -> Self | Deny execution |
block(reason) | fn block(impl Into<String>) -> Self | Block (stronger deny) |
ask(reason) | fn ask(impl Into<String>) -> Self | Ask for confirmation |
Instance Methods
| Method | Signature | Description |
|---|---|---|
is_allowed() | fn is_allowed(&self) -> bool | Check if allowed |
is_denied() | fn is_denied(&self) -> bool | Check if denied |
with_modified_input(input) | fn with_modified_input(self, HashMap) -> Self | Modify inputs |
with_context(ctx) | fn with_context(self, impl Into<String>) -> Self | Add context |
suppress() | fn suppress(self) -> Self | Suppress output |
HookDecision
Decision types for hook outputs.HookDefinition
Define individual hooks with functions and metadata.Methods
| Method | Signature | Description |
|---|---|---|
new(event, func) | fn new(HookEvent, impl Fn(&HookInput) -> HookResult) -> Self | Create definition |
with_matcher(pattern) | fn with_matcher(self, impl Into<String>) -> Self | Set matcher pattern |
with_name(name) | fn with_name(self, impl Into<String>) -> Self | Set name |
matches(target) | fn matches(&self, &str) -> bool | Check if matches target |
execute(input) | fn execute(&self, &HookInput) -> HookResult | Execute hook |
HookRegistry
Manages a collection of hooks.Methods
| Method | Signature | Description |
|---|---|---|
new() | fn new() -> Self | Create empty registry |
add_hook(event, func) | fn add_hook(&mut self, HookEvent, impl Fn) -> &mut Self | Add a hook |
add_hook_with_matcher(event, matcher, func) | fn add_hook_with_matcher(&mut self, HookEvent, impl Into<String>, impl Fn) -> &mut Self | Add hook with matcher |
add_definition(hook) | fn add_definition(&mut self, HookDefinition) -> &mut Self | Add hook definition |
remove_hook(id) | fn remove_hook(&mut self, &str) -> bool | Remove hook by ID |
enable_hook(id) | fn enable_hook(&mut self, &str) -> bool | Enable hook |
disable_hook(id) | fn disable_hook(&mut self, &str) -> bool | Disable hook |
Common Patterns
Rate Limiter Hook
Best Practices
Subscribe to specific events
Subscribe to specific events
Only subscribe to events you need - reduces overhead and improves performance.
Use priority for ordering
Use priority for ordering
Hooks with higher priority run first. Use for security checks before logging.
Keep hooks fast
Keep hooks fast
Hooks run in the hot path - avoid expensive operations.
Return clear deny reasons
Return clear deny reasons
When denying, provide actionable error messages.

