Course contents

How to write AI coding rules that actually change behavior

intermediate4 min readUpdated
  • Cursor
  • GitHub Copilot
  • Claude Code
  • Windsurf / Devin
  • Cline
  • Codex
  • Antigravity CLI
On this page

The test for every line

Before a line goes into a rule, ask one question: would the agent do something different without it?

If the answer is no, cut it. The model already knows how to write readable code, name variables and handle errors in general. Lines like "follow best practices" cost tokens and change nothing.

Lines that pass the test are almost always one of these:

  1. Facts it can't infer: versions, commands, directory layout, which library you chose.
  2. Decisions you made: "we use Zod, not Yup", "server state lives in TanStack Query".
  3. Boundaries: files not to edit, things not to upgrade, actions that need a human.
  4. Known traps: the mistakes the agent actually made in your repo, and the API it keeps reaching for that you've moved away from.

A structure that works

Every official presets.dev standards file follows roughly the same outline. It's a good default for your own rules too:

Rule outline
# <Stack> rules: <focus>

## Updated for <versions, month year>
Don't upgrade the framework unless the task asks.

## Project shape
Where things live. What to read before editing.

## <Two to four topic sections>
The conventions that matter for this stack: data, state, errors, security.

## Tests
Which runner, where tests go, what a change must cover.

## Mistakes to avoid
Short bullets of real failure modes.

## Deprecated APIs to never generate
Old names and their replacements.

## Commands
The exact commands to build, test and lint.

## Example
One short snippet showing the conventions together.

Three sections do most of the work:

  • "Updated for" pins the version. It tells the agent which era of the framework you're in and stops speculative upgrades.
  • "Mistakes to avoid" turns past failures into guardrails.
  • "Deprecated APIs to never generate" fights the model's training data directly. Models have seen far more old code than new code.

Write instructions, not opinions

Compare these pairs:

VagueSpecific
Handle errors properly.Return { ok: false, error } from Server Actions. Never throw a stack trace to the client.
Write tests.Add a Vitest case next to the file as *.test.ts for every behavior change. Run pnpm vitest run <file>.
Be secure.Take the user and tenant id from the session, never from the request body.
Use modern React.Don't add useEffect to sync derived state; compute it during render.

Each specific version names a pattern, a place and often a command. The agent can follow it and you can check it.

Show one example, not ten

A short example that combines your conventions is worth more than a long list of rules. Keep it to 10 to 30 lines and make sure it compiles against your real code style.

Example block in a Next.js rule
'use server'

export async function saveNote(input: { body?: unknown }) {
  const session = await requireSession()
  if (!session) return { ok: false as const, error: 'Sign in required' }
  if (typeof input.body !== 'string') return { ok: false as const, error: 'Note text is required' }
  const body = input.body.trim()
  if (body.length < 1 || body.length > 2000) {
    return { ok: false as const, error: 'Note text must be 1 to 2000 characters' }
  }
  await db.note.create({ data: { tenantId: session.tenantId, authorId: session.userId, body } })
  updateTag(`tenant:${session.tenantId}:notes`)
  return { ok: true as const }
}

This one example teaches four rules at once: check the session first, take ids from the session, validate and cap input, and return small result objects. It's adapted from our Next.js rules.

Before and after

Here's a real-world style first draft for a Python service:

Before
# Python rules
- Write Pythonic code.
- Use type hints.
- Follow PEP 8.
- Write good tests.
- Handle exceptions.

Every line is either something the model already does or something your linter enforces. Here's the rewrite:

After
# Python rules: uv, Ruff and pytest

## Updated for Python 3.13, uv and Ruff, Nov 2026
Don't change the interpreter version or add a new tool unless asked.

## Project shape
- Run everything through uv: `uv run pytest`, `uv run ruff check`.
- App code in `src/app/`, tests in `tests/` mirroring the same paths.

## Boundaries
- Parse external input with the Pydantic models in `src/app/schemas/`.
- Don't catch bare `Exception` in request handlers. Let the error middleware in `src/app/errors.py` map it.

## Tests
- New behavior needs a pytest case. Use the fixtures in `tests/conftest.py`.
- Don't hit the network in tests. Use `respx` mocks.

## Never generate
- `pip install` or `requirements.txt` edits. Use `uv add`.
- `from typing import List, Dict` for new code. Use built-in `list` and `dict`.

## Commands
- `uv run pytest -q`
- `uv run ruff check --fix`

What to leave out

  • Style a linter enforces. Formatting, import order and naming belong in ESLint, Ruff or Prettier. Cursor's own docs say the same: use a linter, not a copied style guide (Cursor rules best practices).
  • Every command the tool already knows. You don't need to explain git status.
  • Rare edge cases. If it comes up once a quarter, it belongs in a skill or a doc the rule points to.
  • Copied code. Point to a canonical file instead ("follow the pattern in src/api/users.ts"). Copies go stale.

How long should a rule be?

Shorter than you think. Official guidance across tools:

  • Cursor: keep rules under 500 lines and split large ones.
  • Claude Code: aim for under 200 lines per CLAUDE.md; move part-specific guidance into path-scoped rules.
  • Antigravity: each rule file is truncated past 24,000 bytes.
  • Codex: all AGENTS.md files together stop at 32 KiB by default.

Our official standards files are 2.5 KB to 9 KB each. Scope them with globs so they only load where they apply. The next lesson covers exactly how.

Common mistakes

  • Writing rules before you've seen the failure. Start with a few lines. Add one each time the agent repeats a mistake.
  • Contradictions across files. If two rules disagree, the agent may pick either. Search your rule files before adding a new one.
  • Using "always" and "never" for preferences. Save absolute words for real boundaries, so they keep their weight.
  • No version. Without "Updated for", the agent can't tell a Next.js 14 rule from a Next.js 16 one.

Recap

  • Keep a line only if the agent would act differently without it.
  • Include facts, decisions, boundaries and known traps.
  • Use the outline: version, shape, topics, tests, mistakes, deprecated APIs, commands, example.
  • One good example beats a long list. Leave style to the linter.

Next step

Make that rule load only where it matters: Scoping and context budget.