Sub topic 2
Give the assistant the context it needs
This chapter assumes you can read Python, have used a chat or terminal assistant, and can run small snippets locally. It covers the second step of the plan: deciding what to put in front of the assistant so its explanation fits your code and not code in general.
Why context changes the answer
An assistant only knows what you show it, plus what it learned in training. If you paste a bare snippet, it explains the snippet as if it came from a typical project. If the real behavior depends on a configuration value, a helper in another file, or a library version, the explanation may be fluent and wrong for your case.
Context is the information that lets the assistant narrow down which of several plausible readings is the right one. You supply it deliberately, and you keep it small.
What counts as useful context
Start from the question you defined in the previous chapter. Then ask what the answer depends on. The table lists the usual candidates.
| Kind of context | Example | When it matters |
|---|---|---|
| The code itself | The function, class, or block you are asking about | Always |
| Your situation | "I'm reading a data-processing utility in a project I just joined" | Always, in one sentence |
| Language and versions | Python 3.11, a specific library version | When behavior changed between versions |
| Callers and callees | The function that calls this one, or a helper it uses | When the snippet refers to names defined elsewhere |
| Inputs and outputs | A sample argument and the result you saw | When the behavior is surprising |
| Configuration | Environment variables, settings files (with secrets removed) | When branches depend on settings |
| Your gap | "I don't know the `@lru_cache` decorator" | Always, so the answer targets what you lack |
You rarely need all of these. Pick the ones the answer depends on.
A worked example
Suppose you meet this function in a utility module and the decorator is new to you.
from functools import lru_cache
@lru_cache(maxsize=None)
def fib(n):
return n if n < 2 else fib(n - 1) + fib(n - 2)A prompt with no context, such as "What does this do?", forces the assistant to guess your level and your concern. A prompt with context looks like this:
I'm reading a Python utility in a data-processing project and I don't know the @lru_cache decorator.
Explain what this function does and why the decorator is there. Walk through fib(5) step by step. Tell me which parts you are certain about and which depend on Python version or configuration.
<code>
from functools import lru_cache
@lru_cache(maxsize=None)
def fib(n):
return n if n < 2 else fib(n - 1) + fib(n - 2)
</code>
</parameter>Notice what this prompt does. It states the situation, names the exact gap, asks for a specific kind of explanation, and marks where the code starts and ends. The delimiters matter because they keep your instructions separate from the code, so comments or strings inside the code are less likely to be mistaken for requests.
Choose the right amount
There are two ways to get this wrong.
- Too little. The snippet calls
load_rows(cfg)and you did not include it. The assistant must guess whatload_rowsreturns, and it will often guess confidently. - Too much. You paste a whole 2,000-line module. The relevant lines get diluted, the answer drifts to unrelated parts, and you share more of the codebase than you needed to.
A workable rule is to start with the smallest unit that contains your question, then add the definitions it points to. If the assistant says it is assuming something about a missing piece, that is your signal to supply the piece.
Say what you already know and what you saw
Two short additions improve many prompts.
- What you already tried or understand. "I know decorators wrap functions, but not what this one does to the return value." This stops the assistant from explaining basics you have.
- Observed behavior. If the code surprised you, give the input and output. "
fib(30)returns instantly, but I expected it to be slow." That points the explanation at the real puzzle.
Context you should not include
Sharing context has a cost. Before pasting, check the code and any configuration for API keys, passwords, tokens, customer data, and internal hostnames. Replace them with placeholders such as API_KEY="<redacted>". The next chapter covers this in detail. For now, treat redaction as part of gathering context, not a step after it.
Where context fits in the larger flow
Check your understanding
Exercise
You paste a function that calls parse_record(line, schema) and ask the assistant what it does. The assistant explains it confidently, but you later find its description of the return value is wrong. Name the likely cause and what you should have included.
Show answer
The snippet used parse_record and schema, but you did not supply their definitions, so the assistant guessed what they do and return. You should have included the definition of parse_record (or at least its signature and a description of its return value) and the shape of schema, after removing any sensitive values.
Exercise
List three pieces of context you would add to a prompt about a function whose behavior differs between Python 3.8 and 3.12, and say why each helps.
Show answer
(1) The Python version you run, so the assistant explains the behavior for that version. (2) A sample input and the output you observed, so the explanation addresses the real behavior. (3) The definitions of any helpers or settings the function reads, so the assistant does not guess at them. Any three of these with a reason are acceptable.