← All guides Generate

Generate Cursor rules from your codebase with AI

Scan your code with Gemini to generate .mdc rules automatically.

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:

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:

  1. Scan your project files (respecting .gitignore)
  2. Send code samples to Gemini with a prompt asking for rules
  3. Parse the AI response into .mdc files with frontmatter
  4. 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:

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:

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:

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

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