AGENTS.md template for React
A ready AGENTS.md for a React single-page app built with Vite and TypeScript. It covers component conventions, the commands that verify a change, and the React habits agents get wrong.
# AGENTS.md A React single-page app built with Vite and TypeScript, tested with Vitest. ## Commands - `npm run dev`: start Vite on http://localhost:5173 - `npm run build`: type-check and build for production - `npm run lint`: ESLint - `npm test`: Vitest in run mode - `npm run preview`: serve the production build locally ## Project structure - `src/main.tsx`: entry point; providers are mounted here - `src/components/`: reusable presentational components - `src/features/`: feature folders: components, hooks and API calls that belong together - `src/hooks/`: shared custom hooks - `src/lib/`: API client, utilities, constants ## Rules for agents - Do not use `useEffect` to derive state or to copy props into state. Compute it during render or handle it in the event. - State lives in component state or the existing store. Do not add another state library. - Test what the user sees and does. No assertions on internal state, no whole-tree snapshots. - Never edit `dist/` or generated files. ## Conventions ### React - Function components only. No class components. - Prefer composition over props drilling. Lift state only when siblings need it. - `useState` for local UI state, `useReducer` for state with multiple transitions, a store (Zustand/Jotai) only for cross-tree state. - Memoize (`useMemo`, `useCallback`, `React.memo`) only when there's a measured render cost — not prophylactically. - Effects are for syncing with external systems. If you're computing derived state in an effect, compute it in render instead. - Keys on lists must be stable IDs, never array indexes (except for static lists). - Accessibility isn't optional — buttons are `<button>`, links are `<a>`, inputs have labels. ### TypeScript - Prefer `type` for object shapes, `interface` only when extending third-party types. - Never use `any` — use `unknown` and narrow, or write a proper type. - No non-null assertions (`!`) except in tests or when a type guard just ran. - Use `readonly` for props, return types, and array params that don't mutate. - Prefer discriminated unions over enums. Enums only for stable public APIs. - Import types with `import type { ... }` to avoid runtime import cost. ### Tailwind CSS - Use utility classes; extract to a component only when a class list repeats 3+ times. - Use design tokens (`tailwind.config.ts`) — no magic `#hex` values in markup. - Use `clsx` / `cn()` for conditional classes. Don't build class strings with template literals. - Prefer `@apply` in global CSS only for truly reused primitives (buttons, inputs). ### Vitest - Co-locate test files (`*.test.ts`) with the code they test. - Prefer `describe` only when grouping helps. Flat tests are fine. - Mock at the boundary (network, fs, clock) — never mock the unit under test. ## Avoid - Fetching in `useEffect` without cleanup/abort. - Mutating state objects directly before setting. - `dangerouslySetInnerHTML` with un-sanitized content. - Declaration-merging global namespaces. - Using `Function`, `Object`, or `{}` as types. ## Guardrails - Never commit secrets, API keys, or credentials. If you spot one in a diff, stop and flag it. - Do not invent dependencies. If a package is not already in the manifest, confirm before adding it. - When a command fails, read the actual error. Do not retry blindly or bypass with `--force`. - Prefer editing existing files over creating new ones. - Run the tests and the linter before saying a task is done, and say what you ran.
The commands and folders are the common defaults for React. Change them to match your project before you commit the file: an agent will run exactly what is written here.
How to use it
Save the file as AGENTS.md in the root of your repository and commit it. Then check each tool you use:
| Tool | What to do |
|---|---|
| Claude Code | Reads AGENTS.md only when no CLAUDE.md exists in or above the folder. If you have a CLAUDE.md, put @AGENTS.md on its first line. |
| Cursor | Reads AGENTS.md from the project root, in the desktop app and the terminal. In the app, rules in .cursor/rules must be .mdc files. The terminal reads only plain .md files there. |
| Codex | Reads AGENTS.md from the root down to the folder you are in, with a shared 32 KiB limit across all of them. |
| GitHub Copilot | Reads AGENTS.md on some surfaces only. Keep a .github/copilot-instructions.md as well if your team relies on Copilot. |
Details and sources: the AGENTS.md compatibility matrix.
What agents get wrong in React projects
These are the reasons the “Rules for agents” section says what it says.
useEffect for everything
Agents derive state inside effects, fetch inside effects with no cleanup, and sync props to state. Most of it should be a computed value or an event handler. The template says so in one line.
New state library per feature
Left alone, an agent adds Zustand in one file and Context in the next. Name the one you use so it stops choosing.
Tests that test the implementation
Generated tests often assert on internal state or snapshot whole trees. The template asks for tests on what the user sees and does.
Editing generated or vendored files
Agents will happily "fix" a file in dist/ or a generated API client. List those folders as off-limits.
Questions
Where does AGENTS.md go in a React project?
In the root of the repository, next to your manifest file. Tools read it from the folder you start them in and, for most tools, from parent folders too.
Does Claude Code read AGENTS.md?
Only when there is no CLAUDE.md in or above the working folder. If you already have a CLAUDE.md, keep it and make its first line @AGENTS.md so both tools read the same rules.
How long should the file be?
Short enough that every line is something the agent would get wrong without it. Codex shares a 32 KiB budget across all the AGENTS.md files it loads, and long files get followed less closely by every tool.
Does this work for Create React App or other bundlers?
Yes. Swap the commands and the entry-point path for yours. The conventions section does not depend on Vite.