pi.on(eventName, handler) when behavior belongs around the agent lifecycle rather than inside a model-callable tool.
Make Slack responses concise
(event, ctx). event depends on the hook. ctx.external contains bounded integration metadata or null.
Select tools for a turn
Call runtime-changing methods inside a handler, not at module discovery time:Block a tool call
tool_call handlers may block a call. They may also mutate event.input in place; later handlers and execution receive the resulting input.
Annotate a tool result
Choose a hook deliberately
| Goal | Hook |
|---|---|
| Modify the prompt or select model/tools | before_agent_start |
| Allow or block a tool call | tool_call |
| Transform a tool result | tool_result |
| Transform model context messages | context |
| Adjust provider stream options | before_provider_request |
| Inspect or replace provider payload | before_provider_payload |
| Observe provider response metadata | after_provider_response |
| Participate in compaction | session_before_compact, session_compact |
| Observe model or thinking changes | model_update, thinking_level_update |