CursorCode generationLevel 2: Context

How Do You Get Cursor to Generate Code That Fits Your Repo?

Name the file to change, @-mention one good existing example, list the exact fields or behavior, and say which hook or helper to reuse. Agent can search your repo, but it follows your patterns far more reliably when you tell it which one to copy.

Prompt to copy
Add [the new thing] in [target file or folder].

Follow the pattern in @[reference file]: same structure, naming and error handling. Use the existing components in @[components file] rather than creating new ones.

It should include:
- [field or behavior 1]
- [field or behavior 2]
- [field or behavior 3]

Store data using [existing hook or service]. Follow the same API route pattern as @[route file].

Only add what's needed, and don't restyle surrounding code. When you're done, run [test or type check command].
Vague"Add a user settings page"
  • Agent guesses layout
  • Picks its own components
  • Invents a style
Constrainednames files and fields
  • Copies your layout
  • Reuses your components
  • Follows your API route
Cursor's own course contrasts these two prompts for the same settings page.

Try it now

  1. 1Pick something your repo has at least one good example of, and press Cmd+I (Ctrl+I) to open Agent.
  2. 2Type @, start typing the example's filename, and pick it from the suggestions. Fill in the prompt above.
  3. 3When the edits appear, compare the new file with the reference in the diff view. It worked if a reader couldn't tell which of the two was written first.

Use it when

Adding another instance of something the repo already does: a new route, a form, a test file, another page in a section. It works best when one existing file is clearly the right model.

Skip it when

Code with no precedent in the repo. There's nothing to copy, and a mediocre reference can do harm, so use Plan mode to work out the shape first. Also skip the detail for trivial additions, where the agent will find the pattern alone.

Why it works

Cursor's course puts it plainly: with a vague prompt the agent "had to guess at everything", and where you give no intent it uses its best judgment. A constrained prompt grounds the work in existing files, components and clear scope, so it follows your patterns instead of inventing new ones. Mentioning the example also saves it from searching for a pattern and settling on an outdated one.

Example

Cursor's course gives this case. Rather than "Add a user settings page", you point at src/app/profile/page.tsx for the layout and src/components/ui/Form.tsx for the form parts. You list display name as a text input, email notifications as a toggle, and theme as a dropdown of light, dark or system. You also say to store settings with the existing useUserPreferences hook.

Going further

If you keep pasting the same reference into prompts, move it to a rule. Cursor's docs recommend rules that point to canonical examples rather than copying them, such as "See components/Button.tsx for canonical component structure". A referenced file isn't inlined. Agent reads it with its tools when it needs it. To compare results on code that matters, you can select several models from the agent dropdown and run the same prompt on each.

Common mistakes

  • Pointing at a bad example, so Agent faithfully copies the thing you meant to fix.
  • Mentioning the whole components/ folder when one file would give a clearer signal.
  • Asking for a page with no fields listed, then spending longer correcting it than you'd have spent specifying it.
  • Letting it add a new utility without checking whether one exists. Ask it first how the repo already handles this.

Sources

Checked 6 November 2026. Products change often, so confirm details with the vendor.

Related

Beyond the course