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

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 bothroot/a/CLAUDE.mdandroot/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/loadsroot/CLAUDE.mdinitially, then loadsroot/a/CLAUDE.mdwhen Claude reads a file inroot/a/. - Additional directory access via flags: Using
--add-dirwithCLAUDE_CODE_ADDITIONAL_DIRECTORIES_CLAUDE_MD=1allows 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.
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
👀 See Also

A Two-Step AI Workflow for Legacy Code Modernization
A Reddit post outlines a two-step 'reverse engineering' approach for using AI with legacy code: first extract business logic into a technology-agnostic Business Requirement Document, then use a 'Master Architect' prompt to rebuild from scratch with modern best practices.

Claude Prompt Codes Retested: L99 Sharper, OODA Narrower, ARTIFACTS Faded, and 3 New Codes to Use
A 6-month retest of L99, OODA, and ARTIFACTS prompt codes on Claude shows L99 sharper on Sonnet 4.6/Opus 4.7, OODA failing on strategic prompts, ARTIFACTS unnecessary for code, and three new codes (/skeptic, /blindspots, /decompose) earning daily use. Stack no more than 2 codes.

The Mother-In-Law Method: Weaponizing Claude's Agreeableness for Brutal Code Reviews
A Reddit user tricks Claude into harsh code reviews by framing the code as written by a hated mother-in-law, resulting in 27 issues found across 4 hostile reviewer agents after 31 minutes of deep analysis.

Claude Isn't Bad at Coding — Your Context Setup Is
After months of using Claude, one developer argues failures stem from how you structure context, not the model itself. Key improvements: separate instructions from logic, cut context noise, and use stable patterns.