Skip to content

Lesson 13 ยท Rules and Configuration

Project Rules and AGENTS.md

Write effective Cursor rules: .mdc files in .cursor/rules, the four rule types, AGENTS.md, user and team rules, precedence and mistakes to avoid.

You should not have to repeat "run the tests with unittest" or "never edit generated files" in every request. Rules are written instructions that Cursor adds to the agent's context.

Four places for instructions

Kind Where it lives Who it affects
Project rules .cursor/rules/ in the repository Work on that project
AGENTS.md A Markdown file in the project root or subfolders The project or a folder
User rules Your customization settings You, across projects
Team rules The team dashboard (Team and Enterprise plans) The whole team

When guidance conflicts, the documentation says rules merge in the order team, project, user, and earlier sources take precedence.

Project rule files

In the desktop app, project rules use the .mdc extension. The documentation says a plain .md file inside .cursor/rules is ignored there. Subfolders are allowed. Each file starts with a short front matter block:

TEXT
---
description: "How we write database migrations"
globs: db/migrations/**/*.py
alwaysApply: false
---

- Every migration must be reversible.
- Never edit a migration that has already been merged.

You choose a rule type, which controls these fields:

Type When it is included
Always Apply In every chat session; globs and description are ignored
Apply Intelligently When the agent judges it relevant, based on description
Apply to Specific Files When a matching file is in context, based on globs
Apply Manually When you mention it, such as @migration-rules

You can create a rule with /create-rule in the chat, or from the customization screen. A rule can refer to a file with an @ mention. The agent then reads that file with its tools; it is not pasted into the prompt. So anything that must always be present belongs in the rule text itself.

Rules do not affect Tab.

See how rule selection works

This stand-in imitates the four types. It is a teaching model with crude matching. In the product, "apply intelligently" is a judgment by the agent, not a keyword test.

Python Stand-in: which rules get included?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

AGENTS.md

AGENTS.md is a plain Markdown file with no metadata, a simple alternative to rule files. Put it in the project root. You can add nested ones in subfolders; they combine with parent files, and more specific instructions take precedence.

TEXT
# Project guide

## Commands
- Tests: python -m unittest discover
- Lint: ruff check .

## Conventions
- Money is stored as integer cents.
- Public functions need type hints and a docstring.

## Do not
- Edit files in generated/.
- Add dependencies without asking.

The documentation says both the desktop app and the terminal companion read AGENTS.md, so it is a good choice for shared basics. The two treat rule files differently, so check the rules page if you use both.

Write rules that work

  • Be concrete. "Money is integer cents" helps. "Write good code" does not.
  • Show the command. The agent needs to know how to test.
  • Point, do not copy. Refer to a good example file instead of pasting it, so the rule does not go stale.
  • Keep them short. The documentation suggests staying under 500 lines and splitting large rules.
  • Do not repeat the linter. Let tools enforce formatting.
  • Learn from mistakes. When the agent repeats an error, add one line to prevent it.

A rule is guidance, not a lock. A model may ignore or misread it. Anything that must be true needs a real control: a test, a linter, a permission, a branch rule or a carefully tested hook.

Check a rule file

This real script looks for mistakes that could leave a rule unused.

Python A small lint for rule files

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

"Manual only" is fine if that is what you intend. The check makes sure you notice.

Treat rules as code

Rules change what agents do. Review them in pull requests. Be careful with rules from the internet or from an unfamiliar repository: they are instructions to your agent. Never put secrets in a rule file.

Check that a rule helps

A rule is only useful if behavior changes. After adding one, run a small test in a fresh chat: ask for a task that the rule should affect, and see whether the result follows it. If not, check these points:

  1. Is the file in the right folder, with the right extension?
  2. Does the rule type match how you expect it to be included?
  3. Do the globs match the real file paths?
  4. Is the description clear enough for the agent to judge relevance?
  5. Does another rule say the opposite?

The context breakdown described in context mentions shows how much space rules take. Many always-applied rules use space in every chat, whether or not they matter to the task.

Review rules every few months. Remove those about code that no longer exists. An outdated rule is worse than no rule, because it pushes the agent confidently in the wrong direction. Give each rule file an owner, just as you would for any other important configuration.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which extension must a project rule in .cursor/rules use in the desktop app?

    The documentation says a plain .md file there is ignored by the desktop app.
  2. 2. Which source takes precedence when rules conflict?

    The documented merge order is team, project, user, with earlier sources winning.
  3. 3. When is an Apply to Specific Files rule included?

    Its globs decide when it is attached.
  4. 4. How should you handle something that must never be violated?

    Rules guide a model but cannot guarantee behavior.

Summary

  • Project rules are .mdc files in .cursor/rules; AGENTS.md is a plain alternative.
  • Four types: always, intelligent, specific files and manual.
  • Team rules outrank project rules, which outrank user rules.
  • Keep rules concrete and short, and enforce critical things with real checks.

Next: Skills and Subagents

Your progress is saved in this browser. No account needed.