Sub topic 8
Writing prompts for multi-step tasks
A multi-step task is one the agent cannot finish in a single edit. It has to read code, change several files, run commands, and react to the results. This chapter covers how to write a prompt that gives the agent a clear target and a way to check its own work.
This chapter assumes you have read the earlier chapters on the agent loop, context and attachments, and custom instructions. You should be comfortable with Git diffs and with running your project's test command from a terminal.
Why prompts matter more in agent mode
In ask mode, a vague prompt gives you a vague answer, and you read it and move on. In agent mode, a vague prompt gives the agent room to make decisions. It may pick the wrong file, invent its own definition of "done", or keep editing long after the useful work is finished. Each loop iteration builds on the last, so an early wrong assumption carries through every later step.
A good prompt reduces three risks:
- Wrong target. The agent edits something you did not intend.
- Wrong finish line. The agent stops too early, or never stops.
- Unverified result. The agent reports success without running anything.
The parts of a strong task prompt
Most effective task prompts contain five parts. You do not need a heading for each one. A few plain sentences work.
| Part | What it tells the agent | Example |
|---|---|---|
| Goal | The change you want | Add input validation to `createUser` |
| Location | Where to work | `src/users.ts` |
| Acceptance criteria | What "correct" looks like | Reject empty names and malformed emails with a clear error |
| Verification | How to check the work | Run `npm test` and fix failures |
| Constraints | What must not change | Do not change the public function signature |
Here is a prompt that includes all five:
Add input validation to the createUser function in src/users.ts.
Reject empty names and malformed email addresses with a clear error.
Add unit tests for both cases next to the existing tests, then run `npm test` and fix any failures.
Do not change the public function signature.The first line gives the goal and location. The second line gives the acceptance criteria. The third line asks for tests and a verification step. The last line is a constraint. The agent can now run a loop it can judge itself against: edit, run the tests, read the failures, edit again.
Why verification is the most valuable part
The agent can only correct itself when it can observe a result. A test command, a type-check, or a linter gives it a concrete signal. Without one, "done" means "the agent believes it is done".
Name the exact command. "Make sure it works" leaves the agent to guess what to run. "Run npm test" does not. If your project has a standard command, put it in your instructions file once. In the prompt, refer to the task-specific check, for example "run only npm test -- users" to keep the loop fast.
Scoping: how big should one task be?
Agents handle a task well when you can describe it in a short paragraph and check it with one command. They struggle when a prompt bundles unrelated work.
Compare these two prompts:
Refactor the whole app to use the new API client and fix any bugs you find.Replace direct fetch calls in src/services/orders.ts with the apiClient from src/lib/apiClient.ts.
Keep the existing function names and return types.
Run `npm run typecheck` and `npm test -- orders` and fix any failures.The first has no boundary. "Any bugs you find" invites unrequested edits across the project. The second names one file, one replacement, and two checks.
Use these rules of thumb:
- If the task touches more than a handful of files, split it into stages and run them one at a time.
- If two parts are independent, such as a code change and a documentation update, make them separate prompts.
- If you are unsure of the approach, ask for a plan first and implement in a second prompt.
Plan first, then implement
For a task with real design choices, start by asking for a plan without edits:
Do not edit any files yet.
Read src/users.ts and its tests, then list the steps you would take to add email validation.
Note any files you would need to change and any risks.Read the plan and correct it. Then send a second prompt such as "Implement steps 1 to 3 of your plan. Stop before step 4." This costs one extra turn, and it catches wrong assumptions before they turn into edits.
Giving context without overloading it
Your prompt competes with attached files, search results, and earlier turns for space in the model's context window. Include what the agent needs and leave out the rest.
- Name files with
#filereferences or attachments when you already know where the work belongs. This saves the agent a search. - Describe patterns to follow by pointing to an example: "Follow the structure of
src/orders.test.ts." - Paste exact error text when fixing a failure, not a paraphrase. Error messages often contain the file and line the agent needs.
- Do not paste large logs. Attach the relevant section or tell the agent which command to run to reproduce the problem.
Conventions that apply to every task belong in your instructions file, not in each prompt. Keep the prompt for what is specific to this change.
Setting boundaries
Constraints are as important as goals. Agents tend to be helpful beyond the request, and that can cause damage.
Useful constraints include:
- Scope limits: "Only edit files under
src/billing/." - Interface limits: "Do not change exported function signatures."
- Dependency limits: "Do not add new npm packages."
- Behavior limits: "Do not delete or skip existing tests."
- Stop conditions: "If the tests still fail after three attempts, stop and explain what you tried."
The last one matters in practice. Without a stop condition, an agent that cannot fix a failure may start changing unrelated code or weakening tests to make them pass. A stop condition turns a runaway loop into a report you can act on.
Iterating on a prompt
Your first prompt is rarely your best. When the result is off, resist the urge to pile on corrections in the same thread without thinking. Decide what went wrong:
- The agent misread the goal. Rewrite the prompt more precisely and try again from a clean state.
- The agent lacked context. Attach the missing files or add a pointer to an example.
- The agent had no good check. Add a verification command.
- The agent exceeded its scope. Add an explicit constraint, then undo the extra edits.
Before a risky task, commit or stash your current work so you have a clean checkpoint:
git status
git add -A
git commit -m "checkpoint before agent task"If the result is wrong, you can discard the agent's changes with git restore . for tracked files, and start again with a corrected prompt. A prompt that failed once, restarted from a clean tree with a fix, usually works better than a long thread of patches. The chapter on reviewing and undoing changes covers this in more detail.
Common mistakes
| Mistake | What happens | Fix |
|---|---|---|
| No verification step | The agent claims success without checking | Name the test or build command |
| "Fix everything" | Unrelated files change | Name one goal and one location |
| Repeating project rules in every prompt | Prompts grow long and drift | Move rules to the instructions file |
| Paraphrasing an error | The agent searches for the wrong thing | Paste the exact message |
| Combining unrelated work | Hard to review and hard to undo | Split into separate prompts |
| No stop condition | The agent loops or weakens tests | Say when to stop and report |
Exercises
Exercise
A teammate sends this prompt to the agent: "Improve the checkout code and make sure it's good." Name three of the five parts of a task prompt that are missing, and say what could go wrong because of each.
Show answer
The prompt lacks a specific goal, a location, acceptance criteria, a verification step, and constraints. Any three of these work. Without a goal and location, the agent may edit unrelated files. Without acceptance criteria, it defines "improve" on its own and may stop too early or keep going. Without a verification step, it cannot check its work and may claim success without running tests. Without constraints, it may change public interfaces or remove tests.
Exercise
Rewrite this prompt so the agent can verify its own work: "Make the date parser handle ISO 8601 strings in src/dates.ts." Your project runs tests with npm test.
Show answer
A sample answer: "Update the parseDate function in src/dates.ts so it accepts ISO 8601 strings such as 2026-11-27T13:15:22Z and returns a Date. Add unit tests for a valid string and an invalid string next to the existing tests, then run npm test and fix any failures. Do not change the function's exported name or signature, and do not modify existing tests." The answer must contain a verification command and at least one constraint.
Exercise
Your prompt asked the agent to fix a failing test. After four attempts, the agent has edited six files and loosened an assertion. Name the prompt element that would have limited this, and give example wording.
Show answer
A stop condition combined with a scope or behavior constraint. Example: "Only edit files under src/billing/. Do not modify existing tests. If the test still fails after three attempts, stop and explain what you tried."