Why Does Cursor's Agent Duplicate Code You Already Have?
Usually you asked for a change before anyone looked at what's already there. Cursor's course names this failure directly: the agent might create a new utility function when one already exists, or use a different pattern from the rest of the codebase. Have it explain how the code works now, and then ask for the change.
Don't change anything yet. Explain how this repo currently handles [form validation]: which patterns it uses, and where the shared [validators] live. Give me file paths, and tell me if there are competing approaches and which one is current. --- then --- Add [the change] using [the helper or pattern you just identified]. Don't create a new utility. If what exists doesn't fit, tell me why before writing anything.
- 1Ask how it works now
- 2Check the answer
- 3Ask for the change
- 4Review the diff
Try it now
- 1Press Shift+Tab until the mode shows Ask, and send the first half of the prompt for a change you're about to make.
- 2Check the paths it names, and note the helper you want it to reuse.
- 3Switch back to Agent, and send the second half. It worked if the diff calls the existing helper and adds no new utility.
Use it when
Before any change in a part of the repo you don't know, for anything shared like validation, auth, logging and date handling, and when you see a diff adding a new helper you weren't expecting.
Skip it when
Skip it for changes in code you know well or for simple edits with an obvious target. If you already know the helper's name, say it directly: "use formatDate from lib/date.ts". That's faster than asking for a tour.
Why it works
A coding agent takes requests literally. If you say "add validation to this form", it can succeed by writing new validation, and nothing in the request tells it to look for yours first. An exploring step changes that. It makes the agent read your existing validators, and it gives you the names you need for a better request. Cursor's course also says you'll get better results when you understand the code first and know specifically what to ask for.
Example
Say you need to validate a new signup field. If you just ask, you may get a new validateEmail in the signup folder. Asking first, you learn that shared validators live in lib/validators/ and that forms use them via a useForm hook. Now the request becomes: "Add a phone field, validated with a new rule in lib/validators/, following the email rule." The diff is smaller and fits in.
Going further
Repeat offenders deserve a pointer in a rule: "Date formatting uses lib/date.ts. Don't add new date helpers." A rule like this saves the exploring step in future. Keep the mode switch in mind too, because Cursor's help says each mode has its own context, so state the helper's name in your next message rather than assuming Agent remembers what Ask found. During review, ask directly: "Does this duplicate logic that exists elsewhere?" Cursor's course lists duplicated logic as something AI-generated code can get wrong even when it passes the tests.
Common mistakes
- Accepting a new helper because it's tested, without checking whether one already exists.
- Asking "how does this work?" and then changing mode without carrying over the answer.
- Reusing the first pattern found when the repo has an old one and a new one. Ask which is current.
- Treating this as a one-off rather than adding a rule that names the shared helper.
Sources
- Understanding your codebase, Cursor Learn
- Reviewing and testing code, Cursor Learn
- Agent mode, Cursor Help
- Ask mode, Cursor Help
- Customizing agents, Cursor Learn
Checked 6 November 2026. Products change often, so confirm details with the vendor.