medium.com 5 min read Original Source

Cursor Rules: Best Practices for Developers

Why Cursor keep “forgetting” and how to fix it?

TL;DR: Context window is the new game we play.

If you use Cursor long enough, you see it:
The agent agrees with everything you say, writes code that ignores your standards, or “hallucinates” features that don’t exist.

But there are ways to make it more reliable and better aligned with how you work:

For a start, I created .cursor/rules file — a way to re-train Cursor on my rules every time we talk, so it behaves like a real teammate agent, instead of a “yes-man”.

And I didn’t just create it without giving it much thought. It took weeks of human “Reinforcement learning”; constant learning, tweaks, and attempts until I shaped a set of rules that turned Cursor into a reliable teammate

The outcome was a .cursor/rules/core-development.mdc rules file, with strict rules that serve our team’s needs much better and more precisely than Cursor’s “out of the box” agent.

I had to guide it with subjects such as:

  • Don’t auto-agree with everything I say. Don’t try to please me.
  • Always check the code first before acting (to reduce hallucinations)
  • Aim for the smallest possible code change.

I wrote these rules in a “martial arts” tone — short, direct, combat style, instructive, no fluff, because the .cursor/rules file I’ve created runs in Always Apply mode. That means that It’s loaded into every new conversation, and in the tokens economy every word counts.

How did I write the file in practice?
I wrote the rule myself, but then I asked Cursor agent to rephrase it to stand in the tone which is where he excels at.

Here are a few examples of what I asked, and what he replied:

I wrote:
“How to say keep SOLID approach, separation of concerns, but also don’t over-complicate and over-engineer? in a clear martial arts army style instructions. cut and clear, bluff, optimal to understand and perform“

The agent replied and suggested a few alternatives, one of them was: “Keep code SOLID but simple — separation of concerns without over-engineering”

Another example, I asked: “How to say readability and maintainability in one short word?” and it replied “CLARITY”

Lastly, I asked him how many tokens the cursor file consumes, and afterwards asked it to cut it to around half, but without losing the quality and clarity. I didn’t take all his suggestions, but he indeed improved and compressed lines with words that weren’t really necessary.

Great, right? Well… not exactly, because there’s another catch:

As the context window moves, he forgets.
No matter how well you write the rules, after a few messages, they’re being ignored.
My educated guess is that it’s due to a Cursor built-in context window optimization mechanism, where recency wins over.

So, you have to maintain a Periodic Rule Reinforcement — and after having some conversation, explicitly add to your last message comments such as “remember the rules” or “read the rules again”, or the next message will continue without them.

A nice trick I’ve done is requesting him to mention what rules he applies as part of his response. This makes it easy to see when he’s following them — and when it’s time to remind him:

And it’s striking how much better the response gets when you suddenly remind the agent about your rules — and ask it to read the rules (again), or explicitly instruct with commands such as to understand the code, learn existing patterns, and reduce assumptions. Hallucinations drop hard!

There are also things the agent can’t figure out alone because he lacks context. Remember? We’re in the context game.

Asking the agent to assist you in your code is just like asking some random developer from one of the other teams to help you out. He will try his best to please you, but he’s not familiar with your preferences, such as how you run the code, how you test it, and what your project standards are.

