Sub topic 2
Give the assistant the context it needs
An assistant can only explain what it can see. When you paste a snippet and ask "what does this do?", it fills every gap with a guess. Sometimes the guess is right. Sometimes it is a confident explanation of code that is not yours. This chapter covers how to decide what to share so the explanation matches your actual situation.
This chapter assumes you can read code in at least one language, have used a chat or terminal assistant before, and can run small snippets locally. It builds on the previous chapter, where you wrote down a specific question. Here you use that question to choose what to include.
Why missing context produces wrong explanations
An assistant has two sources of knowledge: what it learned in training, and what is in your prompt. It cannot see your repository, your Python version, your dependencies, or your data unless you show them. When the prompt is silent on a detail, the assistant answers for the most common case.
Take the decorator from the running example.
from functools import lru_cache
@lru_cache(maxsize=None)
def fib(n):
return n if n < 2 else fib(n - 1) + fib(n - 2)On its own, this snippet supports a clear explanation: lru_cache stores results by argument, so each fib(n) is computed once. But in a real project, three unseen facts could change the answer:
- What do callers pass in? Cached arguments must be hashable, so a list argument would raise an error.
- Which Python version runs it?
maxsize=Nonebehaves the same across versions, but newer versions also offerfunctools.cacheas a shorthand. - Is the function called from several threads, or in a long-running process where an unbounded cache grows without limit?
The assistant cannot answer these from the snippet. If you leave them out, it will usually pick one reading and present it as fact.
The kinds of context that matter
Not all context is equally useful. Start from your question and ask which missing facts could change the answer.
| Kind of context | What it tells the assistant | Example |
|---|---|---|
| The code itself | The thing to explain | The decorated `fib` function |
| Definitions it depends on | What unfamiliar names mean | A helper such as `load_rows` defined in another file |
| Callers | What values actually flow in | The line that calls `fib(user_input)` |
| Environment | Language version, framework, key libraries | Python 3.8 vs 3.12, pandas 2.x |
| Project purpose | Why the code might exist | "A data-processing pipeline that reads CSV exports" |
| Your gap | What you already know and what you don't | "I know decorators, but not `lru_cache`" |
| Evidence | Errors, logs, or output you observed | A traceback or the output of a run |
The last two rows are easy to forget. Telling the assistant what you already know keeps it from explaining decorators from scratch. Sharing observed output gives it something to reason about instead of speculate on.
Show the callers, not only the function
A function's behavior often depends on how it is used. Here is a variant of the earlier example that shows why. It runs as written.
from functools import lru_cache
@lru_cache(maxsize=None)
def total(items):
return sum(items)
print(total((1, 2, 3)))
try:
print(total([1, 2, 3]))
except TypeError as error:
print("TypeError:", error)The first call prints 6. The second call prints TypeError: unhashable type: 'list', because lru_cache builds a dictionary key from the arguments and a list cannot be a dictionary key.
If you paste only total and its decorator, the assistant can describe caching correctly. It cannot tell you whether this function will fail in your project. If you also paste the line total(rows) and mention that rows comes from list(reader), it can point at the exact mismatch. The callers are context that decides whether the explanation is useful.
Share the minimum that answers the question
More context is not always better. A whole file, or ten files, has costs:
- It buries the part you care about. The assistant spreads its attention across everything, and the explanation drifts toward summarizing the file.
- It uses up the model's context window, the limit on how much text it can consider at once. Text beyond the limit is dropped or ignored, often without a warning.
- It raises the chance that you share something sensitive that was not needed. The next chapter covers that in detail.
A workable rule: include the target code, then add each item only if you can say how it would change the answer. This is the inclusion test.
Apply this to the fib example. The decorator line, the function body, and the caller all pass the test. The other 400 lines of the module do not, unless the question is about how fib fits into them.
Three ways to supply context
How you share depends on your tool.
- Paste the relevant lines. This works in any chat assistant. Paste the target code and the few related pieces, such as a helper definition or a caller. Keep each piece separate and labeled, so the assistant can tell them apart.
- Point to files. Terminal and editor assistants can read files in your project. You can name a file and a function instead of pasting. This saves effort, but check what the assistant actually opened. Ask it to name the files it read. If it answers without reading the caller, it is guessing about the caller.
- Describe it in words. Use this when a piece of context is too large or too sensitive to paste. "
rowsis a list of dictionaries with string keys, usually a few thousand items" is often enough, and it contains no real data.
Whichever method you use, label each piece. A prompt with clear sections is easier for the assistant to use correctly than one long block.
I'm reading a Python utility in a data-processing project. I know decorators
but not lru_cache.
Environment: Python 3.8, standard library only.
Target code:
<code>
from functools import lru_cache
@lru_cache(maxsize=None)
def total(items):
return sum(items)
</code>
How it is called (in report.py):
<code>
rows = list(reader)
result = total(rows)
</code>
Question: will this call work, and if not, why?This is not the full prompt structure. The chapter on writing a clear explanation prompt covers goals, output format, and certainty. The point here is the content: the target, a caller, the environment, and your gap.
State your environment explicitly
Version and platform differences are a common source of confident but wrong explanations. Assistants often describe the latest behavior or the most common library version. Examples of environment details worth stating:
- The language version, such as Python 3.8 or 3.12. Features like
functools.cache(added in 3.9) ormatchstatements (3.10) may not exist in your version. - Key framework or library versions, especially for fast-moving libraries.
- The runtime setting, such as a web server with several worker processes, a notebook, or a one-off script. Caching and global state behave differently in each.
- The operating system, if the code touches the file system, paths, or processes.
You can find versions with a command. For Python:
import sys
from importlib.metadata import version
print(sys.version)
print(version("pip"))Replace "pip" with the package you care about. This prints the exact versions, so you do not have to guess or remember them.
Tell the assistant what you already know
Your gap is context too. Compare two openings:
- "Explain this code."
- "I understand decorators and recursion, but I have not seen
lru_cache. Explain only what it adds."
The second one lets the assistant skip material you know and spend the answer on the missing piece. It also gives you a way to catch errors. When the explanation touches something you do know, you can check it against your own understanding.
Include evidence of what the code actually does
If you have run the code, share what you saw. Observed behavior is stronger than any description of intent. Run this snippet and note the output.
from functools import lru_cache
@lru_cache(maxsize=None)
def fib(n):
return n if n < 2 else fib(n - 1) + fib(n - 2)
print(fib(30))
print(fib.cache_info())It prints 832040 and then CacheInfo(hits=28, misses=31, maxsize=None, currsize=31). If you paste this output and ask "why 31 misses and 28 hits?", the assistant is working from facts it can check its explanation against. Without the output, it would have to predict the numbers, and predictions are where errors creep in.
Tracebacks work the same way. Paste the full error text, not a paraphrase. The line numbers and exception type are often the most informative part.
Common mistakes
- Pasting only the line you don't understand. A single line often depends on names defined elsewhere. Add the definitions of any unfamiliar names.
- Leaving out the version. The assistant answers for a version you may not use. State yours.
- Describing code instead of showing it. "A function that caches Fibonacci numbers" removes the details that matter. Paste the code.
- Pasting everything. If the answer drifts into a file summary, you included too much. Trim to what passes the inclusion test.
- Assuming the assistant read your files. In a tool that can open files, confirm which ones it used. An answer about a function it never opened is a guess.
- Pasting secrets and real data. Keys, tokens, and customer records do not belong in a prompt. Replace them with placeholders, or describe them. See the next chapter.
A short checklist
Before you ask, confirm that you have:
- Written down the specific question.
- Included the target code, exactly as it appears.
- Added the definitions and callers that could change the answer.
- Stated the language and library versions and the runtime setting.
- Said what you already understand.
- Included any output or error you observed.
- Removed or replaced anything sensitive.
Exercise
You paste only the total function with its @lru_cache(maxsize=None) decorator into an assistant and ask whether it will work in your project. The assistant says yes, and explains caching correctly. In your project, the only caller is total(list(reader)). What context did you leave out, what actually happens, and what should you add to the prompt?
Show answer
You left out the caller. Because lru_cache builds a dictionary key from the arguments, a list is unhashable, so total(list(reader)) raises TypeError: unhashable type: 'list'. The assistant's explanation of caching was correct, but it could not know what the caller passes. Add the calling line (result = total(rows)), a note that rows is a list, and the Python version. You can also run the snippet yourself and paste the traceback. A fix, such as passing tuple(rows), can then be discussed against the real call.
Exercise
A teammate pastes a 600-line module and asks, "How does the caching work?" The assistant returns a long summary of the whole module with two sentences about caching. Which principle was broken, and how would you rewrite the request?
Show answer
The request included far more context than the question needed, so the answer spread across the whole file. Apply the inclusion test: keep the decorated function, the line that calls it, and the Python version, and drop the rest. Rewrite it as: "Here is the decorated function and where it is called. Explain what the decorator does and what happens on repeated calls with the same argument," followed by those few lines.