Sub topic 8
Know where assistants go wrong
This chapter assumes you can read Python, have used a chat or editor assistant, and can run small snippets locally. It covers the failure modes of assistants that explain code. You will learn to recognize them and to turn a suspicious claim into a check you can run.
An assistant produces fluent text that is statistically likely given your prompt. It does not execute your code, look up the real documentation, or know what it has not seen. Most explanation errors follow from those three facts.
Why a wrong explanation looks right
A wrong explanation usually sounds the same as a correct one. The tone is steady, the structure is tidy, and the vocabulary is accurate. Confidence in the wording tells you nothing about correctness.
Mistakes also tend to be plausible. If an assistant misreads your code, it misreads it in the direction of what code like this usually does. That is why errors are hard to catch by skimming. They fit your expectations.
The common failure modes
The table lists the failures you will meet most often when asking about unfamiliar code. Each one is paired with the signal that should make you suspicious.
| Failure mode | What it looks like | Warning sign |
|---|---|---|
| Invented API or behavior | Names a parameter, method, or flag that does not exist | A specific name you cannot find in the docs |
| Version confusion | Describes behavior from an older or newer release | No mention of which version the claim applies to |
| Simulated execution errors | Traces a loop or recursion and gets a value wrong | Long hand traces with many intermediate numbers |
| Guessing about unseen code | Explains a helper function it was never shown | Confident statements about names defined in other files |
| Invented rationale | Gives a reason the author wrote it this way | "The author probably did this to..." stated as fact |
| Intent over behavior | Describes what the code is meant to do, not what it does | The explanation matches the function name more than the body |
| Agreeing with your guess | Confirms a wrong assumption you put in the question | Your own wording reappears in the answer |
| Skipped details | Summarizes and quietly omits an edge case | A neat summary of a messy function |
Invented APIs and details
When an assistant does not know a detail, it tends to fill the gap with something that fits the pattern. You may see a keyword argument that sounds right, a method on the wrong class, or a default value that is off by one. The name looks reasonable because it was generated to look reasonable.
Treat any specific identifier, number, or default in an explanation as a claim to check. Library names and function signatures can be confirmed in under a minute in the official documentation or with help() in a Python shell.
Version and configuration confusion
Languages and libraries change. The functools.cache decorator exists only in Python 3.9 and later, and it is the same as lru_cache(maxsize=None). Using @lru_cache without parentheses works only in Python 3.8 and later. An assistant may describe either form without saying which version it assumes.
This is why the prompt in the spine asks the assistant to separate what it is sure about from what depends on version or configuration. That request does not make the answer correct, but it makes weak spots visible. You still need to check them.
Errors when tracing execution
Asking an assistant to walk through fib(5) is useful for learning the shape of the computation. It is a poor way to learn exact values. The assistant is not running anything. It is predicting what a trace looks like.
Short traces are usually right. Long ones drift. Be most careful with loops over many iterations, floating-point arithmetic, string formatting, off-by-one boundaries, and anything involving state that changes across calls. For those, run the code and print the values.
Guessing about code it has not seen
If you paste one function that calls normalize_record(), the assistant has not seen normalize_record(). A good answer says so. A weaker one describes what the helper "presumably" does, and later sentences build on that guess as if it were established.
Look for the moment the explanation relies on something you did not paste. If a conclusion depends on that code, the conclusion is unverified. Either paste the missing piece or ask the assistant to list the assumptions it made.
Rationale that was invented
"Why is this here?" is one of the most useful questions, and one of the riskiest. The assistant cannot see commit history, tickets, or conversations. It can describe what the code accomplishes and what reasons are common for such code. It cannot know why this author made this choice.
A trustworthy answer separates the two: "This decorator has the effect of caching results. Typical reasons are..." An untrustworthy answer says "The author added this to avoid repeated computation." Check the first by reading the code. Check the second by reading git log, git blame, or the pull request that introduced the line.
Describing intent instead of behavior
Names and comments pull assistants toward intent. A function called safe_divide will often be described as dividing safely, even if the body only guards one of two failure cases. Comments that are out of date have the same effect.
If a summary reads like a paraphrase of the function name, ask a narrower question: "What does this return when the denominator is zero? When it is a string?" Then confirm the answer by calling the function.
Agreeing with you
If your question contains an assumption, such as "I think this caches on disk, right?", the assistant may agree. Models are tuned to be cooperative. A leading question increases the chance of a wrong answer, and the answer will sound like confirmation.
Ask neutral questions instead: "Where does this cache store its data?" If you do want to test a belief, ask for the strongest evidence against it.
A worked example: claims that fail when you run them
Here is the running example from this tutorial. Suppose the assistant explained it like this: "lru_cache makes fib fast for any input, and you can pass any value to it." Both parts of that sentence are partly wrong, and you can show it in a few lines.
import sys
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("recursion limit:", sys.getrecursionlimit())
# Claim 1: "fast for any input"
try:
fib(5000)
except RecursionError as exc:
print("RecursionError:", exc)
# Claim 2: "you can pass any value"
try:
fib([3])
except TypeError as exc:
print("TypeError:", exc)On a default CPython build, the recursion limit is 1000, so fib(5000) raises RecursionError. The exact message text varies between versions. The list argument raises TypeError: unhashable type: 'list', because the cache uses the arguments as dictionary keys.
The decorator removes repeated work, but it does not remove the call-stack depth of the recursion. Arguments must be hashable. Neither limit was in the original claim. Neither would have been noticed by reading the explanation alone, but running the snippet shows both quickly.
You can also check the cache behavior directly, as in the earlier example from this tutorial.
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())For fib(30) the output ends with CacheInfo(hits=28, misses=31, maxsize=None, currsize=31). Thirty-one misses is one per value from 0 to 30. The 28 hits are the second recursive call in each of fib(3) through fib(30). If an assistant told you a different count, you now have a way to find out.
Gaps that come from the tool, not the model
Some errors are not about the model's reasoning. They come from what the tool could see.
- Truncated input. Long files may be cut off without a clear warning. If the explanation never mentions the end of the file, ask what the last function does.
- Stale knowledge. The assistant's training data has a cutoff. New library releases and recent breaking changes may be missing or wrong.
- Missing environment. The assistant does not know your Python version, installed packages, feature flags, or environment variables. Behavior that depends on them is a guess unless you supply them.
- Concurrency behavior. Documentation for
lru_cachesays the cache is thread-safe, but the wrapped function can still be called more than once for the same arguments if threads race. A short summary such as "thread-safe" hides that distinction.
Turn suspicion into a check
You cannot verify everything, and you do not need to. Verify the claims that your next step depends on, and the claims that sound most specific.
The diagram shows a simple way to sort a claim before you act on it.
A few habits make this routine:
- Underline every concrete name, number, and version in the answer as you read it.
- Mark any sentence that begins with "probably", "likely", "typically", or "presumably". Those are the assistant telling you it is guessing.
- Ask the assistant to list its assumptions and which parts of the code it did not see.
- Pick the one or two claims your understanding rests on and check those first.
Prompts that expose weak spots
You can reduce these failures with the way you ask. None of these prompts guarantee accuracy, but they make errors easier to notice.
Explain what this function does. Quote the line that supports each claim.
List anything you assumed because it was not in the code I pasted.
Separate what the code does from what its name or comments suggest.
If a detail depends on the Python version, say which versions differ.Be careful with a related habit. When you ask "Are you sure?", the assistant often changes a correct answer to a different one. Repeating a question is not verification. Running the code or reading the documentation is.
What to do when you find an error
When a check contradicts the explanation, do three things.
- Correct your own notes first, so the wrong claim does not survive in your understanding.
- Give the assistant the evidence: paste the actual output or the relevant documentation. Ask it to revise its explanation in light of that.
- Re-check the parts of the explanation that depended on the wrong claim. An error early in a walkthrough often carries through to later steps.
If the assistant keeps repeating the same incorrect claim, start a new conversation and include only the code and the confirmed facts. A long thread containing the mistake can bias later answers.
Exercise
An assistant explains the fib function and says: "The @lru_cache decorator is thread-safe, so fib will never run twice with the same argument, even with many threads." Name the part of the claim that is likely wrong or too strong, and describe one way to check it.
Show answer
The cache data structure is thread-safe, but that does not guarantee the wrapped function runs only once per argument. The Python documentation states that if another thread makes the same call before the first one finishes and is cached, the function can be called more than once. To check, read the functools.lru_cache documentation, or write a small test that starts several threads calling a slow, counting version of the function with the same argument and compares the count to 1.
Exercise
You paste one function that calls load_config() and ask what it does. The answer says "it reads config.yaml from the current directory and raises an error if the file is missing." You did not paste load_config(). Which failure mode is this, and what should you do?
Show answer
This is guessing about code the assistant has not seen. The claim about config.yaml and the error is unverified. Either paste load_config() and ask again, or open its source yourself and check the behavior. You can also ask the assistant to list the assumptions it made about code that was not provided.
Exercise
Write a short Python check that shows fib(30) with @lru_cache(maxsize=None) produces 31 cache misses and 28 hits, and say what each number means.
Show answer
Run print(fib(30)) followed by print(fib.cache_info()). The result shows hits=28, misses=31. The 31 misses are one for each distinct argument from 0 through 30, since each is computed once. The 28 hits come from the second recursive call, fib(n - 2), in each call for n from 3 through 30, because that value is already cached by then.