You can divide the agent structure into three layers:

  1. Model layer – the LLM you use (ie. Claude-4-Sonnet)
  2. Cursor layer – the prompts, tools, and default behavior added by Cursor
  3. Custom rules layer – your .cursor/rules/*.mdc files that shape the agent’s behavior to match your needs

So, in order to squeeze the best out of our agent, we need to instruct him with as much context as possible. That’s a layer we are in charge of and need to pay attention to, if we want to improve the Cursor experience.

And this layer can be instructed in two directions:

a. What not to do, such as:

  • Don’t write comments if our standard is to avoid them
  • Don’t run npm run if hot reload is active
  • Don’t write docs unless instructed to (he loves doing it, maybe Cursor added some internal system prompt for their agents)

b. How to do things the right way (your way), like:

  • How to get GitHub branch details and PR comments (after he failed, I walked him through the process, then made it a rule)
  • How to use curl command for specific checks when there’s no MCP available to do it
  • How do we write and run tests in our codebase

Therefore, creating a cursor rules file that reflects your preferences improves result quality and the overall developer experience.
When this file is part of your shared project repository, the whole team benefits — fewer hallucinations, better consistency, and faster velocity.

If you have read until here, I bet you’re curious what our cursor file looks like, so here it is:

Here’s the full core-development.mdc cursor rules file:

---
alwaysApply: true
---

# STRICT RULES

## CRITICAL PARTNER MINDSET

Do not affirm my statements or assume my conclusions are correct. Question assumptions, offer counterpoints, test reasoning. Prioritize truth over agreement.

## EXECUTION SEQUENCE (always reply with "Applying rules X,Y,Z")

1. SEARCH FIRST - Use codebase_search/grep/web_search/MCP tools until finding similar functionality or confirming none exists. Investigate deeply, be 100% sure before implementing.
2. REUSE FIRST - Check existing functions/patterns/structure. Extend before creating new. Strive to smallest possible code changes
3. NO ASSUMPTIONS - Only use: files read, user messages, tool results. Missing info? Search then ask user.
4. CHALLENGE IDEAS - If you see flaws/risks/better approaches, say so directly
5. BE HONEST - State what's needed/problematic, don't sugarcoat to please
6. PERIODICALLY Self-check rule compliance during long conversations
7. LOG CHECK - Check logs for errors (frontend, backend) after implementing changes, or debugging/fixing issues
8. RULE REFRESH - Re-check this rules file every few messages to stay compliant

## CODING STANDARDS

- Plan before coding, explain reasoning for complex suggestions
- No code comments - write self-explanatory code
- Keep imports alphabetically sorted
- Keep code SOLID but simple - separation of concerns without over-engineering
- Aim to keep files under 300 lines - split when it improves clarity
- Write tests for critical paths only. Use AAA pattern with comments. Learn from examples
- Test your tests and run npm run lint:ci for lint check

## Github Rules

- PR content: use gh pr view for basic info
- PR metadata: use gh api repos/OWNER/REPO/pulls/PR_NUMBER for JSON
- PR files: use gh api repos/OWNER/REPO/pulls/PR_NUMBER/files
- PR comments: check 3 types - issues/comments, pulls/comments, pulls/reviews
- Line comments most important: gh api repos/OWNER/REPO/pulls/PR_NUMBER/comments
- PR description: Use .github/pull_request_template.md - extract XXXX from branch name

## PROHIBITED ACTIONS

- DO NOT WRITE DOCS UNLESS EXPLICITLY ASKED TO
- NEVER run npm/yarn start commands - assume dev servers always running
---
alwaysApply: true
---

# STRICT RULES

## CRITICAL PARTNER MINDSET

Do not affirm my statements or assume my conclusions are correct. Question assumptions, offer counterpoints, test reasoning. Prioritize truth over agreement.

## EXECUTION SEQUENCE (always reply with "Applying rules X,Y,Z")

1. SEARCH FIRST - Use codebase_search/grep/web_search/MCP tools until finding similar functionality or confirming none exists. Investigate deeply, be 100% sure before implementing.
2. REUSE FIRST - Check existing functions/patterns/structure. Extend before creating new. Strive to smallest possible code changes
3. NO ASSUMPTIONS - Only use: files read, user messages, tool results. Missing info? Search then ask user.
4. CHALLENGE IDEAS - If you see flaws/risks/better approaches, say so directly
5. BE HONEST - State what's needed/problematic, don't sugarcoat to please
6. PERIODICALLY Self-check rule compliance during long conversations
7. LOG CHECK - Check logs for errors (frontend, backend) after implementing changes, or debugging/fixing issues
8. RULE REFRESH - Re-check this rules file every few messages to stay compliant

## CODING STANDARDS

- Plan before coding, explain reasoning for complex suggestions
- No code comments - write self-explanatory code
- Keep imports alphabetically sorted
- Keep code SOLID but simple - separation of concerns without over-engineering
- Aim to keep files under 300 lines - split when it improves clarity
- Write tests for critical paths only. Use AAA pattern with comments. Learn from examples
- Test your tests and run npm run lint:ci for lint check

## Github Rules

- PR content: use gh pr view for basic info
- PR metadata: use gh api repos/OWNER/REPO/pulls/PR_NUMBER for JSON
- PR files: use gh api repos/OWNER/REPO/pulls/PR_NUMBER/files
- PR comments: check 3 types - issues/comments, pulls/comments, pulls/reviews
- Line comments most important: gh api repos/OWNER/REPO/pulls/PR_NUMBER/comments
- PR description: Use .github/pull_request_template.md - extract XXXX from branch name

## PROHIBITED ACTIONS

- DO NOT WRITE DOCS UNLESS EXPLICITLY ASKED TO
- NEVER run npm/yarn start commands - assume dev servers always running