← Back to all entries
2026-08-19 💡 Tips 'n' Tricks

Hooks, Cache-Aware Prompting, and Parallel Subagent Design

Hooks, Cache-Aware Prompting, and Parallel Subagent Design — visual for 2026-08-19

💡 Claude Code Hooks: Four Event Types That Transform Your Workflow

Claude Code's settings.json supports a hooks system that fires arbitrary shell commands in response to four lifecycle events: PreToolUse, PostToolUse, Stop, and Notification. Most developers have not configured these yet — and are missing the highest-leverage customisation in the entire tool.

The four events

Three patterns worth stealing immediately

Pattern 1 — Block dangerous shell commands before they run:

# ~/.claude/settings.json (user-level) or .claude/settings.json (project-level)
{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          {
            "type": "command",
            "command": "python3 ~/.claude/hooks/block_dangerous.py"
          }
        ]
      }
    ]
  }
}

The Python script reads sys.stdin for the JSON input, checks the command field against a denylist (e.g. rm -rf /, git push --force), and exits 2 with an explanation if matched. Claude receives the explanation as a tool error and adjusts its approach.

Pattern 2 — Auto-format after every file write:

{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Write",
        "hooks": [
          {
            "type": "command",
            "command": "bash -c 'FILE=$(echo $CLAUDE_TOOL_OUTPUT | python3 -c \"import sys,json; print(json.load(sys.stdin).get(\\\"path\\\",\\\"\\\"))\"); [ -n \"$FILE\" ] && prettier --write \"$FILE\" 2>/dev/null; true'"
          }
        ]
      }
    ]
  }
}

Every file Claude writes gets formatted automatically. The hook's exit code does not block the write — formatting happens as a side effect, silently.

Pattern 3 — Push a notification when a long task finishes:

{
  "hooks": {
    "Stop": [
      {
        "matcher": "",
        "hooks": [
          {
            "type": "command",
            "command": "curl -s -X POST https://ntfy.sh/MY_TOPIC -d 'Claude finished'"
          }
        ]
      }
    ]
  }
}

An empty matcher matches every Stop event. Pair with ntfy.sh (free, self-hostable) for cross-device push notifications. You can step away from your machine and get pinged the moment Claude wraps up.

Hook exit codes matter

Exit 0 = success, Claude continues. Exit 2 = block the tool and surface the hook's stdout as an error to Claude. Any other exit code = hook failure, logged but not surfaced to Claude. The exit-2 behaviour is the interesting one — it lets you build a conversational veto layer: Claude tries a command, your hook rejects it with a reason, Claude reads the reason and revises its plan.

⭐⭐⭐ docs.anthropic.com
Claude Code hooks automation settings.json developer productivity

💡 Prompt Caching with Tool Results: The 90% Cost Cut Most Pipelines Miss

Anthropic's prompt caching saves up to 90% on input token costs for repeated content — but most teams apply it only to system prompts and miss the larger opportunity: caching tool results. In an agentic loop that reads the same file or document repeatedly across turns, caching the tool result on the first read can eliminate thousands of dollars per day at scale.

How to cache a tool result

Add "cache_control": {"type": "ephemeral"} to any content block in the messages array, including tool result blocks:

{
  "role": "user",
  "content": [
    {
      "type": "tool_result",
      "tool_use_id": "toolu_01abc",
      "content": [
        {
          "type": "text",
          "text": "... 50,000 tokens of retrieved document content ...",
          "cache_control": {"type": "ephemeral"}
        }
      ]
    }
  ]
}

On the next API call that sends the same tool result (same content, same position in the array), Anthropic's infrastructure recognises the cache hit and charges write-cache price (10% of standard) instead of input price.

Four rules for maximum cache utilisation

The nuance most documentation skips

Cache hits are only charged at write-cache price on the second and subsequent reads. The first call that populates the cache is charged at write-cache price (25% of standard for Sonnet 5). So the economics are: first call costs 25% extra vs standard, every subsequent call within 5 minutes costs ~90% less. The break-even is two calls. If a tool result will be referenced more than once in your loop — a near-certainty for any document-grounded agent — cache it.

⭐⭐⭐ docs.anthropic.com
prompt caching cost optimisation API tool results agentic loops

💡 Designing Tasks for Subagent Forking (Now On by Default)

Since Claude Code v2.1.232 shipped on August 14, subagent forking is on by default for all users. When Claude identifies independent subtasks in its plan, it can now spawn parallel subagents automatically — each with its own context window, tool access, and working scope. This changes how you should write instructions.

What Claude looks for when deciding to fork

Claude forks subagents when it detects tasks that satisfy three conditions simultaneously:

In practice: "Refactor these three modules" → likely to fork. "Refactor this module, then write tests for what you changed" → sequential, no fork. The key is data dependency.

Five patterns for fork-friendly instructions

1. Name the independence explicitly. Don't rely on Claude to infer it. Say: "These three tasks are completely independent of each other and can run in parallel." This suppresses ambiguity and speeds up the forking decision.

2. Give each subtask a distinct workspace. If subagents might write to the same directory, separate them: "Subagent A works in src/auth/, Subagent B works in src/billing/". Collisions in shared directories are the most common source of forked-agent bugs.

3. Inject peer awareness when agents share state. If agents must read a shared file (e.g. a config or schema), say so and tell each agent to treat it as read-only unless explicitly authorised to write. Claude Code's fork model does not automatically impose file-level locks.

4. Cap concurrency with --max-subagents. The default is determined by your plan tier. For cost-sensitive pipelines, set it explicitly:

claude --max-subagents 4 "Analyse all 24 log files in ./logs/ and produce a summary per file"

Without this flag, Claude may spawn one subagent per log file on a large directory — impressive, but potentially expensive.

5. Read subagent outputs explicitly before synthesis. In multi-stage pipelines, add a final instruction: "After all subagents complete, read each of their output files and synthesise a combined report." Claude will not automatically merge parallel outputs without an instruction to do so.

Cross-session @-mention is the missing link for long projects

Also introduced in v2.1.232: subagents can now be @-mentioned across sessions using their session ID. If a subagent is doing a long background task (e.g. a test suite that runs for 20 minutes), you can start a new Claude Code session, @-mention the running subagent, and ask for a status update — without interrupting it. This makes long-horizon agentic work significantly more manageable for teams doing overnight runs or CI-integrated tasks.

⭐⭐⭐ docs.anthropic.com
subagents Claude Code parallel execution agentic design forking
Source trust ratings ⭐⭐⭐ Official Anthropic  ·  ⭐⭐ Established press  ·  Community / research