Sub topic 8
Writing prompts for multi-step tasks
This chapter assumes you have used chat-based AI assistants in VS Code, know basic Git, and can run build and test commands in a terminal. It also assumes you have read the earlier chapters on the agent loop, context, and instructions files.
Why prompts matter more in agent mode
In ask mode, a vague prompt gives you a vague answer that you can read and discard. In agent mode, a vague prompt leads to real edits and real commands. The agent plans, edits files, runs tools, and decides for itself when it is done. If your prompt does not say what "done" means, the agent will pick a definition.
A good multi-step prompt gives the agent a loop it can check itself against. It contains five parts:
- Goal: what should change, in one sentence.
- Location: which files or functions are involved.
- Acceptance criteria: how the new behavior should look.
- Verification: a command the agent can run to prove it works.
- Constraints: what must not change.
A worked example
Here is a prompt that includes all five parts.
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.Each line has a job:
| Line | Part | What it does |
|---|---|---|
| Add input validation to the createUser function in src/users.ts. | Goal and location | Names the change and the file, so the agent does not search the whole project. |
| Reject empty names and malformed email addresses with a clear error. | Acceptance criteria | Defines two failure cases and the expected behavior. |
| Add unit tests ... then run `npm test` and fix any failures. | Verification | Lets the agent observe results and iterate. |
| Do not change the public function signature. | Constraint | Prevents a refactor that breaks callers. |
The verification step matters most. The agent loop works by calling a tool, observing the result, and deciding what to do next. A test command gives it a clear signal. Without one, the agent can only judge its own code by reading it.
How a prompt maps onto the loop
Splitting large tasks
Agents handle one coherent change well. They handle "rewrite the whole backend" badly, because context fills up and early decisions get forgotten. Split large work into steps that each end in a verifiable state.
- Ask for a plan first. For example: "Before editing anything, list the files you would change and why." Review the plan, correct it, then say to proceed.
- Do one step per request, and commit when the tests pass. A commit gives you a checkpoint to return to.
- Start a new chat for an unrelated task, so old context does not leak into the new one.
Common prompt mistakes
- No verification step. The agent stops after editing, and you find the failures yourself.
- Vague scope. "Clean up the user code" invites edits in many files. Name the files and the kind of cleanup.
- Unstated constraints. If the agent may not touch
package-lock.jsonor change an API, say so, or put it in instructions. - Several unrelated goals in one prompt. The agent may finish one and quietly skip the rest. Send them separately.
- Describing the solution instead of the problem. If you dictate every line, you lose the benefit of the agent. State the outcome and the limits.
Exercise
A teammate writes this prompt: "Improve error handling in the app." List two problems with it and rewrite it using the five parts.
Show answer
Problems: it names no files, so the scope is unbounded, and it has no acceptance criteria or verification step, so the agent decides when it is done. A rewrite: "In src/api/client.ts, wrap the fetch calls in fetchOrders so network failures and non-200 responses throw an error with the status code and URL. Add tests in src/api/client.test.ts for both cases, then run npm test and fix any failures. Do not change the exported function signatures."