Sub topic 10
Keeping Context Current and Uncluttered
This chapter assumes you can run Python files and read a traceback. It also assumes you have used an AI chat or coding assistant for a few turns in a row. Earlier chapters covered what to share. This one covers what to remove, replace, and refresh as a session goes on.
Why context goes bad
The assistant works from everything in its context window. That includes your earlier messages, its own replies, attached files, and pasted errors. Nothing in that pile is marked "old" or "no longer true." If a file changed after you attached it, the assistant still sees the old version. If you pasted three different errors, it may mix them up.
Two problems follow:
- Stale context is information that was true once and is now wrong, such as an old version of a file or an error you already fixed.
- Clutter is information that is true but irrelevant to the current task, such as a second file nobody is editing.
Stale context causes wrong answers. Clutter causes vague answers and uses up your budget.
A worked example
Take the file used throughout this tutorial:
def average(values):
return sum(values) / len(values)
print(average([]))Running it produces this error:
Traceback (most recent call last):
File "stats.py", line 4, in <module>
print(average([]))
File "stats.py", line 2, in average
return sum(values) / len(values)
ZeroDivisionError: division by zeroSuppose you ask the assistant to fix it, and it suggests returning None for an empty list. You apply the change, and the file now looks like this:
def average(values):
if not values:
return None
return sum(values) / len(values)
print(average([]))Your next request is: "Now add a test for the empty case." If the old version of stats.py and the old traceback are still the most prominent items in the conversation, the assistant may write a test that expects a ZeroDivisionError. That test matches the context, but it does not match your code.
Refreshing what changed
When a file changes, send the new version instead of describing the change. A description such as "I added an empty check" leaves room for error. The file itself does not.
A short request that replaces stale context:
stats.py has changed. Ignore the earlier version and the earlier traceback.
Current file (attached): average now returns None for an empty list.
Task: Add a pytest test in test_stats.py for average([]) returning None.Three habits keep context current:
- Re-attach a file after you edit it, especially if the assistant will edit it again.
- State which earlier material is obsolete. Say "ignore the earlier traceback" instead of hoping it is understood.
- Replace an error once it is fixed. If a new error appears, paste only the new one.
Removing clutter
Before you send a request, check each item in the context against the current task. Ask whether the assistant needs it to do this step.
| Item | Keep? | Reason |
|---|---|---|
| The current `stats.py` | Yes | The task edits it |
| The fixed `ZeroDivisionError` traceback | No | The bug no longer exists |
| An unrelated `config.py` | No | Nothing in the task touches it |
| The convention "use pytest" | Yes | It shapes the new test |
If the answer to "does this help the current task?" is no, leave it out. This does not mean stripping everything. A short, relevant context beats both a long one and an empty one.
When to start over
Long sessions accumulate dead ends. If you have gone through several wrong attempts, the conversation contains many rejected ideas, and the assistant may drift back to them. A fresh session is often cleaner than a long correction chain.
Start a new session when:
- The task changes to a different part of the code.
- You have corrected the assistant several times on the same point.
- The conversation has grown long enough that early details may be pushed out.
When you restart, carry over a short summary. Include the current file, the goal, and any decisions that still hold:
Context: stats.py (attached, current version).
Decision so far: average returns None for an empty list.
Task: Add a pytest test for the empty-list case. Use test_stats.py.This keeps the facts that matter and drops the rest.
A flow for each request
Exercise
You fixed a bug, edited stats.py, and now want the assistant to add type hints. The conversation still holds the old file and the old traceback. Name two things to do before sending the request.
Show answer
Re-attach the current stats.py so the assistant sees the edited version, and either remove the old traceback or state that it is obsolete because the bug is fixed. Then state the task: add type hints to average without changing its behavior.
Exercise
A session has gone through four wrong attempts at the same fix. What is a good next step, and what should you bring with you?
Show answer
Start a new session. Bring the current file, the goal, and any decisions that still hold, in a short summary. Leave behind the rejected attempts.