Generate Cursor rules from your codebase with AI
What rulegen-ai does
Instead of writing rules from scratch, rulegen-ai scans your codebase, analyzes patterns, and uses Gemini to generate structured .mdc rules with frontmatter.
It looks at:
- File structure: Directory layout, naming conventions, imports
- Code patterns: Recurring function signatures, error handling, component structure
- Style choices: Const vs let, arrow functions vs regular functions, naming conventions
- Framework usage: React hooks, Express middleware, TypeScript patterns
The tool generates rules that match what you're already doing, making it easier for Cursor to follow your project's existing conventions.
How to run it
You need a Gemini API key. The free tier works fine for most projects.
export GEMINI_API_KEY=your_key_here
npx rulegen-ai
The command will:
- Scan your project files (respecting
.gitignore) - Send code samples to Gemini with a prompt asking for rules
- Parse the AI response into
.mdcfiles with frontmatter - Write the generated rules to
.cursor/rules/
Get a free API key: Visit Google AI Studio to generate a Gemini API key. The free tier includes enough quota for most codebases.
What it produces
The tool generates multiple .mdc files, each focused on a specific pattern it found in your code. Example output:
.cursor/rules/
├── typescript-naming.mdc
├── react-components.mdc
├── error-handling.mdc
└── api-routes.mdc
Each file includes:
- Frontmatter: Description and globs that match where the pattern appears
- Rule content: Specific instructions based on what the AI observed in your code
- Examples: Code snippets from your project showing the pattern in use
Customizing the output
The default model is gemini-2.5-flash. You can specify a different model if needed:
npx rulegen-ai --model gemini-2.5-pro
Other options:
- path (positional): Specify a directory to scan (default: current directory). Example:
npx rulegen-ai ./my-project - --format: Output format. Options:
cursor(default),claude-md,agents-md,copilot,windsurf - --max-files: Limit how many files to analyze (default: 50, useful for large codebases)
- --dry-run: Preview rules without writing files
- --verbose: Show which files are sent to Gemini
When to use it vs writing manually
Generated rules are a starting point, not a final product. They work best when:
✓ Good fit
- New to writing rules
- Large existing codebase with established patterns
- Want a baseline to refine manually
- Onboarding new team members
❌ Not ideal
- Very small projects (faster to write manually)
- Codebase has inconsistent patterns
- Need highly specific custom rules
- Working with proprietary or sensitive code
Review and refine
AI-generated rules need human review. After running rulegen-ai, check the output for:
- Vague instructions: The AI sometimes generates generic rules like "write clean code." Delete or make them specific.
- Incorrect patterns: If your codebase has bad practices, the AI might encode them as rules. Fix these manually.
- Over-scoped globs: Generated globs might be too broad or too narrow. Adjust to match your intent.
- Missing context: Add examples or clarifications where the AI's output is unclear.
Run npx cursor-doctor scan after editing to validate frontmatter and catch quality issues.
Privacy and API usage
The tool sends code snippets to Google's Gemini API. If you're working with proprietary or sensitive code, review your company's policies before using this tool. You can limit what gets scanned with .gitignore or the --path flag to exclude sensitive directories.
Related guides
- How to write Cursor rules that actually work
- Convert rules between Cursor, Windsurf, and Claude Code
- Cursor token budget: how many rules is too many?
Generate rules from your code
Scan your project with AI to create a baseline set of Cursor rules. Requires a free Gemini API key.
npx rulegen-ai