Building Hooks in Claude Code

April 23, 2026
Anablock
AI Insights & Innovations

Anablock is a technology and AI systems company helping businesses automate workflows, connect tools, improve lead handling, and build smarter digital growth systems. The Anablock team writes about AI implementation, automation, CRM, lead generation, SEO/AEO, and practical ways businesses can use technology to operate better and grow.

Follow Anablock on LinkedIn

anthropic-claude-code

Building Hooks in Claude Code

Hooks in Claude Code allow you to intercept and control tool calls before or after they execute. This gives you fine-grained control over what Claude can and cannot do in your development environment.

Building a Hook

Creating a hook involves four main steps:

  1. Decide on a PreToolUse or PostToolUse hook - PreToolUse hooks can prevent tool calls from executing, while PostToolUse hooks run after the tool has already been used
  2. Determine which type of tool calls you want to watch for - You need to specify exactly which tools should trigger your hook
  3. Write a command that will receive the tool call - This command gets JSON data about the proposed tool call via standard input
  4. If needed, command should provide feedback to Claude - Your command's exit code tells Claude whether to allow or block the operation

Available Tools

Claude Code provides several built-in tools that you can monitor with hooks:

  • Read - Reading file contents
  • Write - Writing to files
  • Edit - Editing existing files
  • MultiEdit - Editing multiple files
  • Grep - Searching file contents
  • List - Listing directory contents
  • Execute - Running shell commands

To see exactly which tools are available in your current setup, you can ask Claude directly for a list. This is especially useful since the available tools can change when you add custom MCP servers.

Tool Call Data Structure

When your hook command executes, Claude sends JSON data through standard input containing details about the proposed tool call:

{
  "session_id": "2d6a1e4d-6...",
  "transcript_path": "/Users/sg/...",
  "hook_event_name": "PreToolUse",
  "tool_name": "Read",
  "tool_input": {
    "file_path": "/code/queries/.env"
  }
}

Your command reads this JSON from standard input, parses it, and then decides whether to allow or block the operation based on the tool name and input parameters.

Exit Codes and Control Flow

Your hook command communicates back to Claude through exit codes:

  • Exit Code 0 - Everything is fine, allow the tool call to proceed
  • Exit Code 2 - Block the tool call (PreToolUse hooks only)

When you exit with code 2 in a PreToolUse hook, any error messages you write to standard error will be sent to Claude as feedback, explaining why the operation was blocked.

Example Use Case

A common use case is preventing Claude from reading sensitive files like .env files. Since both the Read and Grep tools can access file contents, you'd want to monitor both tool types and check if they're trying to access restricted file paths.

This approach gives you complete control over Claude's file system access while providing clear feedback about why certain operations are restricted.

Written by

Anablock
Anablock

AI Insights & Innovations

Anablock is a technology and AI systems company helping businesses automate workflows, connect tools, improve lead handling, and build smarter digital growth systems. The Anablock team writes about AI implementation, automation, CRM, lead generation, SEO/AEO, and practical ways businesses can use technology to operate better and grow.

Follow Anablock on LinkedIn

Share this article:
View all articles

Related Articles

Lead and Contact Enrichment with Ana AI featured image
September 23, 2026
This post explores how Anablock CRM's AI assistant Ana automates lead and contact enrichment — pulling verified data from Apollo.io and Hunter.io, extracting intent signals from unstructured text, and writing everything directly back to CRM records. It shows sales reps, SDRs, founders, and RevOps teams how to eliminate manual research and keep their CRM current without extra effort.
Anthropic Launches Claude Opus 5.5: Faster, Cheaper, and Safer at the Frontier featured image
September 23, 2026
Anthropic's Claude Opus 5.5 delivers 40% lower costs, 30% faster output, and the strongest safety scores of any Claude model to date — a pivotal shift for B2B teams evaluating AI infrastructure. This analysis breaks down what the release means for coding reliability, agentic safety, and the platforms built to put these gains to work.

Talk to Anablock about building AI around your workflows.

If you are ready to move from research to implementation, we can help map the right AI system around your tools, data, team, and goals.