Custom PostToolUse Hook for On-Demand CLAUDE.md Loading Outside Project Tree

✍️ OpenClawRadar📅 Published: April 18, 2026🔗 Source
Custom PostToolUse Hook for On-Demand CLAUDE.md Loading Outside Project Tree
Ad

CLAUDE.md Loading Behavior in Claude Code

The source details how Claude Code handles CLAUDE.md files under different conditions:

  • Hierarchical loading: When starting a session in a subdirectory, both the local CLAUDE.md and parent directory CLAUDE.md files are loaded. Example: Session in root/a/ loads both root/a/CLAUDE.md and root/CLAUDE.md.
  • On-demand child directory loading: Child directories of the current session's root directory load their CLAUDE.md files when Claude accesses files in those directories. Example: Session in root/ loads root/CLAUDE.md initially, then loads root/a/CLAUDE.md when Claude reads a file in root/a/.
  • Additional directory access via flags: Using --add-dir with CLAUDE_CODE_ADDITIONAL_DIRECTORIES_CLAUDE_MD=1 allows access to CLAUDE.md files in directories outside the project tree, but these load at session start rather than on-demand.

The Problem: External Directory CLAUDE.md Files Not Loading On-Demand

The author notes that when working in one project and instructing Claude to read files from directories outside the session's directory tree, any CLAUDE.md files in those external directories won't load automatically. This creates issues for progressive disclosure of context and requires manual intervention each time.

Ad

Solution: Custom PostToolUse Hook

The solution involves creating a PostToolUse hook that:

  • Checks if Claude is operating outside the project tree on each tool call
  • Walks up from that directory looking for CLAUDE.md files
  • If found, exits with code 2 to feed instructions back to Claude telling it to read them
  • Tracks which files have already been surfaced in a session-scoped temp file to avoid repeated instructions

A SessionStop hook cleans up the temp file used for tracking. The author provides a script called check_claude_md.py (source linked in original post) that implements this PostToolUse hook functionality.

Practical Implementation

The approach addresses the specific limitation where external directory CLAUDE.md files only load at session start when using the --add-dir flag with CLAUDE_CODE_ADDITIONAL_DIRECTORIES_CLAUDE_MD=1. By intercepting tool calls and checking for operations outside the project tree, the hook enables on-demand loading similar to how child directories work within the project tree.

This type of custom hook is useful for developers who frequently work across multiple projects or need to reference shared context files from external directories without manually instructing Claude to read CLAUDE.md files each time.

📖 Read the full source: r/ClaudeAI

Ad

👀 See Also

Essential Custom Instructions for Claude to Prevent Common Annoyances
Tips

Essential Custom Instructions for Claude to Prevent Common Annoyances

A Reddit user shares three specific custom instructions to address common Claude annoyances: requiring warnings before destructive commands, preventing mid-answer plan changes, and keeping code blocks exclusively for functional code.

OpenClawRadar
Stable OpenClaw browser automation using Chrome remote debugging and Playwright
Tips

Stable OpenClaw browser automation using Chrome remote debugging and Playwright

A developer reports success with Chrome's --remote-debugging-port=9222 flag and Playwright's chromium.connect_over_cdp() to maintain persistent browser sessions for OpenClaw, solving disconnection issues with the built-in browser and Chrome extension relay.

OpenClawRadar
Practical Habits for Critical LLM Interaction
Tips

Practical Habits for Critical LLM Interaction

A Reddit post outlines specific techniques for avoiding confirmation bias when working with LLMs, including custom prompt modes like 'strawberry' for neutral explanation and 'socrates' for adversarial scrutiny, plus evaluating training data composition.

OpenClawRadar
Claude Code Plugin Bug Causes Skills to Load Twice, Increasing Context Compaction
Tips

Claude Code Plugin Bug Causes Skills to Load Twice, Increasing Context Compaction

A bug in Claude Code causes plugins to load every skill twice due to stale cache directories and symlink duplication, significantly increasing system prompt size and triggering frequent context compaction. The source provides check scripts to identify the issue and fix scripts to remove stale plugin versions and duplicate symlinks.

OpenClawRadar