Decorative background gradient
Back to Blog
Agents MdAi Coding Agent ConfigClaude Code Setup

How to Write the Best AGENTS.md File — Complete Guide

Write an AGENTS.md file that actually improves AI coding agent behavior — structure, what to include, what to omit, and real-world examples.

How to Write the Best AGENTS.md File — Complete Guide

An AGENTS.md file exists to answer one question: what does an AI coding agent need to know that it can't figure out by reading the code? Get that scoping right and the file pays for itself on every single task; get it wrong and it's just dead weight in every context window.

Step 1: Start With Build, Test, and Run Commands

This is the highest-value section because it's the one thing an agent genuinely cannot infer reliably from the code alone — the same package.json might support multiple valid ways to run tests.

markdown

Being explicit about how to run a single test file, not just the whole suite, saves an agent from re-running an entire slow test suite after every small change.

Step 2: Document Non-Obvious Architectural Constraints

Skip anything visible from the folder structure. Focus on decisions a new engineer would need explained in onboarding.

markdown

These are the kinds of constraints that produce a genuinely wrong implementation if an agent guesses instead of being told — and they're exactly the details that don't show up from reading any single file.

Step 3: State Code Style Rules as Imperatives, Not Preferences

markdown

"Write good tests" or "keep code clean" produce inconsistent results because they're not falsifiable — an agent has no way to check if it complied. "Run npm run lint -- --fix before finishing" is a concrete, checkable instruction that gets followed reliably.

Step 4: Call Out What NOT to Touch

markdown

An explicit denylist prevents an agent from "helpfully" refactoring generated files or vendor code it doesn't have full context on.

Step 5: Add a Section for Testing Expectations

markdown

This is where a team's accumulated debugging lessons (like "don't mock the database, we got burned by it") belong — context that isn't derivable from the code but changes how an agent should validate its own work.

Step 6: Keep It Short and Prune Regularly

markdown

Because AGENTS.md is typically loaded into context on every single agent session, every line has a small but recurring token cost. A 600-line AGENTS.md that repeats information visible in package.json or the folder tree is actively worse than a 150-line one that only states what isn't otherwise discoverable.

A Minimal Template

markdown

Key Takeaways

The best AGENTS.md files contain only what an agent can't infer from reading the code itself — commands, non-obvious constraints, and checkable style rules — stated as concrete imperatives rather than vague preferences, kept under a few hundred lines since the file's token cost recurs on every single agent session.

Frequently Asked Questions

What's the difference between AGENTS.md and a regular README?

A README is written for human contributors and often includes marketing copy, badges, and installation instructions aimed at new users. AGENTS.md is written specifically for an AI coding agent and should focus on operational instructions an agent needs on every task — build and test commands, code style rules, and constraints — without the human-facing framing a README needs.

Should I put my entire architecture documentation in AGENTS.md?

No — only include what an agent can't infer by reading the code itself. A well-organized codebase already communicates its own architecture through file structure and naming; AGENTS.md should cover the parts that aren't visible from the code, like why a particular pattern was chosen, which directories are off-limits, or which commands must run before a commit.

Do different AI coding tools all read the same AGENTS.md format?

AGENTS.md itself is an emerging open convention (plain Markdown, no required schema) that multiple tools — including Claude Code, Cursor, and Aider — have converged on reading from the project root. Some tools also support their own additional config files (like CLAUDE.md for Claude Code) for tool-specific settings, but a well-written AGENTS.md is portable across most of them.

How long should AGENTS.md be?

Long enough to cover build/test commands, key constraints, and code style rules — typically well under 300 lines. Since the file is loaded into the agent's context on every session, unnecessary length has a real, recurring token cost without a matching benefit, especially for content the agent could just as easily discover by reading the code.

Working on something similar? Take a look at my services and case studies, or book a free call to talk about your idea.

Related Articles

Caching Strategies and Cache Invalidation — The Complete Guide
CachingSystem DesignPerformance

Caching Strategies and Cache Invalidation — The Complete Guide

A practical guide to caching strategies (cache-aside, write-through, write-behind) and the cache invalidation techniques that keep them from serving stale data.

September 8, 2026Read more →
Database Indexing and Read Replicas — A Practical Guide
DatabasePostgreSQLSystem Design

Database Indexing and Read Replicas — A Practical Guide

How to choose the right database indexes, avoid the ones that quietly hurt write performance, and scale reads with replicas without introducing replication lag bugs.

September 8, 2026Read more →
Load Balancing and Stateless Service Design — A Practical Guide
System DesignLoad BalancingScalability

Load Balancing and Stateless Service Design — A Practical Guide

How load balancers distribute traffic, why stateless services are what actually makes horizontal scaling work, and how to fix the sticky-session traps that quietly reintroduce state.

September 8, 2026Read more →
Session Stores and Database Connection Pooling Explained
BackendRedisDatabase

Session Stores and Database Connection Pooling Explained

Why in-memory sessions break horizontally scaled apps, how to move session state to Redis correctly, and how connection pooling keeps your database from falling over under concurrent load.

September 8, 2026Read more →
Vertical vs Horizontal Scaling: How to Choose and Implement Each
System DesignScalabilityArchitecture

Vertical vs Horizontal Scaling: How to Choose and Implement Each

A practical comparison of vertical and horizontal scaling — what each actually fixes, where each breaks down, and the architecture changes horizontal scaling requires that most guides skip.

September 8, 2026Read more →
AI Agent Guardrails and Safety — Preventing Prompt Injection and Runaway Actions
Ai Agent SafetyPrompt Injection DefenseAgent Guardrails

AI Agent Guardrails and Safety — Preventing Prompt Injection and Runaway Actions

Build practical guardrails for AI agents — prompt injection defenses, destructive-action confirmation, iteration caps, and permission scoping.

September 7, 2026Read more →

Trending Topics