How to Write a CLAUDE.md File and AGENTS.md for Coding Agents

Every time a coding agent starts a session, it knows nothing about your project. It does not know which command runs the tests, which directories are generated, which patterns your team prefers or which mistakes previous sessions made. Instruction files solve this. A claude.md file for Claude Code, and the increasingly standard AGENTS.md file used by many other agents, give coding agents persistent project context. This guide explains what to include, what to leave out, how to structure these files and how they fit into the broader discipline of context engineering for developers.

Start with how agents actually use these files, because it shapes what belongs in them.

How Agents Use Instruction Files

When a session starts, Claude Code automatically loads CLAUDE.md files from the project root and relevant directories, plus your personal user-level file. AGENTS.md serves the same purpose for agents such as OpenAI Codex, Cursor, and many others that have adopted it as a shared convention. The content becomes part of the agent's context for the entire session.

That has two implications. First, everything in the file influences every task, so it should be broadly relevant. Second, every line consumes context space, so the file should be concise.

What to Include

Effective coding agent instructions cover:

Commands: how to install dependencies, build, run tests, run a single test, lint, type check and start the development server.

Architecture overview: a brief map of the main directories and how components relate, such as which folder holds API routes and where shared utilities live.

Conventions: naming, file organization, error handling patterns, preferred libraries and approaches to avoid.

Workflow rules: branch naming, commit message format, whether to run tests before finishing, and how to handle database migrations.

Gotchas: non-obvious facts that cause repeated mistakes, such as a generated directory that must not be edited or an environment variable required for tests.

Boundaries: actions requiring confirmation, such as modifying infrastructure files or installing new dependencies.

What to Leave Out

  • Generic advice the model already follows, such as write clean code.
  • Long documentation better kept in separate files and referenced when needed.
  • Information easily discovered by reading the code.
  • Secrets, credentials or private URLs.
  • Outdated instructions that contradict current practice.

Before and After Example

Weak instruction file: This is a web app. Please write good code and follow best practices. Use TypeScript.

Strong instruction file:

Project: customer billing dashboard, Next.js 15 app router, TypeScript strict mode, PostgreSQL via Drizzle ORM.

Commands: install with pnpm install. Run all tests with pnpm test. Run one test file with pnpm test followed by the path. Type check with pnpm typecheck. Lint with pnpm lint. Always run typecheck and the relevant tests before reporting a task complete.

Structure: app holds routes and pages. lib holds shared server utilities. components holds UI components using shadcn. db holds the schema and migrations.

Conventions: use server actions for mutations, not API routes. Validate all inputs with zod schemas in lib validation. Use the logger from lib logger, never console log. Prefer named exports.

Gotchas: files in db migrations are generated; create migrations with pnpm db generate, never edit existing migration files. Tests require DATABASE URL pointing to the test database.

Ask before: adding dependencies, changing the database schema, or modifying CI configuration.

The strong version is short, specific and immediately actionable.

AGENTS.md Example and Compatibility

An agents.md example looks much the same as a CLAUDE.md file, because both are plain markdown documents with project instructions. Teams using multiple agents often maintain one canonical file and reference it from the other, or keep shared content in AGENTS.md and agent-specific details in CLAUDE.md. Claude Code can import other files using references, which allows a CLAUDE.md to include the AGENTS.md content without duplication.

Hierarchical Instruction Files

Large repositories benefit from multiple files:

  • A root file with project-wide commands and conventions.
  • Directory-level files for subsystems with distinct rules, such as a frontend package or infrastructure folder.
  • A personal user-level file for your own preferences across projects.

Agents load the relevant files based on where they are working, which keeps context focused.

Claude Code Project Context Beyond CLAUDE.md

Claude code project context also includes:

  • Custom slash commands for repeatable workflows, such as a release checklist.
  • Skills that package detailed instructions and scripts loaded only when relevant.
  • Hooks that enforce rules deterministically, such as running a formatter after edits.
  • Subagents with specialized instructions for tasks like code review.
  • MCP servers providing access to external systems.

A good rule: put always-relevant facts in CLAUDE.md, put task-specific procedures in skills or commands, and put rules that must never be broken in hooks, because hooks are enforced while instructions are followed probabilistically.

Context Engineering for Developers

Instruction files are a core element of context engineering for developers: deliberately designing what information an agent sees, when, and in what form. The goal is to give the agent exactly the context needed for high-quality work without overwhelming it. Concise instruction files, progressive disclosure through skills, and well-organized documentation all contribute.

Maintaining Instruction Files

Treat them like code. Commit them, review changes in pull requests and update them as the project evolves.

Learn from mistakes. When an agent repeats an error, add a concise rule addressing it.

Prune regularly. Remove rules that no longer apply or that the agent already follows without being told.

Test changes. After significant edits, run a few typical tasks to confirm behavior improves.

Common Mistakes to Avoid

  • Writing an essay. Long files dilute important instructions.
  • Vague rules. Specific, verifiable instructions work; aspirational statements do not.
  • Contradictions. Conflicting rules confuse agents. Keep one source of truth.
  • Ignoring emphasis. For truly critical rules, state them clearly and explain why; agents follow rules better when the reason is given.
  • Relying on instructions for hard guarantees. Use hooks, permissions and CI checks for anything that must never happen.

Frequently Asked Questions

How long should a CLAUDE.md file be? Short enough to read in a minute or two. Many effective files are under a few hundred lines, and smaller is often better.

Should I generate it automatically? Claude Code can generate an initial file by analyzing your repository. Treat that as a draft and refine it.

Benefits and Considerations

Good instruction files produce more consistent output, fewer repeated mistakes, faster onboarding for agents and humans, and less time re-explaining context. The considerations include maintenance effort, the risk of stale instructions and context costs when files grow large.

Conclusion

A well-written claude.md file or AGENTS.md is the highest-leverage improvement most teams can make to their coding agent workflow. Include commands, architecture, conventions, workflow rules, gotchas and boundaries, keep the file concise and specific, and use skills, hooks and commands for everything else.

Key takeaways: be specific and brief, document commands first, explain critical rules, update the file when agents make mistakes and enforce hard rules with hooks. Write or revise your project's instruction file today and compare agent results on your next three tasks.