Writing
Prompt Engineering: Make the Task Clear
A prompt’s job is to define the task: the goal, the constraints, examples of a correct result and the shape of the answer. How to write one for a coding agent, and how to tell whether a prompt change helped or the model needed better inputs.
By Ka Lun Chan · Cloud & AI · AI / Engineering judgment
What is a prompt for?
A prompt defines the task: what to achieve, what not to change, what a correct result looks like and what to report back. Prompt changes fix failures where the model misunderstood what you wanted. They do little when the model understood perfectly and lacked the information, the tools or the checks to get it right.
Part of a series on what it takes to make an AI system reliable. This part covers the task definition. What the model sees alongside it is the next part, context engineering.
A prompt for an agent that edits code has much in common with a good engineering ticket. Specify the outcome, constraints, examples that settle edge cases and a definition of done.
The ticket is not the task
The series follows one illustrative scenario. A ticket says the Quantity column on the Orders page sorts as 1, 10, 2. A coding agent can read the repository, edit files, run tests and open a pull request.
Hand the agent the ticket as written and you have left most of the decisions to it. Should it fix only Quantity, or every numeric column? Can it change how quantities display? Where do blank values go? Does “fixed” mean a unit test passes, or that the table sorts correctly when someone clicks the header? A capable model will pick reasonable answers. They may not be yours, and you won’t find out which ones it picked until review.
The likely cause is simple. Text compares character by character, so “10” sorts before “2”, and JavaScript’s default sort() compares values as strings unless you give it a compare function. The likely fixes are where it gets interesting, because several of them make the 1, 10, 2 case pass and break something else.
Writing the task
The rewrite isn’t clever. It fills in the parts the ticket left out.
| Part | The ticket | The task |
|---|---|---|
| Goal | “Quantity sorts wrong. Please fix.” | Clicking Quantity sorts rows by numeric value, ascending then descending. |
| Scope | Unstated | The Orders table sort. Other tables and the API are out of scope. |
| Constraints | Unstated | Keep the displayed formatting. Text columns keep sorting alphabetically. No new dependencies. |
| Examples | 1, 10, 2 | 2, 10, 1,200 sort as 2, 10, 1,200. Blank quantities sort last in both directions. |
| Done means | Unstated | The cases above pass through the table component, and the existing test suite still passes. |
| Report | Unstated | Root cause, files changed, commands run with results, and anything not verified. |
As the prompt the agent receives, that comes out something like this:
<task>
Fix sorting on the Quantity column of the Orders table.
Clicking the header should sort rows by numeric value,
ascending on the first click and descending on the second.
</task>
<constraints>
- Keep how quantities are displayed (for example "1,200").
- Text columns such as Customer must keep sorting alphabetically.
- Change only files under src/orders/ and src/table/.
- Do not add dependencies or change the API.
- If you can't reproduce the bug, stop and say so.
</constraints>
<expected>
Ascending: 2, 10, 1,200, then blanks.
Descending: 1,200, 10, 2, then blanks.
</expected>
<done>
The expected orderings pass through the OrdersTable component,
and the existing test suite passes.
</done>
<report>
Root cause in one or two sentences. Files changed.
Commands you ran and their results. Anything you did not verify.
</report>Anthropic’s prompting guidance recommends XML tags to separate instructions, examples and data. Clear boundaries also make the prompt easier for a reviewer to read.
Two lines carry more weight than they look. “If you can’t reproduce the bug, stop and say so” gives the agent permission to fail usefully, so it doesn’t invent a fix for a bug it never saw. And the scope line gives a reviewer something concrete to check: any change outside those directories is a question, whatever else the diff does.
Examples that pin down the edge cases
The examples in the prompt do more work than the instructions. “Sort numerically” leaves room for interpretation. “2, 10, 1,200, then blanks” doesn’t. Each value in that list is there for a reason: 2 and 10 are the reported bug, 1,200 has a thousands separator that breaks a naive conversion, and blanks force a decision about where empty values go.
Anthropic’s guidance calls this few-shot prompting. Pick examples that define the behavior. When the list grows long, put the additional cases in a test suite.
Ask for the report you will need
The last part of the prompt is easy to skip and matters most after the run. Whatever the agent produces goes to someone: a reviewer, another agent, or an automated check. Ask for what that next step needs.
Ask for the root cause, changed files, exact commands and results, and what remains unchecked. An explicit note about missing descending-order coverage tells the reviewer where to look next.
When the output feeds code instead of a person, make it structured: a fixed set of fields your harness can parse and validate. A free-text report that a script greps for “success” is a reliability problem waiting to happen.
How do you tell whether a prompt change helped?
Run the same set of cases before and after the change, and compare. A prompt change helped if more cases pass and none that passed before now fail. Trying only the case that failed tells you almost nothing, because model output varies from run to run and a fix for one case can quietly break another.
Anthropic’s prompt engineering overview puts this first: define success criteria and a way to test against them before you start editing prompts. For the sorting agent, the cases might be the original bug, the 1,200 value, blanks, descending order, and a check that Customer still sorts alphabetically. Five tasks, run a few times each, is enough to see whether a change moved anything.
Run each case more than once. If the agent fixes the bug on two runs out of three, the prompt isn’t reliable yet, and one lucky run would have told you it was. Evaluation engineering covers how to build those cases and keep them.
When does the model need better inputs instead?
The same overview cautions that only some failing evaluations are best addressed through prompting. Read the failure before adding a rule.
| What the agent did | Fix in | Why |
|---|---|---|
| It fixed Quantity by changing how numbers display | Prompt | The constraint to keep formatting was missing |
| It sorted every column numerically, including Customer | Prompt | Scope and the text-column constraint were missing |
| It reported success without running anything | Prompt, then loop | Done was undefined; the loop should also check |
| It edited a sort helper that the table no longer uses | Context | It never saw the table component |
| It couldn’t tell which test failed or why | Tools | The test runner returned a wall of unparsed output |
| It understood everything and the fix still broke 1,200 | Evaluation and loop | Nothing checked that case before it said done |
Read the transcript and ask whether the agent understood the task. If it followed the instructions and the result still failed, check what it could see, what the tools returned or what verification missed.
The opposite mistake happens too. Teams build retrieval, tools and evaluation around a task nobody wrote down clearly, and then debug all of it when the real problem was a missing constraint. Write the task first. It is the cheapest fix there is.
Sources
Checked in November against the documentation below. The sorting-bug scenario and the prompt are illustrative.
Short answers
How do you know whether a prompt change actually helped?
Run the same set of cases before and after the change and compare the results. If you only try the case that failed, you can’t see whether the change broke something else. Without a fixed set of cases, a better-looking answer is an impression, not evidence.
When should you stop editing the prompt?
When the model understood the task and still got it wrong. If it lacked the relevant code, misread a tool result or had no way to check its work, a longer prompt won’t fix it. Give it better inputs, a better tool or a test instead.
Before you edit the prompt again
- Read the transcript and decide whether the agent understood the task.
- If it didn’t, add the missing goal, constraint, example or definition of done.
- Rerun the same cases you ran before, more than once each.
- If it understood and still failed, look at its inputs, tools and checks instead.
The same principles apply to writing tasks for people. See How to Write User Stories Without Making Everyone Guess.