Star
Agent Craft 2026-10-22 • 8 min read

Cursor Rules That Actually Work: A Field Guide With Examples

Every developer using Cursor starts by dumping a 3,000-word prompt into a root .cursorrules file, only to watch the AI completely ignore it 15 minutes later. Here is why most Cursor rules fail, and real production cursor rules examples that AI agents actually follow.

If you have spent any time with Cursor, you have probably experienced the frustration of writing an explicit rule—"Always use Form Requests for input validation"—only to watch the agent generate inline ->validate() calls on the very next turn.

The problem is almost never the intelligence of the underlying model (Claude 3.5 Sonnet, GPT-4o, etc.). The problem is rule architecture. When instructions are bloated, vaguely phrased, or dumped into a single global file, attention heads dilute the directives during generation.

Here is the field-tested guide to writing cursor rules examples that agents obey with high adherence.

The 4 Invariants of High-Adherence Cursor Rules

  1. Keep Rules Under 1,000 Tokens: Long, essay-like rules cause context dilution. High-adherence rules are concise, bulleted, and structured with clear headings.
  2. Use Negative Constraints ("NEVER"): Language models respond significantly better to negative constraints than polite suggestions. Instead of "Consider using policies", write "NEVER query or mutate database models without evaluating an authorization Policy."
  3. Use Scoped .cursor/rules/*.mdc Files: Do not put all rules in one giant file. Split them by domain (database, UI, security) using Cursor's modern frontmatter glob scoping.
  4. Provide One Terse Negative & Positive Example: Showing a 3-line bad pattern and a 3-line good pattern eliminates ambiguity instantly.

Example 1: Laravel Security & Authorization Rule

Save this as .cursor/rules/laravel-security.mdc:

--- description: Laravel Security & Authorization Invariants globs: app/Http/Controllers/**/*.php, app/Filament/**/*.php --- # Security Invariants for Controllers & Filament Resources ## Mandatory Rules - NEVER disable CSRF protection on web routes. - NEVER query or mutate resources by route ID without calling $this->authorize('action', ). - NEVER read raw inputs with $request->all(). Always typehint dedicated FormRequest classes. ## Anti-Pattern (DO NOT WRITE) `php // Insecure: BOLA vulnerability + mass assignment = Project::find(); ->update(->all()); ` ## Correct Pattern `php public function update(UpdateProjectRequest , Project ): RedirectResponse { ->authorize('update', ); ->update(->validated()); return redirect()->route('projects.show', ); } `

Example 2: Next.js 15 Server Components & Actions Rule

Save this as .cursor/rules/nextjs-architecture.mdc:

--- description: Next.js 15 App Router & Server Actions Guidelines globs: app/**/*.{ts,tsx} --- # Next.js 15 Guidelines ## Core Rules - Default to React Server Components (RSC). Only add 'use client' when state (useState), effects (useEffect), or browser event listeners are required. - Place all server mutations in dedicated files under pp/actions/ with 'use server' declared at file top. - Validate all Server Action input arguments with Zod schemas before querying the database. - Wrap all async Supabase database queries in try/catch blocks that return structured { data, error } objects.

Example 3: Python FastAPI & Pydantic Boundaries

Save this as .cursor/rules/fastapi-conventions.mdc:

--- description: FastAPI Schemas and Dependency Injection globs: app/**/*.py --- # FastAPI Clean Architecture Rules - Enforce strict typing on all function signatures using Pydantic v2 schemas. - NEVER access raw request bodies or dictionaries in endpoint handlers. - Inject database sessions using db: AsyncSession = Depends(get_db). - Return explicit HTTP status codes (status_code=status.HTTP_201_CREATED on resource creation).

The Multi-Agent Problem: What About Claude Code and Codex?

Writing separate rules for Cursor (.cursor/rules/*.mdc), Claude Code (CLAUDE.md), OpenAI Codex (CODEX.md), Copilot, and Cline leads to fragmentation and rule drift.

In TechTalks Stack Kits, we solve this with single-source rule compilation:

You write your rules once in rules/source/ as clean Markdown files. Our deterministic transpiler compiles them automatically into all 6 agent dialects:
  • .cursor/rules/*.mdc (with YAML frontmatter and globs)
  • CLAUDE.md (compact hierarchy for Claude Code)
  • CODEX.md (OpenAI Codex directives)
  • .github/copilot-instructions.md
  • .clinerules
  • .agents/rules/*.md (Antigravity IDE format)

Start With Production-Tested Rules

Don't spend days writing rules from scratch. Browse our 30 stack kits to get production-tested agent configurations, pre-configured security checkers, and zero-token-bloat rules today.

Written by Umar Farooq
Senior Full-Stack & Application Security Engineer