NewRun a task on a schedule

AGENTS.md and CLAUDE.md best practices for background coding agents

· 9 min read · Sinatra

How to write AGENTS.md and CLAUDE.md so an agent working alone from a ticket sets up the repo, runs your real checks and hands back a PR you can trust.

On this page

Most AGENTS.md and CLAUDE.md files are written for an agent running in your terminal. When it can't find the test command, it asks, or you notice and tell it. A background agent working from a ticket has nobody to ask. It clones the repo onto a fresh machine, reads the instruction file, and works out from that how to install things, what to run and when it's done.

Vendor details checked November 2026 against each tool's own docs, linked inline.

What AGENTS.md is

The agents.md site calls it "a simple, open format for guiding coding agents" and "a README for agents." There's no schema: "AGENTS.md is just standard Markdown. Use any headings you like; the agent simply parses the text you provide." It's "stewarded by the Agentic AI Foundation under the Linux Foundation," and Codex, Cursor, Jules, GitHub Copilot's coding agent and many other tools read it.

CLAUDE.md is the same idea for Claude Code. Anthropic's memory docs describe "markdown files that give Claude persistent instructions for a project," read "at the start of every session."

Neither file is enforced. Anthropic treats them as context: "They aren't enforced configuration."

Why an unattended agent needs more from the file

The agent starts with nothing installed: no node_modules, no running database, no .env you made last spring. Every setup step you do from memory is one it has to rediscover.

Then it has to verify its work. If you list testing commands, the agents.md FAQ says, "the agent will attempt to execute relevant programmatic checks and fix failures before finishing the task." OpenAI's Codex cloud environment docs agree: "If your repo includes AGENTS.md, the agent uses it to find project-specific lint and test commands." If the file names the wrong command, or none, the agent runs whatever looks plausible.

Some checks also can't run in a sandbox, like an E2E suite that needs a browser. An agent that doesn't know that fails loudly or skips them quietly, when what you want is a line in the PR saying what didn't run. In your terminal you fill these gaps without noticing. With a background coding agent they turn into PRs that fail CI, or that claim tests they never ran.

An AGENTS.md example for a background agent

This is a complete file for a small TypeScript monorepo with a Next.js app, a Fastify API and Postgres. Keep the shape and replace the specifics.

# AGENTS.md

Acme is a Next.js app (apps/web) and a Fastify API (apps/api) on Postgres,
in a pnpm workspace. Node 22, pnpm 9.

## Set up from a fresh clone

1. `corepack enable && pnpm install --frozen-lockfile`
2. `cp .env.example .env` (the defaults work locally; nothing in it is secret)
3. Start Postgres: `docker compose up -d db`. Without Docker, use a local
   Postgres 16 and set `DATABASE_URL` in `.env`.
4. `pnpm db:migrate`

Setup is done when `pnpm db:migrate` and `pnpm typecheck` both exit 0.

## Checks to run before you hand back

Run from the repo root, in this order. All three should pass; the exception is below.

1. `pnpm typecheck`
2. `pnpm lint`
3. `pnpm test` (needs Postgres; it creates and uses `acme_test`)

While working, `pnpm --filter @acme/web test` runs one package. Run the full
`pnpm test` once at the end.

## What can't run here

- `pnpm e2e` needs a browser and the staging API. Don't run it. If your
  change touches checkout or sign-in, say in the PR that E2E wasn't run.
- There are no staging or production credentials here. Don't look for them.

If a check fails for a reason unrelated to your change, don't fix it in the
same PR. Say which command failed and paste the first error.

## Conventions

- API routes live in `apps/api/src/routes/`, one file per resource.
- Money is integer cents everywhere. Never use floats for amounts.
- Database access goes through `packages/db`. Don't import Prisma in apps.

## Don't

- Edit a migration in `packages/db/migrations/` that's already on main.
  Add a new one.
- Edit `packages/api-client/src/generated/`. Run `pnpm codegen` instead.
- Add a dependency without saying why in the PR description.

## Pull requests

- One ticket, one PR. Link the ticket.
- List the commands you ran and whether each passed, and any check you didn't run and why.

It's about 50 lines, and every section tells the agent something it can do or check.

What to put in it

Write setup for a machine that has never seen your repo, and end it with one command that proves setup worked, so the agent knows when to stop installing.

Name the checks exactly and in order. "Run the tests" is ambiguous in a monorepo with four test commands. Anthropic's version of this advice: write "Run npm test before committing" instead of "Test your changes."

Say what can't run in the sandbox and what to do about failures that aren't the agent's, or it may "fix" a flaky test in a PR about something else. Add only the conventions you'd actually point out in review. And ask for the commands and results in the PR description, which is much easier to review than "all tests pass."

What to leave out

Leave out secrets. Anyone who can read the repo can read the file, and so can every agent. Name the environment variable and let the platform inject the value.

Leave out rules your linter already enforces. List pnpm lint as a check and skip restating its rules. OpenAI gives the same advice for Codex review rules: "reserve formatting and lint checks for CI."

