Skip to content

Effective prompting techniques for coding agents

Effective prompting techniques for coding agents are five patterns that turn a vague request into checkable work: the agent asks its questions before it codes, the prompt states the constraints and the command that proves the result, an existing file serves as the pattern, planning precedes implementation, and review covers the evidence instead of every line.

This page is for developers who drive Claude Code, Codex or Cursor every day, and for tech leads who want the whole team to prompt the same way. You ask for “user authentication with email verification”. The agent picks a session strategy you would never ship, wires in a mailer you do not use, writes tests that mock everything, and reports success. Every one of those choices was a gap in the prompt that the model filled with a default.

What these prompting techniques give you on a real task

Section titled “What these prompting techniques give you on a real task”
  • A clarification prompt that surfaces missing requirements before any code exists
  • A constraints-and-check template that tells the agent what “done” means as a command it can run
  • A plan-then-implement loop that stops architectural mistakes while they cost a sentence to fix
  • Recovery prompts for a stuck debugging loop, a self-review pass and a cleaner second draft
  • An evidence report you can check in two minutes instead of reading a 600-line diff
  • The per-tool levers (project instructions, file references, reasoning effort, context hygiene) that fix quality before you touch the model

Why does the same model produce good and bad code?

Section titled “Why does the same model produce good and bad code?”

A coding agent treats everything you leave unsaid as a decision it is allowed to make. Each technique below closes one kind of gap:

Gap in the promptWhat the agent doesTechnique that closes it
Requirements you have not statedGuesses a plausible defaultAsk for questions first
Library, config and error behaviourImprovisesState constraints
“Done” is undefinedStops when the code looks doneName the check it must run
Your house conventionsInvents its ownPoint at an existing file
The approach itselfCommits to the first ideaPlan, then implement one step

The order matters. Questions come first because the answers change the constraints; the check comes before the plan because a plan without a check cannot be accepted or rejected.

Ask the agent to question you before it writes code

Section titled “Ask the agent to question you before it writes code”

End any request larger than a one-line fix by asking the agent what it needs to know. The agent exposes its assumptions while they are still cheap to change, and you answer them in one message instead of un-picking them from a diff.

The cap on questions matters. Without it, some sessions return twenty trivial questions and the important one is buried at number fourteen. Anthropic’s Claude Code best practices describe a longer variant for large features: the agent interviews you with its AskUserQuestion tool and writes the result to a spec file, and you implement it in a fresh session (Claude Code best practices, checked 2026-09-26). A written spec is the input the rest of the artifact chain builds on.

The same closing line works across the lifecycle: “ask what you need to write the best spec”, “ask what you need to plan this”, “ask for any context that would help diagnose this failure”.

State the constraints and the check the agent must run

Section titled “State the constraints and the check the agent must run”

Vague requests get generic code. Name the library, the configuration source, the failure behaviour and, most importantly, the command that decides whether the work is finished.

  • Instead of: “Add a database connection.”
  • Write: “Add a PostgreSQL connection using the pg library. Read the connection string from DATABASE_URL. Retry with exponential backoff, at most five attempts. Export a typed healthCheck() function. Done when npm test -- db passes and npm run typecheck is clean.”

The last sentence does the most work. Anthropic’s best practices put it plainly: without a check the agent can run, “looks done” is the only signal available, and you become the verification loop (Claude Code best practices, checked 2026-09-26). With a check, the agent runs it, reads the failure and iterates on its own.

“Do not edit or delete existing tests” is not decoration. An agent that cannot make a check pass will sometimes weaken the check instead; the protect-the-oracle page covers how to enforce that mechanically. For turning a user story into checks like these, see executable acceptance criteria.

Point at an existing file instead of describing the convention

Section titled “Point at an existing file instead of describing the convention”

To make the agent match your codebase, show it a file that already follows the convention. A concrete example is a tighter specification than a paragraph of prose, and it carries details you would forget to mention: error types, logging calls, naming, export style.

  • Instead of: “Create a service class with a constructor, a private logger, and JSDoc on public methods.”
  • Write: “Create PaymentService following the structure of src/services/AuthService.ts: same constructor injection, same error classes, same logging. Do not copy its TODO comments or its deprecated legacyLogin method.”

Pick a file that has been reviewed recently, and say what not to copy. The agent reproduces the example faithfully, including its mistakes. Each tool also has a way to attach the file directly, shown in the per-tool section below.

Plan first, then implement one step at a time

Section titled “Plan first, then implement one step at a time”

For anything bigger than a small fix, separate thinking from doing. The agent proposes a plan with no code; you correct the plan; then it implements one step and stops.

  1. Ask for a plan and forbid edits. Switch the tool to its read-only planning mode (see agent mode vs ask mode for the exact switch in each tool), then send:

  2. Correct the plan, not the code. The plan comes back as a numbered list. Fix it while it is still text: “Use a separate AvatarUpload component instead of editing ProfilePage. Validate the MIME type on the server, not only in the browser.”

  3. Implement one step and stop.

This loop catches a wrong approach when it costs one sentence to fix, not a 30-file diff. The full discipline, from intent through spec and plan to tasks, is on the artifact chain page.

Priming the model with a role (“you are a senior security engineer”) is widely taught, but the evidence does not support it as an accuracy lever. Zheng, Pei, Logeswaran, Lee and Jurgens (“When ‘A Helpful Assistant’ Is Not Really Helpful”, arXiv, November 2023) tested 162 roles across 4 model families and 2,410 factual questions and found that adding a persona to the system prompt did not improve performance compared with no persona, and that the effect of any single persona was “largely random” (paper arXiv 2311.10054, abstract as published in the authors’ repository, read 2026-09-26).

