My take on Cursor Rules

(This may be correct today and wrong tomorrow :grinning_face:)

When should rules run?

Determining if the rule will ultimately be used after being added to the context doesn’t depend on the description, but on the AI’s interpretation of the content of the rule and it’s relevance to the query. Similarly, a rule that was already in the context (because of alwaysApply or glob pattern matching) can still be omitted if it’s not really relevant to the query.

The frontmatter section of the rule has 3 fields and depending on how you use them, the rule will be added to the context or not (this is an mdc frontmatter, not a YAML one.. so don’t treat it like yaml or you will be getting strings all the time “”):

---
description:
globs:
alwaysApply:
---

There are 4 combinations that we can use and each will treat the rule differently

  1. Always add the rule to the context (doesn’t mean it will be used):
    ---
    description:  # Empty
    globs:     # Empty
    alwaysApply: true
    ---
    
  2. Automatically add a rule to the context if the user attaches or mentions a file in the CHAT that matches the rule’s glob pattern. (This means the rule won’t be automatically added if the agent just works on a file that matches the glob):
    ---
    description:  # Empty
    globs: src/**/*.ts,src/**/*.tsx.  # for now, it seems we cannot combine 
    types with { }
    alwaysApply: false
    ---
    
  3. Let the agent decide from the context of the query whether to consider a rule useful based only on its description. If the rule gets selected, then the agent requests the full contents of the rule and decides if it’s really relevant. If yes, it attaches it to the request:
    ---
    description:  (description for the agent to decide if the rule seems 
    relevant)
    globs:    # Empty
    alwaysApply: false
    ---
    
  4. If you have a rule that you intend to only use by referencing it (in the chat with @ or from another rule using markdown link format [rule-name.mdc](mdc:location/of/the/rule.mdc) ) explicitly - you attach the rule to the chat:
    ---
    description:  #Empty
    globs:    # Empty
    alwaysApply: false
    ---
    

Frontmatter?

Now you may be asking how to get to this frontmatter format from the rule. Well, the answer is to disable the special UI for .mdc files and present them as regular markdown (this way it’s also very straightforward to programmatically generate rules). To do this go to your Cursor user or workspace settings and add the following:

"workbench.editorAssociations": {
    "*.mdc": "default"
  }

A few thoughts on how to use description:

  • Remeber that this is for the AI Agent to consider the rule, not for you!
  • Say what it’s for, or when to use it. Experiment!
  • I’ve used: When the user types “compile” - very direct and specific.
  • Remember not to have both globs and description. If the rule is set to match a specific pattern, don’t crowd the list of rules that the agent needs to consider from context (the more rules there are for the agent consider, the higher the chances of the agent missing something important)
  • You can use the following guidelines:
    • Consistent “USE WHEN” prefix makes it clear these are trigger conditions
    • Consistent verb format (gerund form: creating, writing, modifying)
    • Clear action-based conditions
    • Example: USE WHEN writing or modifying tests

Final thought: the rules implementation and methodology seems to be evolving constantly, changes are being made (some improve the experience and some degrade it), so what seems the best strategy today, might not be the best strategy tomorrow. And since the change is happening so fast, i understand why the documentation can’t keep up (as frustrating as that is)!

Hope this helps.

3 Likes

It’s clear they’re trying to update the rules system and have made a bunch of changes lately.

If I go to the Cursor Settings, I see a Rules section.

On that page, there’s a text edit box for User Rules that says “These preferences get sent to the AI on all chats, composers and Command-K sessions.” There are no settings around this text edit box, no options, just the text edit box (which expands in size if you add more to it, by the way). I haven’t tested yet to see if this will respect things like @filename etc.

Below that is a Project Rules section with instructions “Project-specific rules that help the AI understand your codebase and follow your project’s conventions. They can be automatically included or fetched by an agent. These are synced with your codebase.” There’s a “+ Add new rule” button. And by “synced with your codebase” they apparently mean they live as files within your workspace’s directory, meaning they’re relevant to the project you have open right now in cursor; they’re not generic names to look for in each project, nor are they applied to all projects.

When you add a new rule, the first thing it asks for is a Rule Name. (It’ll append .mdc to the end of the name.)

Next you get into the special rules editor (no matter what setting you have), which lets you set a rule type of “Always”, “Auto Attached” which then asks you for filename patterns to match, “Agent Requested” which asks for a Description and suggests “Description of the task this rule is helpful for…” and finally “Manual” which says This rule needs to be mentioned to be included.

