Back to .md Directory

PreToolUse API

Available when inheriting from `ClaudeHooks::PreToolUse`:

May 2, 2026
0 downloads
0 views
ai claude
View source

PreToolUse API

Available when inheriting from ClaudeHooks::PreToolUse:

Input Helpers

Input helpers to access the data provided by Claude Code through STDIN.

📚 Shared input helpers

MethodDescription
tool_nameGet the name of the tool being used
tool_inputGet the input data for the tool
tool_use_idGet the unique identifier for this tool use (e.g., "toolu_01ABC123...")

Hook State Helpers

Hook state methods are helpers to modify the hook's internal state (output_data) before yielding back to Claude Code.

📚 Shared hook state methods

MethodDescription
approve_tool!(reason)Explicitly approve tool usage
block_tool!(reason)Block tool usage with feedback
ask_for_permission!(reason)Request user permission
update_tool_input!(updated_input)Update the tool input and automatically approve the tool (sets permissionDecision to 'allow')

Output Helpers

Output helpers provide access to the hook's output data and helper methods for working with the output state.

📚 Shared output helpers

MethodDescription
output.allowed?Check if the tool has been explicitly allowed (permission_decision == 'allow')
output.denied?Check if the tool has been denied (permission_decision == 'deny')
output.blocked?Alias for denied?
output.should_ask_permission?Check if user permission is required (permission_decision == 'ask')
output.input_updated?Check if tool input has been updated
output.permission_decisionGet the permission decision: 'allow', 'deny', or 'ask'
output.permission_reasonGet the reason for the permission decision
output.updated_inputGet the updated input (if provided)

Hook Exit Codes

Exit CodeBehavior
exit 0Operation continues<br/>STDOUT shown to user in transcript mode
exit 1Non-blocking error<br/>STDERR shown to user
exit 2Blocks the tool call<br/>STDERR shown to Claude

Exit code behaviors related to chosen output stream

Outputting to a specific stream has a different effect depending on the exit code.

[!TIP] The most common and useful cases expressed in the tables below are handled automatically by calling hook.output_and_exit. You only need to worry about this when you want very specific behavior.

ALLOW

Claude Code behavior depending on combination

Exit CodeSTDERRSTDOUT
0RUNSRUNS
1RUNSRUNS
2BLOCKSRUNS

Output visibility depending on exit code

Output Visibility / Exit Code012
STDOUT sent to Claude✅ YES✅ YES✅ YES
STDOUT shown to User❌ NO❌ NO❌ NO
STDERR sent to Claude❌ NO❌ NO✅ YES
STDERR shown to User❌ NO✅ YES✅ YES

ASK

Claude Code behavior depending on combination

Exit CodeSTDERRSTDOUT
0RUNSASKS
1RUNSASKS
2BLOCKSASKS

Output visibility depending on exit code

Output Visibility / Exit Code012
STDOUT sent to Claude✅ YES✅ YES✅ YES
STDOUT shown to User✅ YES✅ YES✅ YES
STDERR sent to Claude❌ NO❌ NO✅ YES
STDERR shown to User❌ NO✅ YES✅ YES

DENY

Claude Code behavior depending on combination

Exit CodeSTDERRSTDOUT
0BLOCKSBLOCKS
1BLOCKSBLOCKS
2BLOCKSBLOCKS

Output visibility depending on exit code

Output Visibility / Exit Code012
STDOUT sent to Claude✅ YES✅ YES✅ YES
STDOUT shown to User✅ YES✅ YES✅ YES
STDERR sent to Claude❌ NO❌ NO✅ YES
STDERR shown to User❌ NO✅ YES✅ YES

Related Documents