Sub topic 7
Telling the Assistant What to Do
This chapter assumes you have used an AI chat or coding assistant before and can run a Python file from a terminal. It covers the task statement. This is the part of a request that says what the assistant should do with the context you supplied.
Why the task statement matters
Files, selections, and errors are raw material. None of them says what you want. Suppose you attach stats.py and paste the ZeroDivisionError. The assistant has to guess which of these you want:
- an explanation of the error
- a fix that raises a clearer exception
- a fix that returns a default value
- a rewrite of the whole module
- a unit test that reproduces the failure
Each guess leads to a different answer, and most of them are not what you wanted. The task statement removes the guessing. It is usually the shortest part of a request, and it has the largest effect on the answer.
A good task statement has three parts:
- The action. What should happen: explain, fix, refactor, review, write a test.
- The target. Which code or behavior the action applies to.
- The constraints. What must stay the same, and what the result must look like.
Pick one verb and make it specific
Start with a verb that names the kind of work. "Look at this" and "help with this" leave the action open. The verbs below each lead to a different kind of answer.
| Verb | What you get | Changes your code? |
|---|---|---|
| Explain | A description of what the code or error means | No |
| Diagnose | A likely cause, with reasoning | No |
| Fix | A modified version of the code | Yes |
| Refactor | The same behavior with a different structure | Yes |
| Review | A list of problems and risks | No |
| Write a test | New test code | No, it adds code |
Use "diagnose" when you do not yet trust your own idea of the cause. If you ask for a fix straight away, the assistant may patch a symptom and hide the real problem. Ask for a diagnosis first, then ask for the fix in a second request. Chapter 12 returns to this step-by-step approach.
State the desired outcome, not only the problem
"Fix the crash" describes a problem. "Return None when the list is empty" describes an outcome. An outcome can be checked, and a problem description cannot.
Compare these two requests about the same file.
Vague:
Fix the error in stats.py.Specific:
Context: stats.py (attached), selection: the average function (lines 1-2).
Error: ZeroDivisionError: division by zero when average([]) is called.
Task: Change average so an empty list returns None instead of raising. Keep the function signature and do not change other code.The specific version names the file, the selection, and the error. It states the new behavior (None for an empty list). It lists two constraints. After the assistant answers, you can check the result against that behavior.
The assistant should produce something like this:
def average(values):
if not values:
return None
return sum(values) / len(values)
print(average([]))Running it prints None. You can compare this against your request line by line. The function signature is unchanged, and the only addition is the guard.
Ambiguity remains even in the good request. Should average([]) return None or 0? You decided that in the request, so the assistant did not have to choose. Whenever a decision belongs to you, put it in the task statement.
Add constraints
Constraints narrow the space of acceptable answers. Without them, assistants tend to do more than you asked: rename variables, add logging, or restructure nearby functions. Each extra change is more code for you to review.
Common constraints fall into a few groups:
- Scope. "Do not change other code." "Only edit the
averagefunction." - Interface. "Keep the function signature." "Do not rename public methods."
- Style. "Add type hints to new code." "Follow the existing naming."
- Dependencies. "Use only the standard library."
- Output form. "Show only the changed function." "Give a unified diff."
Keep constraints to those that matter. A list of twelve rules makes it hard to see which ones are important, and some may conflict. If a rule applies to every request in a project, move it out of the request. Chapter 9 covers standing instructions, such as a conventions file:
# Project conventions
- Language: Python 3
- Tests: pytest, files named test_*.py
- Keep functions small and add type hints to new codeRules in a file like this do not need repeating in each task statement. The task statement then holds only what is specific to this request.
Say how the context relates to the task
Supplied context has different roles, and the assistant cannot always tell which role each piece plays. Label them. Use short words such as "attached," "selection," and "error" as in the example above. Then add a sentence if the relationship is not obvious.
For example, when you attach two files, say which one to change:
Context: stats.py (attached) and test_stats.py (attached, read-only).
Task: Make the failing test in test_stats.py pass by changing stats.py only.Here the test file is a specification. Without the words "read-only," an assistant may edit the test to make it pass, which defeats the purpose of the test.
The same applies to errors. Say whether the error is the thing to fix or only a clue. "The error below is from the old version, shown for reference" prevents the assistant from chasing it.
Control the shape of the answer
You can also state what the answer should look like. This saves time reading and applying it.
- "Show only the changed lines" is better than a full file when the file is long.
- "Explain in three sentences" limits length.
- "List the options with one trade-off each, and do not write code yet" separates discussion from implementation.
- "Before changing anything, tell me what you plan to do" gives you a checkpoint.
The last form is useful for risky changes. You read the plan, correct it, and only then ask for the code. A wrong plan costs one sentence to fix. A wrong implementation costs a review.
One task per request
A request that asks for a fix, a refactor, a new feature, and tests mixes several jobs. The answer becomes long, and an error in one part is hard to separate from the rest. Split the work:
- Ask for the fix.
- Read and run the result.
- Ask for the test in a new request that builds on the first.
If the tasks truly depend on each other, number them in the request and ask the assistant to do them in order. Even then, keep the list short.
Sometimes you do not know the task. You have an error and no idea what to ask. In that case, make the task a question: "What are the possible causes of this error in this code, ranked by likelihood?" That is a valid task statement, and it prepares for a precise request next.
A request, step by step
The diagram shows how the parts of a request fit together before you send it.
Walk through it with the running example.
- Context:
stats.py, theaveragefunction, and theZeroDivisionErrortraceback. - Verb: Change (a fix).
- Target: the
averagefunction. - Outcome: an empty list returns
None. - Constraints: keep the signature, change nothing else.
- One task? Yes. Send it.
Common errors
These are the patterns that most often make a task statement fail.
- No task at all. You paste a traceback and nothing else. The assistant explains the error, and you wanted a fix.
- An outcome hidden in a story. Three paragraphs of history, with the request in the last clause. Put the task first or last, and set it apart on its own line.
- Contradictory instructions. "Keep it minimal" and "make it production ready" pull in opposite directions. Pick one.
- Unstated decisions. The request does not say what to return for an empty list, so the assistant picks
0and you wantedNone. - Unverifiable goals. "Make it better" has no end point. Name a property you can check, such as "no function longer than 20 lines."
- Asking for a fix when you need a diagnosis. The assistant patches the symptom. If you are unsure of the cause, ask for the cause first.
Checking your task statement
Before you send a request, read only the task line and ask three questions.
- If the assistant did exactly what this says, would I be satisfied?
- Is there a second reasonable reading that I would not want?
- Could I tell, from the answer alone, whether it succeeded?
If the answer to the first is no, add the missing outcome. If the answer to the second is yes, add a constraint. If the answer to the third is no, rewrite the task as a checkable behavior.
Exercise
A learner attaches stats.py and pastes the ZeroDivisionError traceback with the message "Can you help with this?" Rewrite the request as a task statement. It should ask for a fix so that average([]) returns None, and it must include an action, a target, an outcome, and two constraints.
Show answer
For example: "Task: Change the average function in stats.py so an empty list returns None instead of raising ZeroDivisionError. Keep the function signature and do not change any other code." The action is "change," the target is the average function, the outcome is returning None for an empty list, and the constraints are keeping the signature and leaving other code alone.
Exercise
You attach stats.py and test_stats.py. A test in test_stats.py fails, and you want the code fixed, not the test. What should the task statement say to prevent the assistant from editing the test?
Show answer
It should mark the test file as read-only and say which file may change, for example: "test_stats.py is read-only. Make the failing test pass by changing stats.py only." Afterward, check the diff to confirm the test file is untouched.
Exercise
You have a traceback but do not know why it happens. Should you ask for a fix or something else first, and why?
Show answer
Ask for a diagnosis first, for example "List the likely causes of this error in this code, ranked by likelihood." A fix request can patch a symptom and hide the real cause. Once you know the cause, you can send a precise fix request.