AGENTS.md template for Supabase
A ready AGENTS.md for a Next.js app on Supabase. It covers migrations, row-level security and generated types, the Supabase CLI commands, and the key and policy mistakes agents make.
# AGENTS.md A Next.js (App Router) app in TypeScript using Supabase for the Postgres database and auth. ## Commands - `npx supabase start`: start the local Supabase stack (needs Docker) - `npm run dev`: start Next.js on http://localhost:3000 - `npx supabase migration new <name>`: create a new migration file - `npx supabase db reset`: rebuild the local database from migrations. Local only - `npx supabase gen types typescript --local > types/database.ts`: regenerate types after a schema change ## Project structure - `supabase/migrations/`: SQL migrations. The only place the schema changes - `supabase/functions/`: Edge Functions - `lib/supabase/`: browser and server Supabase clients - `types/database.ts`: generated types. Never edit by hand - `app/`: Next.js routes ## Rules for agents - The service role key is used only in server code and never in a file that can reach the browser. Do not use it to get around a failing policy. - Every new table gets `enable row level security` and its policies in the same migration. - Schema changes are made only as files in `supabase/migrations/`. Regenerate `types/database.ts` afterwards. - Server code that checks who the user is calls `supabase.auth.getUser()`, not `getSession()`. ## Conventions ### Supabase - Row-Level Security is mandatory on every table. No public tables without policies. - Use typed client (`database.types.ts` generated via `supabase gen types`). - Auth: prefer server-side session reads over client-side when possible. - Use Edge Functions for logic that must not run on the client; validate all inputs. ### 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. ### PostgreSQL - UUIDv7 or bigint identity for PKs — never auto-increment integers for user-visible IDs. - Indexes on every foreign key and on columns used in WHERE/ORDER BY. Check with `EXPLAIN ANALYZE`. - Use `jsonb` (not `json`). Add GIN indexes only when you query into it. - Transactions for multi-row writes. Be explicit about isolation level when it matters. ## 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. ## 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 Supabase. 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 Supabase projects
These are the reasons the “Rules for agents” section says what it says.
The service role key in the browser
When a query returns nothing because of a policy, agents switch to the service role key. That key bypasses every policy, and in client code it is public.
Tables without row-level security
Agents create a table and move on. Without RLS and a policy, anyone holding the public key can read or change every row.
Schema changes outside migrations
Agents suggest pasting SQL into the dashboard. The change then exists in one database only, and the next reset or deploy loses it.
Trusting getSession() on the server
It reads the cookie without checking it with Supabase. Server code that protects data should call getUser().
Questions
Where does AGENTS.md go in a Supabase 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.
I use Supabase without Next.js. Does this still help?
Yes. Keep the Supabase commands, the migrations and the security rules, and replace the Next.js parts with your framework. The generator can build that combination for you.