AI codingSetup guide4 min readUpdated September 2026

Cursor Rules Files: Examples for Next.js, Python and SQL Repos

A Cursor rules file is a short, plain-language set of instructions the editor's AI reads before it works in your repository. Good ones state your stack, your conventions and your hard limits in specific, checkable sentences. Long, vague ones get ignored.

Below you'll find sample rules for three common stacks, a method for writing your own, and a way to test whether a rule changes behavior. Cursor has changed where rules live and how they're scoped over time, so confirm the current file location and format in its documentation before you copy anything.

Vendors Covered in this Article

Disclosure: We may earn a commission if you buy through some links on this page. It doesn't change what we recommend.

What belongs in a rules file, and what doesn't?

Rules work best when each one names a behavior a reviewer could check in a pull request. Put these in:

  • Your stack and versions, so the assistant doesn't suggest a deprecated API or the wrong framework style.
  • Naming, file layout and where new code goes.
  • Commands: how to run tests, the linter and the type checker.
  • Hard prohibitions, such as never editing generated files or committing secrets.
  • Patterns you want repeated, with a pointer to a real example file in the repo.

Leave out general advice like "write clean code" and "follow best practices". The model already tries to, and the sentence gives it nothing to act on. Also leave out anything long. Every rule competes for attention with your actual prompt, so a rules file of a few dozen lines usually beats one of several hundred.

Example rules for a TypeScript and Next.js app

Adapt these to your repo. Each line is a rule you can check:

  • Use the App Router. New pages go in `app/`, and shared components in `components/`. Don't create files under `pages/`.
  • Components are server components by default. Add `"use client"` only when a component needs state, effects or browser APIs.
  • Use TypeScript strict mode and never use `any`. If a type is unknown, use `unknown` and narrow it.
  • Validate all request bodies with a schema before using them. Reuse the schemas in `lib/schemas/`.
  • Fetch data in server components or route handlers, never with a client-side call to an internal API for data the page could load itself.
  • Read configuration through `lib/env.ts`, never directly from `process.env` in components.
  • Run the type check and the test suite before finishing a task, and report the result.

Notice the pointers to real paths. A rule that names `lib/schemas/` gives the assistant something to open and copy from.

Example rules for a Python service

For a small API service, rules often cover typing, error handling and testing:

  • Target the Python version pinned in the project file. Use type hints on all function signatures.
  • Keep request and response models in `schemas/` and business logic in `services/`. Route handlers stay thin.
  • Raise domain exceptions from services and translate them to HTTP errors in one place.
  • Never build SQL by string concatenation. Use parameterized queries or the existing data access layer.
  • Every new endpoint needs a test in `tests/` covering one success case and one validation failure.
  • Don't add a new dependency without saying why, and prefer what's already in the lockfile.

The last rule matters more than it looks. Assistants readily add packages, and each one is a supply chain decision. The review checklist for AI-written code covers how to verify them.

Example rules for SQL migrations and row-level security

Database code is where a wrong suggestion is most expensive, so rules here should be strict:

  • Every schema change is a new migration file. Never edit an applied migration.
  • Every new table enables row-level security in the same migration and ships with at least one policy.
  • Policies check the authenticated user or tenant, never `true`.
  • Additive changes first: add a column as nullable, backfill, then add the constraint in a later migration.
  • Never drop a column or table in the same release that stops using it.

Pair these with a look at real policy patterns in the row-level security examples, and keep a policy test in your suite so a missing policy fails the build rather than a review.

How do you know a rule works?

Test rules the way you'd test code. Pick a rule, ask the assistant for a small task that would normally break it, and see what happens with the rule on and off. If behavior doesn't change, the rule is too vague, too long or in the wrong scope, and you should rewrite it more concretely or move it closer to the files it governs.

Review the file every couple of months. Delete rules for problems the model no longer has, and add a rule whenever the same review comment appears three times. Keep the file in version control so changes get reviewed, and tie it to your team's assistant policy, since rules can encode part of it, such as which directories are off limits.

Rules are guidance, not enforcement. Anything that must never happen, such as committing a secret, still needs a check in CI.

Executive Capability Standard

What Good Looks Like

Each repository has a short, versioned rules file of checkable instructions, tested against real tasks and backed by CI checks for anything critical.

Building The Capability (5-Stage Skill Ladder)

1. Learn:Read the current Cursor documentation on rule files, scoping and file locations.
2. Do Manually:Write ten specific rules from your last month of review comments and test each on a small task.
3. Delegate:Give one senior engineer ownership of the shared rules template and the review of changes to it.
4. Automate:Add CI checks such as lint rules, type checks and migration tests for the constraints the rules describe.
5. Buy:Move to a team plan with organization-level controls once you need shared rules and settings across many repos.

How to Get Started

Disclosure: We may earn a commission if you buy through some links on this page. It doesn't change what we recommend.

Cursor

Fits as the editor that reads these files, provided you confirm the current rules format and your plan's data settings.

Visit Cursor→

Frequently Asked Questions

Where does a Cursor rules file go?

In the repository, so it's versioned and shared with the team. The exact location and format have changed across Cursor versions, so check the current documentation. Whatever the location, commit it and review changes like any other config file.

How long should a rules file be?

Short: a few dozen specific lines is usually better than several hundred. Every rule uses part of the model's attention. Keep the ones that prevent real mistakes and delete the rest.

Do rules guarantee the assistant follows them?

No. Rules raise the odds but don't enforce anything. Treat them as guidance and back important constraints with tests, linters and CI checks that fail the build.

Should each project have its own rules?

Yes for stack and layout rules, since those differ per repo. Keep organization-wide rules, such as security prohibitions, in a shared template that each repository copies and adapts.

About the numbers

This guide doesn't quote a sourced benchmark. Figures in it are estimates or general guidance, so check them against your own numbers.

Related Guides