Sub topic 2
Context limits and spending your budget
This chapter assumes you have used an AI chat or coding assistant, can run a short Python file, and know what a stack trace is. It covers one idea: the assistant can only read a limited amount of text per request, so you have to decide what goes in.
What the context window is
The context window is the maximum amount of text a model can consider at one time. It holds your messages, any files or selections you attach, pasted errors, standing instructions, and the assistant's own earlier replies. The model does not see anything outside it.
The window is measured in tokens, not characters or lines. A token is a chunk of text, often a short word or part of a word. Code usually uses more tokens per line than prose, because of symbols, indentation, and long identifiers. A rough rule for English is that one token is about four characters. For code, treat that as a loose guide only.
Different models have different limits, and tools may reserve part of the window for their own instructions or for the reply. The space you can use is therefore smaller than the advertised number.
What counts against the budget
Everything in the conversation competes for the same space.
| Item | Why it costs tokens |
|---|---|
| Your request | The task and constraints you write |
| Attached files | The full text of each file, even unrelated parts |
| Selections and pasted errors | Every line you include |
| Standing instructions | Sent again with each request |
| Earlier turns | Past questions and answers stay in the history |
| The reply | The answer is generated inside the same window |
Because the reply also uses the window, filling it with input leaves less room for a complete answer.
What goes wrong when you overspend
A full window causes three kinds of problems.
- Truncation. Some tools cut off older or longer content without telling you. The assistant then answers from a partial file and may invent the missing parts.
- Dilution. Even when everything fits, a large pile of text makes the important lines harder to find. The assistant may focus on the wrong function or repeat advice from an earlier turn.
- Cost and delay. Many services charge by token, and larger inputs usually take longer to process.
Spending the budget well
Think of each request as a small budget. Spend tokens on the text that the answer depends on, and leave out the rest.
Take the small 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 zeroThis file is tiny, so attaching all of it costs almost nothing. Now imagine stats.py is 2,000 lines long. Sending the whole file to fix a two-line function spends most of the budget on code that has no bearing on the bug. The better choice is to send the function, the error, and one sentence about the goal. The later chapters on files and selections cover how to choose between them. Here the point is only that size drives the decision.
A practical order for deciding:
- Write the task first, so you know what the answer needs.
- Add the smallest piece of code that contains the problem.
- Add the error text exactly as printed.
- Add more only if the first answer shows something is missing.
Signs you are near the limit
- The assistant forgets a constraint you stated earlier in the conversation.
- It contradicts its own previous answer.
- It refers to code that is not in your file.
- The tool shows a warning about context size or trimmed content.
When you see these, do not keep adding text. Start a fresh conversation, and carry over only a short summary of what you decided plus the current code that matters.
Exercise
A teammate attaches a 3,000-line file and a 200-line stack trace to ask why one 10-line function returns the wrong value. Name two problems this can cause, and say what to send instead.
Show answer
Problems: the important lines get diluted among unrelated code, and the input may be truncated or leave less room for the reply, so the answer may rest on partial information. Send the 10-line function, only the relevant part of the stack trace (the failing line, the call chain, and the exception type), and a short task statement that says what the function should return.