A role still shapes what the model looks at and how it formats the answer. Use it for focus, and let explicit criteria carry the weight:

  • Weak: “You are an expert security engineer. Review this code.”
  • Strong: “Review src/api/upload.ts for path traversal, missing MIME validation, unbounded file size and SSRF through the URL-import option. For each finding: file, line, exploit sketch, fix.”

A more useful move than one persona is several viewpoints at once, because disagreement surfaces the trade-offs a single answer hides.

Which prompts get a stuck or finished agent back on track?

Section titled “Which prompts get a stuck or finished agent back on track?”

Three prompts pay for themselves on almost every task.

Hypothesis before fix. When the agent is thrashing on a bug (edit, re-run, revert, repeat), stop it and force a measurement instead of another guess. The error-driven development page builds a whole workflow on this.

Self-review against the request. A second pass over the agent’s own diff, checked against what you asked for, catches scope creep and forgotten requirements. It is a filter, not proof; the next section covers proof.

The second draft. The first working version is often exploration. Once the tests pass, the agent knows enough to write a simpler version behind the same tests.

How do you check the result without reading every line?

Section titled “How do you check the result without reading every line?”

Prompting well moves the work from reading code to checking evidence. Build the evidence into the prompt, then let machines and a second reviewer do the reading.

  1. The check is in the prompt. Every implementation prompt ends with “done when” commands (tests, types, lint), and the agent pastes their output. A missing or red result means the task is not done, whatever the summary says.

  2. The agent writes an evidence report. Ask for it at the end of every task:

  3. A second reviewer reads the diff, not you. Run an independent review in a fresh context: /code-review in Claude Code, /review or codex review in Codex, or Bugbot on the pull request in Cursor. The agent that wrote the code is a weak judge of it.

  4. You sign off on the evidence, CI decides the merge. You read the report, spot-check the tests that prove the riskiest requirement, and read only the files the reviewer flagged. CI runs the same checks again and blocks the merge on red.

What a complete report must contain for a pull request is on the evidence bundle page, and the review routine is on reviewing an agent’s pull request. For code that passes its tests but is still wrong, see catching plausible-but-wrong code.

Where prompting differs in Claude Code, Codex and Cursor

Section titled “Where prompting differs in Claude Code, Codex and Cursor”

When output quality drops, check the setup before the model: missing project instructions, no file reference, too little reasoning effort, or a context window full of failed attempts. The techniques above are identical in all three tools; the levers differ. Commands below were checked on 2026-09-26 against claude --help 2.1.283 and codex --help 0.157.1, and the Claude Code command reference.

  • Project instructions: run /init to generate a CLAUDE.md, then prune it as the project changes.

  • Point at files: type @ plus a path in the prompt, for example @src/services/AuthService.ts, to include the file directly.

  • Plan first: /plan <description> enters plan mode for that task.

  • Reasoning effort: /effort high (or xhigh) before a hard design or refactor; claude --effort high sets it for a session. From v2.1.280 (the latest channel) the default model is Claude Opus 5.5, which runs at medium effort by default, so raise effort before switching model.

  • Context hygiene: /context shows what fills the window; /compact <focus instructions> summarises mid-task; /clear starts fresh between unrelated tasks.

  • Current library docs: install the Context7 plugin from the official marketplace:

    Terminal window
    claude plugin install context7@claude-plugins-official

    Then add “use context7” to a prompt that touches a fast-moving library.

Which model to run, and when to leave the default, is covered on the models hub. The rule this site follows: start on the tool’s default model, tune effort before switching, and switch only when your own evals say so. For the deeper per-tool prompt catalogues, see Claude Code prompt engineering, Codex prompt engineering and Cursor prompt templates.

Tech leads: once a prompt from this page works for your team, stop pasting it. Commit it as a skill so every developer and every tool runs the same version; see agent skills.

What goes wrong with these prompts, and how do you recover?

Section titled “What goes wrong with these prompts, and how do you recover?”
  • The agent keeps making the same mistake after you corrected it. The context is full of failed attempts. Anthropic’s guidance is to start fresh once you have corrected the same issue more than twice (best practices, checked 2026-09-26). Recovery: /clear in Claude Code or /new in Codex, then send one prompt that includes what you learned.
  • The clarification step returns trivial questions. Recovery: cap the number, demand “questions whose answers would change the design”, and ask it to read the code first so it does not ask what the repository already answers.
  • The agent copied the example file’s bugs. Recovery: pick a recently reviewed file and add “do not copy” lines for known debt.
  • The implementation drifts from the approved plan. Recovery: implement one step per prompt and require a “deviations from the plan” list at the end of each step.
  • Self-review reports that everything is fine. Self-review filters; it does not prove. Recovery: rely on the check commands and an independent reviewer in a fresh context, as in the verification steps above.
  • The check passes because the agent edited the check. Recovery: forbid test edits in the prompt, diff the test directory before accepting, and make the checks read-only to the agent as described in protect the oracle.
  • The code targets an old library API. Recovery: add Context7 or paste the current docs page; see documentation as context.
  • Quality drops late in a long session. Recovery: check context use (/context in Claude Code) and compact or restart; see context windows.