- Observe Execution - Track steps, tool calls, token usage, and errors for logging, metrics, or debugging
- Intervene Dynamically - Inject feedback, retry failed steps, or terminate runs based on runtime conditions
- Handle Failures - Automatically retry on rate limits, errors, or stalled execution with exponential backoff
Events
Hooks receive events at different points in the agent lifecycle. Events fire in order:AgentStart → (StepStart → GenerationEnd → ToolStart → ToolEnd)* → AgentEnd.
| Event | When it fires |
|---|---|
AgentStart | Run begins. |
StepStart | New step starts. Contains step number. |
GenerationEnd | LLM generation completes. Contains message and usage. |
ToolStart | Tool execution begins. Contains tool_call. |
ToolEnd | Tool execution completes. Contains tool_call, message, stop. |
AgentStalled | No tool calls and no stop conditions met. |
AgentError | An error occurred. Contains error. |
Reacted | A hook returned a reaction. Contains hook_name, reaction. |
AgentEnd | Run completes. Contains stop_reason, result. |
Observational vs Interventional
Hooks that returnNone are observational—they don’t affect execution:
Reaction intervene in execution:
Reactions
| Reaction | Effect |
|---|---|
Continue(messages) | Continue with modified message list. |
Retry(messages) | Restart the current step with optional new messages. |
RetryWithFeedback(feedback) | Restart with a feedback message injected. |
Fail(error) | Terminate the run with an error. |
Finish(reason) | Successfully terminate the run. |
Finish > Fail > Retry/RetryWithFeedback > Continue.
Built-in Hooks
retry_with_feedback
Injects feedback and retries when a specific event occurs. Essential for handling stalled agents:backoff_on_ratelimit
Automatically retries with exponential backoff on rate limit errors. Use this in any production agent:backoff_on_error:
summarize_when_long
Manages context windows by summarizing conversation history when it grows too large:- Proactive: Summarizes before each step if tokens exceed threshold
- Reactive: If a context length error occurs, summarizes and retries
tool_metrics
Logs tool usage metrics to the platform:tool/total_count, tool/total_time, tool/success_rate, and per-tool metrics when detailed=True.
Event History
Each event provides the full run history viaevent.events. Use this for context-aware logic:
event.get_events_by_type(EventType)- All events of a typeevent.get_latest_event_by_type(EventType)- Most recent event of a type

