Introduction
Keep AI-generated code within controllable boundaries
In previous chapters, we completed tool setup, tech stack selection, and scaffolding. Now that the project skeleton is in place, itโs time to make AI understand and follow your project standards.
Core Concept: Rules as Constraints
Cursor Rules are the โcontractโ between you and AI. Through clear rule definitions, you can:
| Goal | Without Rules | With Rules |
|---|---|---|
| Code Style | AI randomly chooses styles | Consistently follows project standards |
| Architecture | Code may be misplaced | Automatically placed in correct directories |
| Tech Choices | May use wrong libraries | Uses specified tech stack |
| Best Practices | Different implementations each time | Consistent implementation patterns |
Chapter Goals
Through this chapter, you will master:
- Writing principles and best practices for Cursor Rules
- What rules to write at different stages of project development
- Using meta prompts to quickly generate rule frameworks
Layered Rule Design
A complete rule system typically includes the following layers:
.cursor/rules/
โโโ global-rules.mdc # ๐ Global Rules (always active)
โ # Tech stack, directory structure, TypeScript standards
โ
โโโ routing-rules.mdc # ๐ฃ๏ธ Routing Rules (active for pages/**)
โ # Page structure, route protection, auth flow
โ
โโโ api-rules.mdc # ๐ API Rules (active for clients/**)
โ # HTTP client, data fetching, error handling
โ
โโโ ui-rules.mdc # ๐จ UI Rules (active for *.tsx)
# Component usage, icon system, theme configChapter Contents
โ๏ธ How Rules Workโ๏ธ Rules Writing Best Practices๐ Rules Development Sequence๐ค Meta Prompts for Generating Rules
Reference Rule Sets
Weโve collected some verified rule examples that can serve as references for writing your rules:
- Vite + Shadcn/UI Rule Set - Complete frontend project rule examples
Next Steps
Ready? Letโs start with How Rules Work to understand how rules influence AI behavior.
Last updated on: