Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Use PreToolUse when a check must inspect or stop a proposed tool call before it runs. Use PostToolUse when the check needs the result of a successful call or should do follow-up work, such as logging, formatting, or reviewing output. A post-tool hook cannot undo or prevent an operation that has already completed.

How to choose between PreToolUse and PostToolUse

Pick the event according to when the check needs to take effect—not simply what the check is called. Anthropic describes PreToolUse as running before a tool call and PostToolUse as running after a successful call. See the Hooks reference and the hook configuration guide.

Question Use PreToolUse Use PostToolUse
Must the check stop a proposed operation before it runs? Yes. Inspect the tool and proposed input, then deny when the policy requires it. No. The tool has already completed successfully.
Does the check need the tool’s returned result? No; the result is not available yet. Yes. The callback receives tool_response.
Typical work Validate a planned file path, inspect a command, or intervene in proposed input. Review output, log a completed call, format or lint after a write, or add context.

What the Python SDK gives each hook

The Python Agent SDK passes different data depending on the event. Both hook inputs include an event name, tool name, and tool-use ID; subagent identifiers such as agent_id and agent_type are optional.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • PreToolUse: includes the proposed tool_input. Use it to examine what the tool is about to do.
  • PostToolUse: includes the input used and tool_response. Use the response when the check depends on the outcome.

Callbacks are asynchronous and receive (input, tool_use_id, context), returning a hook output. In the Python reference, the documented default timeout is 600 seconds for most events and 30 seconds for UserPromptSubmit; these are reference defaults and may change between SDK versions. Consult the Python Agent SDK reference for the version you use.

Register hooks and match the tools they should handle

In Python, configure hooks through ClaudeAgentOptions.hooks. A HookMatcher lets you limit a callback to matching tool names; for example, match Bash before execution and Write|Edit afterward.

options = ClaudeAgentOptions(
    hooks={
        "PreToolUse": [
            HookMatcher(matcher="Bash", hooks=[validate_bash_command]),
        ],
        "PostToolUse": [
            HookMatcher(matcher="Write|Edit", hooks=[post_write_handler]),
        ],
    }
)

The matcher prevents unrelated tools from invoking that callback. Anthropic’s Python SDK hooks example shows callback registration and examples for both event types. Adjust the event and matcher to the tools and SDK version in your application.

Examples: preventive checks and post-call work

Block a command before it runs

Register a PreToolUse handler for Bash, inspect the proposed command in tool_input, and return a permissionDecision of deny when it matches a prohibited pattern. This is the appropriate point for a policy that must prevent the command from executing.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Validate a planned file path

Use PreToolUse with a Write matcher to check a destination before the write occurs. Anthropic’s hook configuration guide documents this path-validation pattern.

Review a result or add context

Use PostToolUse when the callback needs the returned response. The SDK example checks tool_response for an error string and can return a reason, system message, and additional context.

Format or lint after a write

For a formatter, linter, audit log, or notification that should run after a successful write or edit, register a PostToolUse handler matched to Write|Edit. This is follow-up processing, not a way to prevent the write.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Handle failed calls with the failure event

PostToolUse is the successful-execution event. The Python SDK reference lists PostToolUseFailure separately for tool execution failures, with an error field in its input. If your requirement includes handling failed calls, evaluate that event rather than assuming a success hook covers them. Check the SDK reference for the event support and behavior in your installed version.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Implementation checklist

  1. Decide whether the check must act before execution or needs a successful result.
  2. For prevention, register PreToolUse; inspect tool_name and tool_input, and return a deny decision when appropriate.
  3. For result-dependent work, register PostToolUse and inspect tool_response.
  4. Add a matcher such as Bash, Write, or Write|Edit to limit which tools invoke the callback.
  5. If failures need their own handling, assess PostToolUseFailure.
  6. Verify callback signatures and event availability against the language SDK and version you have installed. The Python reference notes that the TypeScript SDK supports additional events not yet available in Python.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.