Sub topic 1
What Context Is and Why It Matters
An AI assistant answers from the information in front of it. It has no view of your screen, your project, or your last debugging session unless that information reaches it. This chapter defines context, shows how the same question gets different answers depending on the context supplied, and lists the mistakes that cause most poor answers. Later chapters cover each kind of context in detail.
Prerequisites
This chapter assumes you have:
- Basic programming experience in any language. The examples use Python, but you only need to read a short function and a traceback.
- Experience with an editor or a terminal, including opening files, selecting text, and running a script.
- Some prior use of an AI chat or coding assistant, so that sending a message and reading a reply is familiar.
You do not need to know how language models work internally. The working model below is deliberately simple.
What context is
Context is everything the assistant can use when it writes a reply. It has two sources:
- What you send. This is your message, any files or snippets you attach, text you paste, and earlier messages in the same conversation.
- What the assistant can read on its own. Depending on the tool, this might include open editor tabs, project files it can search, or the output of commands it runs. Some tools offer none of this, and then only the first source exists.
The assistant combines these into a single input and produces a reply. It does not keep a hidden picture of your project between conversations. If a fact is not in the context, the assistant either asks about it or guesses.
The guess is the risk. A model is trained to produce plausible text. When information is missing, it fills the gap with something that looks reasonable. For code, that can mean an invented function name, an assumed library version, or a fix for a different bug than yours. The reply reads confidently either way, so a guess is hard to spot unless you check.
The kinds of context in this tutorial
This tutorial focuses on three kinds of context you control, plus the instruction that ties them together.
| Kind | What it is | Typical use |
|---|---|---|
| File | The full contents of one or more files | The assistant needs surrounding code, imports, or structure |
| Selection | A specific range of lines you highlight | The question concerns one function or block |
| Error | An error message or stack trace, with the command that produced it | Diagnosing a failure |
| Task statement | One or two sentences saying what to do | Every request, to say what the other items are for |
Context without a task leaves the assistant to decide what you want. A task without context leaves it to decide what you are talking about. Good requests supply both.
A worked example
Take a small file named stats.py:
def average(values):
return sum(values) / len(values)
print(average([]))Running python stats.py fails with this traceback:
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 zeroHere are three ways to ask for help with it.
Request A: no context
My average function crashes. Can you fix it?The assistant cannot see the function. It does not know the language, the input, or the error. It will probably reply with a question, or with a generic answer such as "check for division by zero" and an invented function. Neither is wrong as an answer to the words you typed. The problem is that the words carry almost nothing.
Request B: context but no task
Here is stats.py and the traceback.If you attach the file and paste the traceback, the assistant can see the bug. It still has to decide what you want. It might explain the error, raise a clearer exception, return zero, or return None. Each is a reasonable reading. You will get whichever one it picks, and you may need another round to correct it.
Request C: context and task
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.This request names the file, points to the relevant lines, quotes the error, and states the outcome and its limits. The assistant can produce a small, targeted change:
def average(values):
if not values:
return None
return sum(values) / len(values)
print(average([]))Request C is only a few lines longer than Request A. The extra lines remove the guesses. Note also that the constraints ("keep the signature", "do not change other code") limit the answer as well as describing the problem. Constraints are context too.
Why answer quality tracks context
A working model helps here. Think of the assistant as a very capable colleague who has just walked into the room with no memory of anything before this moment. They have strong general knowledge of programming. They know nothing specific about your code, your goals, or your conventions unless you tell them.
Three consequences follow from that model.
- Missing facts become guesses. The assistant cannot flag what it does not know that it does not know. A confident answer does not mean it had the facts.
- Present facts get used, including wrong ones. If you paste an outdated version of a function, the assistant will reason about that version. It has no way to know it is stale.
- Specific context beats general knowledge. When your file says one thing and the assistant's training says another, a well-built request makes the file win. A vague request lets the general pattern win, and the result may not fit your project.
So the quality of an answer is limited by the quality of the context, and you are the one who controls most of it.
The loop at the bottom is the main habit this tutorial teaches. When an answer misses, you rarely need to rephrase the same request. You usually need to change what the assistant has to work with.
Context is more than code
Beginners often treat context as "the code I paste". In practice several other items change the answer as much as code does:
- The environment. Language version, operating system, and library versions affect which fixes are valid. A fix for Python 3.12 may not work on 3.8.
- The goal behind the request. "Return
Nonefor empty input" and "make this crash loudly on empty input" produce opposite changes to the same line. - What you already tried. Telling the assistant that you tried a fix and what happened keeps it from suggesting that fix again.
- Constraints. Do not change the signature, do not add dependencies, keep the style consistent. These shape the answer.
- Conventions. Standing facts about the project, such as the test framework or naming rules. The
conventionschapter covers these, and a short sample looks like this:
# Project conventions
- Language: Python 3
- Tests: pytest, files named test_*.py
- Keep functions small and add type hints to new codeSupplying this once, instead of in every request, saves effort and keeps answers consistent.
Common errors
These mistakes cause most weak answers. Each is a failure of context, not of the assistant's ability.
- Describing instead of showing. "The function breaks on empty lists" is a summary written by someone who already knows the cause. The assistant needs the code and the actual error text. Your summary may also be wrong, and then the assistant works from a wrong premise.
- Sending too little. A single line pulled out of a function hides the variables, types, and callers that explain it. If the assistant must guess how
valuesis built, it will. - Sending too much. Pasting an entire project buries the relevant lines. Long contexts also have limits, which the next chapter covers. More is not safer. Relevant is safer.
- Truncating the error. Sharing only the last line,
ZeroDivisionError: division by zero, drops the line number and the call chain. The assistant then cannot tell which call failed. - Leaving out the task. The assistant fills the gap with its own idea of what you want.
- Sending stale context. An old version of a file, or an error from before your last edit, leads to an answer for a problem you no longer have.
- Sending secrets by accident. Files and logs can contain API keys, passwords, or personal data. Anything in the context may leave your machine, so check before you send.
Checking the answer is still your job
Good context raises the chance of a correct answer. It does not guarantee one. After the assistant replies, run the code, read the change, and compare it against what you asked for. In the example, you would run python stats.py and confirm that the output is None and that nothing else changed.
When the answer is wrong or vague, ask what the assistant lacked. Was the file missing? Was the error cut short? Was the task ambiguous? Fix that one gap and ask again. This keeps each round small and makes it easy to see which change helped.
What comes next
The next chapter looks at limits on how much context an assistant can take in at once, and how to spend that budget. After that, the tutorial covers deciding what is relevant, then files, selections, and errors one at a time, followed by task statements, combining them, conventions, keeping context current, keeping sensitive data out, and refining after a miss.
Exercise
A teammate sends this request: "The report script is broken, please fix it." They attach nothing. Name three pieces of context the assistant needs, and say what it is likely to do without them.
Show answer
Any three of: the script's code (a file or the relevant selection), the exact error message or traceback with its line numbers, the command that was run and its input, the environment such as language and library versions, and the intended behavior (the task). Without them the assistant has to guess. It may ask a clarifying question, or it may invent a plausible but unrelated fix, such as changing code it imagines exists.
Exercise
In the worked example, Request B attached the file and the traceback but gave no task. Why can the assistant still return an unwanted change, and what single sentence would prevent it?
Show answer
The context shows what failed but not what outcome you want. The assistant could raise a clearer exception, return 0, or return None, and all fit the evidence. A sentence such as "Change average so an empty list returns None instead of raising" states the outcome and removes the ambiguity. Adding "Keep the function signature and do not change other code" also limits the scope.
Exercise
You paste an error and the assistant proposes a fix for a line that no longer exists in your file. Using the model from this chapter, what is the most likely cause, and what do you change?
Show answer
The context is probably stale. You may have pasted an older version of the file, or the error came from before your last edit. The assistant reasons faithfully about whatever it was given. Re-run the script, copy the current error and the current code, and send those.