Sub topic 7
Break larger tasks into steps
This chapter assumes you can write a specific goal, add context, and set constraints (covered in earlier chapters). It also assumes you can run tests in at least one language. Here you will learn how to split a large request into a sequence of smaller prompts, and how to avoid the errors that come from doing it badly.
Why one big prompt fails
A single prompt such as "build a blog tool with posts, tags, slugs, search, and an RSS feed" asks the assistant to make dozens of design decisions at once. You didn't specify most of them, so the assistant guesses. The result usually has three problems:
- Hidden decisions. The assistant picks a storage format, an error policy, and a module layout. You find out only when you read the whole output.
- Hard review. A 400-line response is hard to check. A bug in line 30 can hide behind code that looks fine.
- Hard correction. If the data model is wrong, everything built on it is wrong too, and you must re-request all of it.
Smaller prompts fix this because each one has its own testable goal. You review a small piece, confirm it works, and only then build on it.
What a good step looks like
A good step has the same properties as a good single prompt:
- One goal. You can state it in one sentence.
- A check. You can tell, by running a test or reading a short diff, whether it succeeded.
- Clear inputs. Everything it depends on is either in the prompt or already in code you pasted.
- A small surface. It touches one function, one module, or one concern.
If you cannot say how you would check a step, it is too vague or too big. Split it again, or rewrite the goal.
A decomposition procedure
Follow these four moves.
- List the pieces. Write down the parts of the task in plain language. Don't write prompts yet.
- Find the dependencies. Mark which pieces need the output of others.
- Order the pieces. Put dependencies first. Pure functions with no side effects usually come before code that touches files, networks, or databases.
- Attach a check to each piece. Choose a test, a command, or a review question.
The diagram shows the dependencies for a small blog tool.
Steps 1 and 2 can be tested without touching the disk. Step 3 is the first one with side effects, so it comes after you trust the pure parts.
Writing the step prompts
Each step prompt uses the same template as before (task, context, constraints, examples, output format). What changes is the context. Later steps need the earlier results, so you paste in the code that already works.
Step 1: a pure function
Task: Write a Python 3.10 function slugify(text: str) -> str that converts an article title into a URL slug.
Context: This is step 1 of a small blog tool. Titles are plain ASCII.
Constraints: Standard library only; lowercase; each run of non-alphanumeric characters becomes one hyphen; no leading or trailing hyphens.
Examples: slugify("Hello, World!") -> "hello-world"
Output format: Only the function.You run the tests for it, and only when they pass do you move on.
Step 2: build on the verified code
The second prompt includes the verified function as context, and it states what must not change.
Task: Add a Post dataclass and a function make_post(title: str, body: str) -> Post.
Context: Python 3.10. This code already exists and passes its tests:
import re
def slugify(text: str) -> str:
text = text.strip().lower()
text = re.sub(r"[^a-z0-9]+", "-", text)
return text.strip("-")
Constraints: Use dataclasses from the standard library. Post has fields title, slug, body. make_post sets slug by calling slugify. Do not modify slugify. Raise ValueError if the title is empty or becomes an empty slug.
Examples: make_post("Hello, World!", "Hi").slug -> "hello-world"
Output format: Only the new code, not a repeat of slugify.Notice three things. The existing code is pasted rather than described, so the assistant cannot misremember it. The constraint "do not modify slugify" protects the work you already verified. The error behavior is stated, so the assistant doesn't invent one.
The matching code might look like this:
import re
from dataclasses import dataclass
def slugify(text: str) -> str:
text = text.strip().lower()
text = re.sub(r"[^a-z0-9]+", "-", text)
return text.strip("-")
@dataclass
class Post:
title: str
slug: str
body: str
def make_post(title: str, body: str) -> Post:
slug = slugify(title)
if not slug:
raise ValueError("title must contain at least one letter or digit")
return Post(title=title, slug=slug, body=body)
if __name__ == "__main__":
post = make_post("Hello, World!", "Hi")
print(post.slug)Running it prints hello-world. An empty title gives an empty slug, so the single if not slug check covers both error cases in the prompt.
Choosing the size of a step
There is no fixed rule, but these signals help.
| Signal | What it suggests |
|---|---|
| You can't write a test or check for the step | Split it, or clarify the goal |
| The response needs more than one file or about 100 lines | Probably too big |
| The prompt contains the word "and" joining two different goals | Split at the "and" |
| The step is a one-line change | Too small. Combine it with a neighbor |
| The assistant keeps asking or guessing about the same thing | Add that detail to the context |
Over-splitting has a cost. Each prompt needs context, and tiny steps can produce pieces that don't fit together. Aim for steps you can review in a few minutes.
Ask for a plan first
For a task you don't know how to split, ask the assistant to plan before it writes code. This is itself a decomposition step.
Task: Propose a plan for adding RSS feed output to my blog tool. Do not write code yet.
Context: Python 3.10, standard library only. The tool has slugify, a Post dataclass, and JSON file storage.
Constraints: Maximum 6 steps. Each step must be testable on its own and name the function or file it changes.
Output format: A numbered list. For each step, give the goal and how to test it.Then review the plan before approving it. Look for missing steps, wrong ordering, and invented requirements. Edit the plan, then use each line as the goal of a later prompt. The plan is cheap to fix. Code built from a bad plan is not.
Carry state between steps
The assistant only knows what is in the conversation. Long chats cause two problems. Early details can be forgotten or contradicted, and a wrong early answer stays in the conversation and influences later ones. Three habits help.
- Restate the decisions. Start each step with a short list of what is already fixed: "Python 3.10, standard library only, Post has title, slug, body."
- Paste current code, not remembered code. If you edited the assistant's output, paste your edited version. Otherwise the assistant works from a version that no longer exists.
- Start a fresh conversation when it drifts. If answers get inconsistent, open a new chat and paste in a short summary and the current code.
Common errors when splitting
- Interface drift. Step 2 calls
slugify(text, max_length=50), but step 1 never had that parameter. Fix it by pasting the real signature into each later prompt and asking the assistant to use only the functions you list. - Unrequested rewrites. The assistant "improves" earlier code while adding the new feature. State "do not modify existing functions" and compare the result with a diff.
- Skipping verification between steps. A bug in step 1 then shows up as a confusing failure in step 4. Run the check for each step before starting the next.
- Splitting by file instead of by behavior. "Write models.py" is a weak goal. "Add a Post type that rejects empty titles" is testable.
- Splitting a tightly coupled change. Some changes, such as renaming a function used in many places, must happen in one step. Ask for it as one change and request the output as a diff.
When not to split
Splitting adds overhead. A single prompt is the better choice when:
- The task is small and has one clear check, such as a single function with examples.
- The pieces are so intertwined that separating them forces you to repeat the same context in every prompt.
- You are exploring and don't yet know what you want. A quick rough attempt can show you what the steps should be.
Summary of the working model
Decompose until each step has one goal and one check. Order steps so dependencies come first and side effects come last. Give each prompt the verified code it depends on, plus the constraints that protect it. Verify before moving on. If you are unsure how to split, ask for a plan and edit it.
Exercise
You want an assistant to add a "tags" feature to the blog tool: a post can have tags, tags are normalized (lowercase, trimmed, no duplicates), posts can be listed by tag, and the index page shows a tag list. Break this into 4 ordered steps. For each, give the goal and one check. Say which step you would do first and why.
Show answer
One valid answer: (1) Write a pure function normalizetags(tags: list[str]) -> list[str] that lowercases, trims, drops empty strings, and removes duplicates while keeping order. Check: unit tests such as normalizetags([" Python", "python", ""]) -> ["python"]. (2) Add a tags field to Post, using normalizetags in makepost. Check: makepost with duplicate tags returns a post with a clean list, and existing tests still pass. (3) Add postsby_tag(posts, tag) -> list[Post]. Check: a test with three posts and two tags returns the right subset. (4) Update the index page generator to render the tag list. Check: the generated output contains each tag once. Do step 1 first because it is a pure function with no dependencies, so it is easy to test, and steps 2 to 4 all rely on tags being normalized the same way.
Exercise
Your assistant's step 3 response calls a function loadposts(directory, recursive=True), but the loadposts you wrote in step 2 takes only a directory. Name the error, give two ways it could have been prevented, and say what you do now.
Show answer
This is interface drift: the assistant used a signature that does not exist. It could have been prevented by pasting the real signature or code of load_posts into the step 3 prompt, and by adding the constraint "use only these existing functions with these exact signatures." Now, reply with the correct signature and ask for a corrected version, or edit the call yourself. Then run the step's check before continuing.