How to prompt AI coding agents: examples that work
The four parts of a prompt a coding agent can act on, with before-and-after examples for a bug fix and a feature.

Key takeaways
- A good agent prompt states the goal, gives context, sets constraints and says how the agent can check its work.
- Give exact file paths and error messages instead of describing the problem in general terms.
- Ask for a plan before any change that touches more than a few files.
- Move rules you repeat into CLAUDE.md or AGENTS.md, and start a fresh session when a long one drifts.
The best prompts for AI coding agents read like a good ticket: what should change, where, what must stay the same, and how to tell it worked. Give the agent the exact error, the file names and a check it can run, and ask for a plan before any large change. Clever wording matters much less than being specific.
This guide shows the four parts of a prompt an agent can act on, before-and-after examples for a bug fix and a new feature, and the habits that keep a long session on track. It applies to Claude Code, Codex, GitHub Copilot, Cursor and most other agents.
Why prompting an agent is different
A chatbot answers once and waits. An agent reads files, runs commands and edits code until it decides it is finished. A vague prompt to a chatbot gets a vague answer you can ignore. A vague prompt to an agent gets twenty changed files you now have to review.
So the job of the prompt shifts. You are not trying to get a good paragraph; you are trying to set a scope the agent stays inside and a finish line it can check by itself.
The four parts of a good prompt
- Goal. The outcome, not the method. "Customers can cancel a booking up to 24 hours before" says more than "add a cancel button".
- Context. File paths, the exact error message, a screenshot, a link to the ticket, and what you already tried. Agents can search, but pointing them at the right file saves time and tokens.
- Constraints. What must not change: public APIs, the database schema, the design system, files outside one folder, "no new dependencies".
- Done when. A check the agent can run itself: a test, a command, a page at a given screen width. Anthropic's best practices for Claude Code single this out: an agent that can verify its own work fixes its own mistakes before you see them.
Before and after: a bug fix
Before:
The login is broken, please fix it.
The agent has to guess what "broken" means, which login, and when to stop. It may rewrite the auth module to be safe.
After:
Signing in with Google fails on the deployed site but works locally.
The browser console shows: "redirect_uri_mismatch".
The callback is handled in app/auth/callback/route.ts and the URL is built in lib/auth.ts.
Find the cause and fix it. Don't change the email sign-in flow.
Done when: pnpm test passes and you've explained which URL was wrong and why.
Same request, but now the agent knows the symptom, the exact error, where to look, what to leave alone and how to finish. Ask for the explanation, too: if the agent can't say why the bug happened, its fix is probably a guess.
Before and after: a new feature
Before:
Add dark mode.
After:
Add a dark theme to the marketing site in site/.
- Follow the visitor's system setting by default, with a toggle in the header that remembers the choice.
- Use the colour tokens in site/styles/tokens.css; add dark values there, don't hardcode colours in components.
- Text must keep at least 4.5:1 contrast.
First show me a plan: which files you'll change and how the toggle stores the choice. Wait for my OK before editing.
For anything that touches more than a few files, asking for a plan first is the cheapest review you will ever do. Correcting a plan takes a sentence; undoing a wrong implementation takes much longer. Most agents also have a plan or read-only mode that enforces this.
Phrases that help, and ones that don't
| Instead of | Try | Why |
|---|---|---|
| "Make it better" | "The table is hard to scan on a phone; stack each row as a card below 600 px" | Names the problem and the target |
| "Fix all the bugs" | "Fix the failing test in cart.test.ts, then stop" | One task with an end |
| Pasting 2,000 lines of log | The error, the 20 lines around it, and the command that produced it | Keeps the context small and on point |
| "Use best practices" | "Follow the patterns in src/api/orders.ts" | Points at a real example in your code |
| "Don't break anything" | "Run the full test suite before you say done" | A check instead of a wish |
Give feedback like a reviewer
The second prompt matters as much as the first. When the result is wrong, say exactly what is wrong and where: "the button overlaps the price at 390 px width" beats "the layout is off". Screenshots help a lot for visual problems. If the agent went down the wrong path, it is often faster to undo its changes and restate the task with the missing constraint than to patch on top. That is easy when you commit before each task; see how to undo AI code changes.
Watch the length of the session, too. A long conversation carries every old file read and dead end. When the agent starts repeating itself or forgetting earlier decisions, start a fresh session with a short summary of where things stand.
Put the rules you repeat in a file
If you type the same constraint in every prompt ("use pnpm", "never touch migrations"), move it to your project's instruction file. Claude Code reads CLAUDE.md, and most other agents read AGENTS.md, at the start of every session. Our guide to writing CLAUDE.md and AGENTS.md covers what belongs there. Your prompts can then focus on the task at hand.
Point at the exact spot in Ninja Builder
A lot of feedback on web projects is visual, and describing a spot on a page in words is slow. In Ninja Builder, the Preview pane shows the site next to the chat at desktop, tablet or phone size. Turn on Comments, click the element that is wrong, type what should change, and add as many pins as you need. Send comments to the agent turns them into one message that includes the page address, the screen size and the exact element for each note, so the agent knows precisely what you meant.


For larger changes, set the chat's Autonomy to Plan first so the builder proposes a plan before it edits anything. The files and Preview docs and keep the work on track docs have the details. You can try it free for 14 days, then for $4.99 a month: download Ninja Builder.
Frequently asked questions
What is the best prompt format for Claude Code or Codex?
There is no magic format. State the outcome you want, point at the relevant files and errors, list what must not change, and give a test or command that proves the work is done.
Should I ask a coding agent for a plan first?
Yes, for anything that touches more than a few files. Reviewing a plan takes a minute and catches wrong assumptions before the agent edits your code.
Why does my AI agent change files I didn't ask about?
Usually the prompt had no constraints. Say which folders or files are in scope and what must stay unchanged, and put permanent rules in your CLAUDE.md or AGENTS.md.
How long should a prompt for a coding agent be?
As long as it needs to be specific, and no longer. A few lines with the goal, the files, the constraints and the check usually beat both a one-liner and a page of background.
Keep reading

How to write CLAUDE.md and AGENTS.md files that agents follow
Write project instruction files that coding agents actually follow, with a copyable template and one setup for every tool.

How to undo AI code changes: checkpoints, rewind and Git
Three layers of undo for AI edits, from one file to a full Git restore, with the exact commands.

How to review AI-generated code: a practical routine
Six steps, in the same order every time, to catch what AI coding agents get wrong before it reaches your main branch.