Hook events are fired at specific points during agent execution. Each event type has a specific callback signature and context structure.
Event Types
PreToolUse - Before a tool is executed (can deny)
Callback signature: function(tool_name, tool_input, context)
tool_name: Name of the tool being called (character)tool_input: Named list of arguments passed to the toolcontext: Common correlation fields plustool_call_idandtool_annotations(if available)Return:
HookResultPreToolUse()to allow/deny
PostToolUse - After a tool completes
Callback signature: function(tool_name, tool_result, tool_error, context)
tool_name: Name of the tool that was called (character)tool_result: Result returned by the tool (or NULL on error)tool_error: Error message if tool failed (or NULL on success)context: Common correlation fields plustool_call_idReturn:
HookResultPostToolUse()to continue/stop
PostToolUseFailure - After a tool reports an error
Callback signature: function(tool_name, tool_result, tool_error, context)
Same arguments as PostToolUse, fired only when
tool_erroris not NULL
Stop - When the agent stops
Callback signature: function(reason, context)
reason: Why the agent stopped (for example"complete","request_limit","cost_limit", or"provider_error")context: Common correlation fields plususageandcost; nativerun()also includestotal_turnsReturn: NULL (informational only)
SubagentStop - When a sub-agent completes (LeadAgent only)
Callback signature: function(agent_name, task, result, context)
agent_name: Name of the sub-agent that completed (character)task: The task that was delegated (character)result: Result returned by the sub-agentcontext: Common correlation fields plus parent/child Agent and run IDsReturn: NULL (informational only)
SubagentStart - When a delegated sub-agent starts (LeadAgent only)
Callback signature: function(agent_name, task, context)
agent_name: Name of the sub-agent that startedtask: The delegated taskcontext: Common correlation fields plus parent/child Agent IDs
PermissionRequest - When permission policy denies a tool call
Callback signature: function(tool_name, tool_input, permission_result, context)
Return:
PermissionResultAllow()to override the denial, orPermissionResultDeny()to replace the denial reason
ConfigChange - When runtime configuration changes
Callback signature: function(key, old_value, new_value, context)
UserPromptSubmit - When a user prompt is submitted
Callback signature: function(prompt, context)
prompt: The user's prompt text (character)context: Common correlation fieldsReturn: NULL (informational only)
Notification - Informational runtime notice
Callback signature: function(message, context)
message: The notification text (character)context: Common correlation fields pluslevel,code, and any event-specific metadataReturn: NULL (informational only)
PreCompact - Before conversation compaction
Callback signature: function(turns_to_compact, turns_to_keep, context)
turns_to_compact: List of turns that will be compacted into a summaryturns_to_keep: List of recent turns that will be preservedcontext: Common correlation fields plustotal_turnsandcompact_countReturn:
HookResultPreCompact()to allow/cancel or provide custom summary
SessionStart - When an agent session begins
Callback signature: function(context)
context: Common correlation fields pluspermissions,provider, andtools_countReturn: NULL (informational only)
SessionEnd - When an agent session ends
Callback signature: function(reason, context)
reason: Why the agent stopped (for example"complete","request_limit","cost_limit", or"hook_requested_stop")context: Common correlation fields plususageandcost; nativerun()also includestotal_turnsReturn: NULL (informational only)
Context Structure
The context parameter is always a named list. Common fields:
working_dir: The agent's current working directoryrun_context: Immutable canonical product context for the active runagent_id: Stable identifier for the Agent instanceagent_name: Optional human-readable Agent nameparent_agent_id: Parent Agent identifier for delegated runsparent_run_id: Parent run identifier for delegated runsdelegation_id: Delegation identifier for delegated runs and toolstool_annotations: (PreToolUse only) Tool annotations from ellmer if availabletool_call_id: Canonical tool lifecycle identifierusage: Run-scoped AgentUsage for tool and terminal lifecycle hooksusage_limits: Active UsageLimits for tool lifecycle hooksrun_id: Identifier for the active runtotal_turns: (native Stop, PreCompact, native SessionEnd) Conversation turnscost: (Stop, SessionEnd) List withinput,output,cached, andtotalcompact_count: (PreCompact only) Number of turns being compactedlevel: (Notification only) Informational severity such as"info"or"warning"code: (Notification only) Stable notification code when availablepermissions: (SessionStart only) The agent's permissions configurationprovider: (SessionStart only) List withnameandmodeltools_count: (SessionStart only) Number of registered tools
Examples
if (FALSE) { # \dontrun{
# PreToolUse callback example
agent$add_hook(HookMatcher$new(
event = "PreToolUse",
callback = function(tool_name, tool_input, context) {
message("Tool: ", tool_name, " in ", context$working_dir)
HookResultPreToolUse(permission = "allow")
}
))
# PostToolUse callback example
agent$add_hook(HookMatcher$new(
event = "PostToolUse",
callback = function(tool_name, tool_result, tool_error, context) {
if (!is.null(tool_error)) {
warning("Tool failed: ", tool_error)
}
HookResultPostToolUse()
}
))
} # }