Sub topic 10
Keeping Context Current and Uncluttered
This chapter assumes you have used an AI chat or coding assistant, can run Python files from a terminal, and know how to attach files or paste text into a request. It covers what happens to context over a long session. Context gets stale, it piles up, and it can mislead the assistant. You will learn how to spot those problems and fix them.
Why context decays
Earlier chapters treated context as something you build for one request. In real work you send many requests in a row, and the context from earlier turns stays in the conversation. Three things go wrong over time:
- Stale context. You attached
stats.py, then edited it. The assistant may still be reasoning from the old copy. - Cluttered context. You pasted three error messages, two files, and a long discussion of an approach you later dropped. Most of it no longer bears on the task.
- Conflicting context. An early message said "keep the signature unchanged." A later message asked for a new parameter. The assistant now has two instructions that cannot both hold.
The assistant does not know which parts you consider current. It sees everything in its window and tries to use all of it. The cost of stale or extra material is usually wrong answers, not an error message.
Stale context
Stale context is any piece of context that no longer matches reality. The most common case is a file that changed after you shared it.
Take the running example. You share this file and ask for a fix:
def average(values):
return sum(values) / len(values)
print(average([]))The assistant proposes a change, and you apply it by hand with one modification of your own:
def average(values):
if not values:
return None
return sum(values) / len(values)
print(average([]))Ten minutes later you ask, "Now add a median function next to average." If the assistant still holds the first version, its answer may rebuild average from the old text. It might even reintroduce the bug you fixed. The assistant is not careless. Its only copy of the file is out of date.
Signs that context is stale
- The assistant refers to code, line numbers, or function bodies that no longer exist.
- It reintroduces a bug you already fixed.
- It reports an error you resolved several turns ago as if it were still happening.
- A suggested patch does not apply because the surrounding lines differ.
How to refresh
- Re-attach the file after you change it. Many tools read a file when you attach it and do not track later edits, so check how yours behaves.
- Say so explicitly. Write "I changed
averageas follows; treat this version as current." - Replace, don't append. Send the new version and tell the assistant the old one is obsolete. Two versions side by side invite mixing them up.
- Re-run the failing command and share fresh output. An error message from before your fix describes a program that no longer exists.
Clutter
Clutter is context that is accurate but not needed for the current task. Chapter 2 covered the budget: the window is finite, and every token you spend on irrelevant material is a token not available for relevant material. Clutter has a second cost beyond space. It dilutes attention, because the assistant must work out which parts matter.
Common sources of clutter:
| Source | Example | Better option |
|---|---|---|
| Whole files when one function matters | Attaching a 900-line module for a two-line fix | Share the selection plus its signature and imports |
| Repeated error output | Pasting the same traceback after each failed attempt | Share the latest traceback only |
| Full logs | 400 lines of build output around one failure | Share the failing step and a few lines before it |
| Abandoned approaches | A long exchange about a design you rejected | Start a fresh conversation, or summarize the decision |
| Unrelated files | Test files and config when the question is about parsing | Attach only what the task touches |
Trimming a traceback
A full traceback is the right thing to share the first time. The one from the example is already short:
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 zeroLonger tracebacks from real projects contain many frames from library code. Keep the frames from your own code, the final exception line, and the line that triggered the call. Remove repeated frames from recursion, and say you did, for example "(37 identical frames omitted)". Do not cut the exception type or message, which are the most informative lines.
Trimming a file
You can trim without losing meaning. For a function-level task, share:
- the function you want changed,
- the signatures of functions it calls,
- the imports and constants it uses.
Leave out the rest, and mark the cut so the assistant does not assume the file is complete:
# stats.py (excerpt; other functions omitted)
from typing import Optional
def average(values: list[float]) -> Optional[float]:
if not values:
return None
return sum(values) / len(values)
# ... other functions omitted ...The note "excerpt" matters. Without it, the assistant may conclude that average is the only function in the file and make wrong assumptions about naming or structure.
Conflicting context
Instructions given at different times can contradict each other. Suppose the conversation contains these two messages:
- "Keep the function signature and do not change other code."
- "Also let the caller pass a default value to return for empty lists."
The second request requires a signature change, which the first forbids. The assistant has to guess, and either guess may disappoint you.
Avoid the problem with these habits:
- When a requirement changes, say so: "This replaces my earlier constraint about the signature."
- Restate the full, current task in one message instead of relying on a trail of corrections.
- If a conversation has accumulated several revisions, write a short summary of the current requirements and use it as the new starting point.
When to start fresh
Continuing a conversation keeps useful history, but history also carries stale and conflicting material. A new conversation gives you a clean window. Use this table to decide.
| Situation | Continue | Start fresh |
|---|---|---|
| Follow-up question about the same change | Yes | |
| Next step in the same task, files unchanged | Yes | |
| Several files changed since they were shared | Yes | |
| The approach changed completely | Yes | |
| Answers keep ignoring a constraint you stated early | Yes | |
| New task in a different part of the project | Yes |
Starting fresh costs you the work of re-supplying context. To make that cheap, keep a short handoff summary as you go. It states the goal, the current state of the code, what you tried, and what remains. Paste it at the top of the new conversation along with the current versions of the files.
Goal: make average() in stats.py return None for an empty list.
State: average now has the early return (shown below). The file runs without error.
Tried: a try/except around the division; rejected because it hides other errors.
Next: add a median() function with the same empty-list behavior.
Constraints: Python 3, keep functions small, add type hints to new code.This summary is short and carries decisions, not raw history. It also records why an option was rejected, which prevents the assistant from proposing it again.
Standing instructions drift too
Standing instructions, such as a conventions file, are context that you supply once and then stop looking at. They go stale like everything else. Here is the example from the plan:
# Project conventions
- Language: Python 3
- Tests: pytest, files named test_*.py
- Keep functions small and add type hints to new codeIf the project moves to a different test runner, or adds a rule about docstrings, and the file is not updated, the assistant keeps following the old rules. Review the file when you change tooling or style, and keep it short. A conventions file that grows to several pages becomes clutter in every single request.
A pre-send check
Before sending a request, especially the fifth or sixth in a row, run through this list:
- Is every attached file the current version?
- Is the error output from the most recent run?
- Does each piece of context relate to the task in this message?
- Do any earlier instructions conflict with the current one?
- Have I said what has changed since the last message?
Common mistakes
- Assuming the tool tracks edits. Some tools re-read attached files each turn and some do not. Test it once: change a comment in the file, ask the assistant to quote it, and see what it returns.
- Pasting every failed attempt. Each new traceback pushes useful context out of focus. Keep the latest one and summarize earlier attempts in a sentence.
- Cleaning up by deleting too much. Removing context to reduce clutter can remove the one definition the task needs. Cut by relevance to the task, not by length.
- Correcting without replacing. Saying "actually, ignore that" leaves the old text in the window. Where you can, state the replacement explicitly and in full.
Exercise
You attached stats.py and asked for a fix to average. You applied the fix, then asked the assistant to add median. Its reply rewrites average with the original ZeroDivisionError bug and also cites a traceback you already resolved. Name the problem, and list two steps you would take before asking again.
Show answer
The assistant is working from stale context: the file and traceback it holds are from before your fix. Two steps: re-attach the current stats.py and state that average now returns None for an empty list, and drop or label the old traceback as resolved (or start a fresh conversation with a handoff summary). Then restate the median task against the current file.
Exercise
Your conversation has a first message saying "do not change the function signature," and ten messages later you want to add a default parameter to average. What should you do so the assistant does not guess between the two instructions?
Show answer
State explicitly that the new request replaces the earlier constraint, for example "This replaces my earlier instruction about the signature: add a default parameter." Better still, restate the complete current task and constraints in one message, or write a short summary of current requirements and start a new conversation with it.