Hooks (Control)
Intercept and modify agent behavior. Can allow, deny, or block execution.Callbacks (Observe)
Receive notifications about events. Cannot modify or block execution.Quick Comparison
| Feature | Hooks | Callbacks |
|---|---|---|
| Purpose | Control flow | Observation |
| Can modify input? | ✅ Yes | ❌ No |
| Can block execution? | ✅ Yes | ❌ No |
| Return value matters? | ✅ Yes (decisions) | ❌ No |
| When used | Before/during execution | After events |
| Typical use | Validation, policies, guardrails | Logging, UI, monitoring |
When to Use Each
- Use Hooks When
- Use Callbacks When
You need to control behavior:Examples:
- Block specific tool calls
- Modify prompts before LLM
- Validate tool arguments
- Implement rate limiting
- Enforce security policies
Code Examples
Hook: Block and Modify
Callback: Log and Notify
Decision Flow
Hook Decisions
Hooks returnHookResult with a decision:
| Decision | Effect |
|---|---|
allow() | Execution proceeds normally |
deny(reason) | Execution blocked, error returned |
block() | Silent block, no error |
ask(prompt) | Request human confirmation |
Summary Table
| Scenario | Use | Example |
|---|---|---|
| Block tools based on policy | Hook | HookResult.deny() |
| Log all interactions | Callback | register_display_callback() |
| Modify prompt before LLM | Hook | Modify event.prompt |
| Show progress in UI | Callback | Display callback |
| Validate tool arguments | Hook | Check args, return allow/deny |
| Track completion | Callback | on_task_complete |
| Rate limit calls | Hook | Count and deny after limit |
| Send webhook notifications | Callback | HTTP POST in callback |