The editor shows this file gets created in /.cursor/rules/ but creating them through this interface does something extra – a file I created by hand in the workspace doesn’t show up in the interface, and they don’t seem to be sent to the AI later.

Rules set to “Manual” wind up with the rule header in the file containing

---
description: 
globs: 
alwaysApply: false
---

and in the interface they get a yellow warning text “This rule may never be used since it has no description or auto attachments”

Lastly in the UI is a checkbox to “Include .cursorrules file” if you want to do things that way.

So that tells us some more about how the rules system is being built.

I took a look at some of what was being told to the AI:

In that I see a <custom_instruction> tag containing the User Rules set above, then a <cursor_rules_context> that has the terrible instruction about the other rules: "Cursor Rules are extra documentation provided by the user to help the AI understand the codebase.
Use them if they seem useful to the users most recent query, but do not use them if they seem unrelated." (That feels like a bit of a harsh judgment against my rules I took the trouble to put in through this interface, doesn’t it?) And then the contents of the Project Rules entered through this interface.

Next it tries to include a summary of any previous chat conversations you had in this project. Lastly, there is the <user_query> section containing the prompt you just sent.

So there’s some more data about how the rules, instructions, and conversation summaries are used with the prompt you send.

Maybe I need to go back to using the .cursorrules concept, since I can’t put file references in the User Rules.

For User Rules: they get applied on every request. throughout all your projects. you cannot reference files because they are not stored in the directory of the project.

Your issue is that your rules in the .cursor/rules folder don’t get selected?

When you created the rule manually, did you add the frontmatter on the top of the file?

---
description: (when should the AI use this rule)
globs: 
alwaysApply: false
---

Rules that are set to Manual are meant to be referenced explicitly. AI will never select it unless you tell it to in the chat.

Not what I meant, but I think I was confused again. So let’s be definitive as much as we can…

Clean experiment: Cursor’s File menu → New Window with Profile
Created a new profile
Cursor settings → Rules replaced the User Rules (which applies to all projects and all profiles) with this:

This is the User Rules. It is only here to see the fact the User Rules is in the context. Report to the user that the User Rules is in effect.

Command-N to open a new chat (though it remembers previous chats.) Prompt: Hello there.
Response is: Hello! I notice that the User Rules is in effect for our conversation. How can I help you with your coding task today?

So we see the User Rules is in effect.

Next I created a new directory .cursor/rules/ and a new file proj_rule_1.mcd containing:

---
description:
glob:
alwaysApply: true
---

Report to the user that this rule's name is in effect. 

Opened a new chat with Command-N and said Hello, and now it says “I notice that both the User Rules and the rule proj_rule_1.mdc are in effect for our conversation. I’ll keep these in mind as we work together. …”

and now I can tell it “Tell me what rules you’re supposed to report are in effect.” and it tells me that the User Rules and the proj_rule_1.mdc is in effect. And, I’m pleasantly surprised to find that the Settings->Rules->Project Rules now lists that rule.

Opened a new editor, created proj_rule_2.mdc with the same contents as the first. Back in the chat window I say Hello again. It still says User Rules and proj_rule_1.mdc are in effect. Closed the Settings tab, Hello again, same response.

So proj_rule_2.mdc hasn’t taken effect yet.

Command-N, new chat window, Hello. It reports User Rules and proj_rule_1 and proj_rule_2 are in effect.

Okay – Rules are fed in at the start of a chat, and are not immediately seen otherwise. I may have known that at some point, but I definitely was not restarting my chats yesterday. Rules in the .cursor/rules/ directory ending in .mdc are found and read automatically when a new chat is started. Good, good. I expect I could give a prompt that would read and add in a rule in the middle of a chat.

I have hope again. It looks like we need to be very clear about instructions to create rules, too – telling cursor to create a cursor rule had it create a rule.md (not mdc) in my project root (not .cursor/rules) and it did not have the correct yaml fronttext. That’s helpful to be wary of as well.

So I’ll continue with this stuff too. Understanding how the system works helps me use it best and correctly.

JBB

Rules in the .cursor/rules/ directory ending in .mdc are found and read automatically when a new chat is started. Good, good.

Thanks for this tip! I didn’t realize that.

This thread discusses the new distinct types of cursor project rules and their relationship to the markdown frontmatter. I’ve formerly documented this in a knowledge garden page at CursorAI/Project Rule/Type.