AGENTS.md template for Next.js
A ready AGENTS.md for a Next.js App Router project in TypeScript. It tells coding agents where server and client code live, which commands prove a change works, and the App Router mistakes they make most often.
# AGENTS.md A Next.js (App Router) web app written in TypeScript and styled with Tailwind CSS. ## Commands - `npm run dev`: start the dev server on http://localhost:3000 - `npm run build`: production build; run this before saying a change is done - `npm run lint`: ESLint with the Next.js rules - `npx tsc --noEmit`: type-check without emitting files - `npm test`: run the test suite ## Project structure - `app/`: routes, layouts and route handlers (App Router). Server Components by default - `app/api/`: route handlers (`route.ts`) - `components/`: shared React components - `lib/`: server-side helpers, data access, third-party clients - `public/`: static assets served as-is ## Rules for agents - Components are Server Components by default. Add `"use client"` only to the smallest component that needs state, effects or browser APIs. - This project uses the App Router only. Do not write `getServerSideProps`, `getStaticProps` or anything under `pages/`. - Never read a secret in client code, and never rename a secret to `NEXT_PUBLIC_` to make it readable. - A change is not done until `npm run build` passes. ## Conventions ### Next.js (App Router) - App Router (`app/`) is the default. Only use Pages Router in an existing project that uses it. - Server Components by default. Add `"use client"` only when you need state, effects, or browser APIs. - Data fetching lives in Server Components or Route Handlers — not in `useEffect`. - Server Actions for mutations — always validate inputs with Zod/Valibot before doing work. - Use `loading.tsx` and `error.tsx` boundaries. Stream with `<Suspense>` where it helps. - `next/image` for images, `next/font` for fonts. Never `<img>` or `<link rel=stylesheet>` for Google Fonts. - Cache with `fetch` options (`next: { revalidate }`, `cache: 'force-cache'`) — don't reinvent. - Metadata goes in `generateMetadata` or a static `metadata` export, not ad-hoc. ### 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. ### 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. ### 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). ## Avoid - Putting server-only secrets in client components (they'll leak into the bundle). - Calling an API route from a Server Component on the same server — just call the function directly. - Declaration-merging global namespaces. - Using `Function`, `Object`, or `{}` as types. - Fetching in `useEffect` without cleanup/abort. - Mutating state objects directly before setting. - `dangerouslySetInnerHTML` with un-sanitized content. ## 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 Next.js. 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 Next.js projects
These are the reasons the “Rules for agents” section says what it says.
Adding "use client" to fix an error
Agents reach for "use client" the moment a hook or event handler fails in a Server Component, then the whole subtree ships to the browser. The template tells them to push the client boundary down to the smallest component that needs it.
Mixing Pages Router and App Router habits
Models trained on years of getServerSideProps examples still write them. In an App Router project those functions are silently ignored, so the page renders with no data and no error.
Reading secrets in client code
Only variables prefixed NEXT_PUBLIC_ reach the browser. An agent that reads process.env.API_KEY in a client component gets undefined and often "fixes" it by renaming the secret to be public.
Skipping the production build
A page can work in next dev and fail in next build on type errors, missing Suspense boundaries or dynamic-rendering rules. The Commands section makes the build the finishing check.
Questions
Where does AGENTS.md go in a Next.js 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.
Do I need separate rules for the Pages Router?
If your project still has a pages/ folder, say so in the Project structure section and name which router new work goes in. Agents handle a mixed project well once it is written down; they guess badly when it is not.