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: List containingworking_dirandtool_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: List containingworking_dir(current directory)Return:
HookResultPostToolUse()to continue/stop
Stop - When the agent stops
Callback signature: function(reason, context)
reason: Why the agent stopped ("complete", "max_turns", "error")context: List containingworking_dir,total_turns,costReturn:
HookResultStop()
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: List containingworking_dirReturn:
HookResultSubagentStop()
UserPromptSubmit - When a user prompt is submitted
Callback signature: function(prompt, context)
prompt: The user's prompt text (character)context: List containingworking_dirReturn: 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: List containingworking_dir,total_turns,compact_countReturn:
HookResultPreCompact()to allow/cancel or provide custom summary
SessionStart - When an agent session begins
Callback signature: function(context)
context: List containingworking_dir,permissions,provider,tools_countReturn:
HookResultSessionStart()
SessionEnd - When an agent session ends
Callback signature: function(reason, context)
reason: Why the agent stopped ("complete", "max_turns", "cost_limit", "hook_requested_stop")context: List containingworking_dir,total_turns,costReturn:
HookResultSessionEnd()
Context Structure
The context parameter is always a named list. Common fields:
working_dir: The agent's current working directorytool_annotations: (PreToolUse only) Tool annotations from ellmer if availabletotal_turns: (Stop, PreCompact, SessionEnd) Number of turns in the conversationcost: (Stop, SessionEnd) List withtotal,input_tokens,output_tokenscompact_count: (PreCompact only) Number of turns being compactedpermissions: (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()
}
))
} # }