How to Write a Good First Prompt for a Code Change

JS
Jai Singh
16 Nov, 2026 3 min read 31 views

A good prompt for a code change says what you want, where it goes, what must not change, and how to check it worked. Agents are good at filling gaps, but they fill them with guesses. Every gap you close is one less guess to review. You do not need long prompts, just complete ones.

The four parts of a useful prompt

  1. Goal. The behaviour you want, described from the user's or caller's point of view.
  2. Location. The files, functions or modules involved, if you know them.
  3. Constraints. What to keep, what to avoid, which style to follow.
  4. Verification. How the agent can confirm success: a command, a test, an expected output.

Weak versus strong

Compare two prompts for the same job.

Weak:
Make the search faster.

Strong:
The product search in src/search/products.js loads every product and filters
in JavaScript. Change it to filter in the SQL query using the existing
db.query helper, keeping the function signature and return shape. Do not
add new dependencies. Run npm test; the tests in test/search.test.js must
still pass. Tell me if any test needs to change and why.

The weak prompt forces the agent to decide what "faster" means, where to look and how far to go. It might add a cache, rewrite the module or install a search library. The strong prompt gives a clear target and fences off the options you do not want.

Describe behaviour, not just code

When possible, say what should happen with concrete inputs. Examples are the most compact way to remove ambiguity.

Add a formatBytes(n) helper to src/format.js.
Examples:
  formatBytes(0)        -> "0 B"
  formatBytes(1023)     -> "1023 B"
  formatBytes(1024)     -> "1.0 KB"
  formatBytes(1536)     -> "1.5 KB"
  formatBytes(1048576)  -> "1.0 MB"
Use base 1024. Add a test for each example.

An agent can turn those examples straight into tests, and you can read the tests to confirm that its understanding matches yours.

State limits explicitly

Agents tend to be helpful beyond the request: they tidy neighbouring code, rename things or upgrade a library. If you want a narrow change, say so.

  • "Only change files under src/search."
  • "Do not modify existing tests; add new ones."
  • "Keep the public API of this module unchanged."
  • "Do not install new packages."

Ask the agent to report uncertainty

Add one sentence that invites honesty: "If anything is ambiguous, ask me before changing code" or "List any assumptions you made." This is cheap, and surfaces mistakes early. For bigger tasks, ask for a plan first and approve it; the post on plan-first prompts covers this.

A reusable template

Goal: <what should be true when this is done>
Where: <files / modules / functions>
Constraints: <style, limits, things not to touch>
Verify: <command to run and expected result>
Report: <what to tell me when finished>

You will not fill every line every time. A one-line typo fix needs one line. The template is a reminder of what to consider.

Match the prompt to the size of the task

A rename inside one function needs a single sentence. A change that crosses layers, such as a new field from database to API to UI, deserves a few lines on each layer and an explicit list of tests to update. If you cannot describe the finished result in a short paragraph, the task is probably too big or too unclear; split it, or ask the agent to propose a plan first. Writing the prompt also forces you to clarify your own thinking, and many developers find that the act of writing the acceptance check reveals a missing requirement before any code is produced.

Common prompt mistakes

  • Bundling several unrelated tasks in one prompt.
  • Describing a solution you are unsure about instead of the problem.
  • Leaving out the test command, so nothing verifies the result.
  • Using pronouns like "fix it" after a long conversation; name the thing.
  • Pasting secrets or customer data as examples.

Iterate, do not perfect

Your first prompt does not have to be perfect. Look at the result, notice what the agent assumed, and refine. If you find yourself adding the same constraint repeatedly, move it into your project instruction file so every prompt starts better.

Was this article helpful?

Comments

No comments yet. Have a question or a tip? Be the first to share it.

Leave a comment

Comments are reviewed before they appear.