Writing a good CLAUDE.md

Quick Overview

The key to writing a good Claude.md file for AI agents is to ensure the main context file is concise, universally applicable, and acts as an index, explicitly guiding the agent to reference specific, up-to-date source code files (like schema.md or protocol.md) rather than relying on potentially outdated information embedded within the context itself.

Key Points: The core problem is that LLMs inherently favor context provided early and late in the prompt (recency bias), causing them to ignore vital rules if they appear in the middle of extensive context. For complex enterprise projects, context files (like Claude.md) should be concise, ideally under 150 to 200 instructions, acting as an index rather than containing all necessary operational knowledge. A key solution is to use a 'stop hook' in the agent's code (like ) that runs after code generation but before execution, allowing the agent to check if the generated code adheres to the rules. The best practice is to reference external, current source files (like schema.md or protocol.md) directly, ensuring the agent uses the most up-to-date information, rather than pasting large chunks of code into the main context. The speaker argues that the simple, rule-based linter approach is insufficient; agents need deterministic tools like Bison or ESLint to automatically fix formatting or syntax issues. The ideal structure separates high-level project understanding (the main context file) from low-level, frequently updated implementation details (referenced files).

Context: This podcast segment discusses best practices for structuring context files, specifically 'Claude.md', when developing AI agents using large language models (LLMs) like Claude. The speakers highlight the common frustration where agents, despite having clear instructions, often ignore critical rules if those rules are buried within a massive context window, leading to errors in code generation or adherence to project standards.

Detailed Analysis

The discussion centers on effective context management for AI agents, specifically using a file like 'Claude.md'. The primary takeaway is that context files should function as concise indexes rather than comprehensive manuals. The speaker notes that LLMs exhibit a strong bias towards information at the beginning and end of the context window, causing them to forget or ignore critical rules embedded in the middle, especially when the context is large (e.g., over 150-200 instructions). This leads to agents ignoring critical rules like not exposing API keys or following specific formatting guidelines, resulting in poor performance or security risks. The suggested solution involves using a 'stop hook' in the agent's code that runs post-generation but pre-execution to check rules, and structuring the project so the main context file only points to the latest source code files (like schema.md or protocol.md) for specific details, thereby keeping the main context lean and focused. The ideal is to use deterministic tools for fixing issues rather than relying on the LLM's inherent biases or simple rule-checking.

Raw markdown version of this recap