Vague advice like "write clean code" can't be checked, so cut it. Anthropic's examples of the alternative are "Use 2-space indentation" instead of "Format code properly," and "API handlers live in src/api/handlers/" instead of "Keep files organized."

Keep it short. Anthropic suggests you "target under 200 lines per CLAUDE.md file," because "longer files consume more context and reduce adherence." Codex stops adding instruction files once their combined size reaches project_doc_max_bytes, "32 KiB by default."

Keep personal preferences out. Claude Code has CLAUDE.local.md for "personal project-specific preferences" (safe alongside a CLAUDE.md that imports AGENTS.md, as below), and Codex reads a global ~/.codex/AGENTS.md. And delete commands that no longer work: the agent trusts the file and will run a stale command.

AGENTS.md vs CLAUDE.md

Most coding agents read AGENTS.md. Claude Code started with CLAUDE.md, so many repos have both.

ToolWhat it reads
OpenAI CodexAGENTS.override.md, then AGENTS.md, then any names you add to project_doc_fallback_filenames
Claude CodeCLAUDE.md. Reads AGENTS.md instead when there's no CLAUDE.md, .claude/CLAUDE.md or CLAUDE.local.md in the working directory or above it (v2.1.277 and later)
GitHub CopilotOne or more AGENTS.md files anywhere in the repo, or a single CLAUDE.md or GEMINI.md at the root

So does Claude read AGENTS.md? Yes, but "by default, Claude reads AGENTS.md only when you have no CLAUDE.md in your working directory or above it." A CLAUDE.local.md counts for that check, so adding one for your own notes can stop Claude from reading the shared AGENTS.md.

Keep one source of truth

Make AGENTS.md the canonical file and point CLAUDE.md at it. Anthropic documents two ways. The first is an import:

@AGENTS.md

## Claude Code

Use plan mode for changes under `src/billing/`.

Claude reads the imported file first, then the Claude-specific lines below it. The second is a symlink, if you have nothing Claude-specific to add:

ln -s AGENTS.md CLAUDE.md

Anthropic advises against the symlink if anyone clones the repo on Windows, where Git can check it out as "a one-line CLAUDE.md in place of your instructions."

For background agents, use the import. You don't pick the Claude Code version a hosted agent runs, and Anthropic lists sessions where Claude can't read AGENTS.md directly; its fix for those is the import. Don't write "see AGENTS.md" in prose instead: with that, "Claude sees AGENTS.md only if it decides to open the file."

Nested AGENTS.md files per directory

In a monorepo, give each package a short AGENTS.md with its own commands and conventions, and keep setup and repo-wide checks in the root file. Anything that does damage if missed, like the migration rule, stays in the root file too.

AGENTS.md              setup, repo-wide checks, PR rules
apps/web/AGENTS.md     web test command, component conventions
apps/api/AGENTS.md     API test command, route conventions
packages/db/AGENTS.md  seed and fixture notes

The agents.md rule is "the closest AGENTS.md to the edited file wins; explicit user chat prompts override everything." But the tools load nested files at different times.

Claude Code loads files above the working directory at launch and a subdirectory's instructions only after it opens a file there; files are "concatenated into context rather than overriding each other," closest last. If your root CLAUDE.md imports AGENTS.md, Claude reads CLAUDE.md files only, so nested AGENTS.md files never load. Put a one-line CLAUDE.md containing @AGENTS.md next to each nested AGENTS.md.

Codex builds its chain once, at start, walking from the project root "down to your current working directory." Closer files win "because they appear later in the combined prompt," but "Codex stops searching once it reaches your current directory."

A background agent usually starts at the repo root, so on Claude Code a nested file loads late and only through its own CLAUDE.md import, and on Codex it doesn't load automatically. Put setup and the final checks in the root file.

Test the file like a new hire would

Clone the repo into a fresh container and follow only the file. Anything you had to do that it doesn't mention goes in. Then hand an agent a small, well-written ticket and read what it ran, not only the diff. If it skipped a check or ran the wrong one, fix the file.

How Sinatra uses these files

We build Sinatra, a background coding agent. Its agent sets up its own checkout and reads .sinatrarc.json, snapshot Dockerfiles and setup docs as hints, so if your setup depends on a post_clone command, describe it in your README or AGENTS.md too.

When Sinatra's self-review (its review of its own PR) pushes a fix, it re-runs whatever your agent instructions and contributing docs name as verification. If that fails because of the fix, it reverts the fix. If it fails for another reason or can't run in the sandbox, the review says which command and what blocked it.

Context shared across repositories, like dashboards, cluster endpoints and tool commands, goes in workspace agent instructions (up to 16 KiB), with credentials kept in Secrets and referenced by environment-variable name. A repository's AGENTS.md and CLAUDE.md take precedence for that repository's conventions.

Common questions

To see how an agent uses your AGENTS.md on a real ticket, start free on Sinatra and hand it one from Linear.

Assign it to Sinatra. Come back to a PR.

Sinatra picks up work from Linear, GitHub, Slack and Telegram and opens a draft pull request for your team to review. Bring your own Claude or Codex access; Sinatra never bills for model usage.