How to Write Better Prompts for AI Coding Agents

How to Write Better Prompts for AI Coding Agents

AI coding agents — Claude Code, OpenAI Codex, and Cursor — can read a repository, edit files, run tests, and fix their own mistakes. Each one now works as a command line (CLI), in an editor, in the cloud, and with MCP tools. A Pragmatic Engineer survey of 906 engineers in early 2026 found that 70% use two to four AI tools at the same time.

Anthropic, OpenAI, and Cursor agree: be clear, give facts the agent cannot guess, set limits, and give it a way to check its work.

Table of Contents (9)
  1. 1. Delegate an outcome, not a vague wish
  2. 2. Give the agent what it cannot see
  3. 3. Define "done" as something that can be checked
  4. 4. Break big work down — and plan before you build
  5. 5. Show an example, don't just describe the style
  6. 6. Set constraints in positive, specific language
  7. 7. Iterate like a reviewer, not a bystander
  8. FAQ
  9. Conclusion

1. Delegate an outcome, not a vague wish

Say what should change, and for whom. Anthropic’s Claude Code docs put it simply: name the file, describe the problem, and say what “fixed” looks like.

Weak: Fix the login bug.

Better: Login fails after the session times out.
Look at token refresh in src/auth/, write a
failing test that shows the bug, then fix it.

OpenAI says the same for Codex. Use this short template for any task bigger than a one-line fix:

Goal: [what should be true when this is done]
Context: [files, patterns, or error messages]
Constraints: [what not to change, which libraries to use]
Done when: [a test or result that proves it]

2. Give the agent what it cannot see

An agent is very good at reading your code. It can usually find the file layout and test setup faster than you can explain them. Anthropic’s guide is clear: only write down what the agent cannot find by reading the code — for example, a choice made in a meeting, or a deploy step that lives in one person’s head.

If you already have what the agent needs, give it the thing itself. In Claude Code and Cursor, type @ and the file path, or paste the error.

Weak: Add a calendar widget like the ones on the home page.

Better: Follow the pattern in HotDogWidget.php and
build a calendar widget that lets you pick a month
and page between years. No new libraries.

That example comes from Anthropic’s own docs. Attach only the files that matter.

3. Define “done” as something that can be checked

Anthropic says it plainly: give the agent something it can run that comes back pass or fail. Then it can keep working on its own. Without a check, the agent stops when the work looks done — and you become the test suite.

A check can be a test, a build, or a linter:

Write a validateEmail function. Test cases:
user@example.com is true, invalid is false,
user@.com is false. Run the tests after implementing.

That example is adapted from Anthropic’s docs. If a test fails, the agent reads the output, fixes the code, and runs the check again.

4. Break big work down — and plan before you build

OpenAI lists splitting big tasks into small ones as one of its six main strategies. Do not ask for a whole feature in one prompt.

Weak: Build user authentication.

Better:
1. Create an AuthRepository class for login via
   POST /api/login, following the pattern in
   user_repository.dart.
2. Add unit tests for success and failure.
3. Wire it into the login screen, without changing
   the screen's public API.

If you are not sure how to do the work, plan first. Claude Code has Plan Mode, Cursor has a similar mode, and Codex has /plan. Ask for a plan, and approve it first.

5. Show an example, don’t just describe the style

When the result has a set shape — a function, a format, a way to handle errors — show it. OpenAI calls this few-shot prompting: give a few examples, and the model learns the pattern.

Weak: Make the API client handle errors properly.

Better: Follow the error handling in
src/lib/api-client.ts: return a Result<T> type
instead of throwing, and log failures with the
request ID. Use the same pattern in the new
billing client in src/lib/billing.ts.

Pointing at a real file beats a long note about your style. If you want only a diff, say so.

6. Set constraints in positive, specific language

Limits lead to better code than open requests. Tell the agent what not to touch, which libraries to use, and how far the change should go.

Weak: Improve the checkout flow.

Better: Only change handleSubmit in
src/checkout/form.tsx. Use the validation
helpers in src/lib/validation.ts. Do not change the
function signature, and do not add new libraries.

Write rules in a positive form: models follow “do this” better than “don’t do that”. Give the reason for a rule, not just the rule.

A prompt alone cannot enforce a rule with no exceptions. Anthropic splits rules the model weighs (your prompt, CLAUDE.md) from rules the tool enforces (hooks, deny rules). If a rule must never be broken — never read .env — use the tool’s enforcement settings.

7. Iterate like a reviewer, not a bystander

Your first prompt will often be wrong in a small way. Start simple, read the diff, and fix it by pointing at the exact case that failed.

Weak: That doesn't work. Try again.

Better: The fix works for a logged-in user, but it
still fails when the refresh token is expired and
the access token is still valid — see the failing
case in tests/auth/refresh.test.ts, line 42.
Handle that case the same way, and rerun the suite.

Check the diff before you accept it. Ask a fresh agent to check it against your spec and report gaps that affect correctness.

Finally, save what works. If you fix the same mistake twice, put that fix in a lasting file, not in your next prompt: CLAUDE.md in Claude Code, or AGENTS.md in Cursor and Codex.

FAQ

How long should a prompt for a coding agent be?

Long enough to answer four questions: what should change, which files matter, what the limits are, and what counts as done.

Should I paste my whole codebase into the prompt?

No. Attach only the files that matter, using @ links where the tool supports them.

What do I do when the agent keeps making the same mistake?

Fix it once, with the exact failing case. If it happens again, move the fix into a lasting rule file. If the agent still misses it, put the rule in an enforced setting — a hook or a deny rule.

Is Plan Mode worth the extra step?

Yes, for work that touches three or more files, or where the approach is unclear. A short plan stops the worst failure: a big change that solves the wrong problem.

Conclusion

Better prompts are not about clever words. They are about good delegation: a clear result, the facts only you can give, limits to respect, an example to copy, and a check that proves the work is done. Use the template — goal, context, constraints, done-when — for your next task.

Sources:
– https://code.claude.com/docs/en/best-practices.md
– https://developers.openai.com/api/docs/guides/prompt-engineering?api-mode=responses
– https://developers.openai.com/api/docs/guides/prompting
– https://cursor.com/docs/rules#project-rules
– https://newsletter.pragmaticengineer.com/p/ai-tooling-2026
– https://parallel.ai/articles/codex-vs-claude-code
– https://www.builder.io/m/explainers/best-cursor-ai-prompts-for-coding
– https://learn.kirankrishna.ai/learn/codex-best-practices

Leave a Comment