Sub topic 4
Write a Clear Explanation Prompt
This chapter assumes you can read code in at least one language, have used a chat or terminal assistant, and can run small Python snippets locally. You already know how to state a specific question about unfamiliar code and how to pick the context to share. Here you combine those into one prompt.
Why structure matters
An assistant answers the question you wrote, not the question you meant. A vague prompt such as "explain this code" gets a generic tour of every line. The assistant has to guess your background, what you already know, and what you plan to do with the answer. A structured prompt removes most of that guessing.
A good explanation prompt has five parts:
- Situation. Where the code lives and what you are doing with it.
- Gap. The exact thing you do not understand.
- Request. The kind of explanation you want.
- Honesty clause. A request to separate certain claims from conditional ones.
- Code. The code itself, in a clearly marked block.
The running example
The code used in this chapter is a small function that looks odd if you have not met the lru_cache decorator. A decorator is a function that wraps another function to change its behavior. The @ line above def applies one.
from functools import lru_cache
@lru_cache(maxsize=None)
def fib(n):
return n if n < 2 else fib(n - 1) + fib(n - 2)Suppose you found this in a data-processing project. You can read the recursion, but the first two lines of the definition are new to you.
A weak prompt and what it produces
explain this
def fib(n): ...This prompt has problems:
- It does not say which language or project the code comes from.
- It does not say what you already know. The assistant may spend most of its answer explaining recursion, which you understand.
- It does not ask for a format. You might get a one-line summary or a long essay.
- It leaves out the decorator, which is the part you care about, if you paste carelessly.
The answer will probably be correct but badly aimed. You then spend several turns steering it. A better first prompt saves those turns.
A strong prompt, part by part
Here is the prompt from the topic plan:
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>Situation
"I'm reading a Python utility in a data-processing project" tells the assistant the language and the domain. It also tells the assistant you are reading, not writing. That changes the answer. A reader needs to understand intent and behavior. A writer might need alternatives and improvements.
Keep this part short. One sentence is usually enough. Add more only when the project context changes the meaning of the code, for example when the function is called from a web handler that runs many times per second.
Gap
"I don't know the @lru_cache decorator" names the exact missing piece. This is the most useful sentence in the prompt. It tells the assistant what to skip (recursion, Fibonacci numbers) and what to dwell on (caching behavior).
If you cannot name the gap, say so and describe what confuses you: "I don't understand why this function is fast when it looks like it does exponential work." That is still specific.
Request
The request names the output you want. The example asks for three things:
- What the function does.
- Why the decorator is there. This is a rationale request, and it asks about intent, not mechanics.
- A step-by-step trace of
fib(5). This is a walkthrough request, with a concrete input so the assistant cannot stay abstract.
Common request types and the wording that triggers them:
| You want | Ask for |
|---|---|
| A quick orientation | "Summarize what this does in two or three sentences." |
| The mechanism | "Walk through this with input X, step by step." |
| The reason | "Why would the author have written it this way instead of Y?" |
| Edge cases | "What inputs would make this fail or behave surprisingly?" |
Asking for all four at once produces a long, unfocused answer. Choose the one or two that match your gap.
Honesty clause
"Tell me which parts you are certain about and which depend on Python version or configuration" asks the assistant to mark its own uncertainty. This does not make the assistant more accurate, and it cannot see its own errors. It does make the answer easier to check. Claims labeled "depends on version" tell you where to look in the documentation. A later chapter covers how assistants go wrong, so treat this clause as a way to get a list of things to verify, not as a guarantee.
Useful variations:
- "Mark anything you are inferring rather than reading directly from the code."
- "If you would need to see another file to be sure, say which one."
- "Say so if you are not familiar with this library."
The second variation matters for code that calls functions you did not paste. Without it, assistants often guess what those functions do and present the guess as fact.
Code block
Put the code inside clear delimiters. The example uses <code> tags. Triple backticks work too. The purpose is to separate your words from the code. Otherwise a comment in the code such as # ignore previous text could be read as part of your instructions, or your question could be read as part of the code.
Also keep these points in mind:
- Paste the exact code, including imports and decorators. Trimming the
@lru_cacheline from the example would remove the thing you asked about. - Include line numbers or file names if you paste more than one snippet, so the answer can refer to them.
- Paste only what is needed. Remove secrets before pasting. The previous chapter covers this.
Order matters less than separation
The order situation, gap, request, honesty clause, code is a convention, not a rule. Some people put the code first and the question after, because assistants often weigh the end of a long prompt heavily. What matters is that each part is distinct and that the question appears in plain words, not buried in a comment or a paragraph of background.
For a long snippet, put the question both before and after the code. A one-line reminder after the block is cheap:
Reminder: I only need to understand the decorator and why it is used here.Check the claim by running it
The assistant's explanation will probably say that lru_cache stores results so repeated calls with the same argument are not recomputed. That is a claim you can test. Run this:
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())Expected output:
832040
CacheInfo(hits=28, misses=31, maxsize=None, currsize=31)Compare this with what the assistant said. Each of the 31 values from fib(0) to fib(30) is computed once (31 misses). The other 28 calls found the answer already stored (28 hits). If the assistant claimed the function recomputes values, or gave different counts, you have found an error. A later chapter on verification covers this in detail. The point here is that a well-formed prompt asks for claims you can test.
If the trace the assistant gave for fib(5) does not match this behavior, ask a follow-up that points at the mismatch instead of starting over.
Common prompt mistakes
| Mistake | Effect | Fix |
|---|---|---|
| Pasting code with no question | Generic line-by-line summary | State the gap and the request |
| Asking "explain everything" | Long answer that buries the part you need | Name one or two specific questions |
| Paraphrasing the code instead of pasting it | The assistant explains your paraphrase, which may be wrong | Paste the exact code |
| Omitting the language or framework | Wrong assumptions about syntax and libraries | Say it in the situation sentence |
| Omitting your version | Answers that describe behavior from a different release | State the version when behavior might differ |
| Stacking five questions | Shallow answers to each | Ask one question, then follow up |
| Mixing instructions into the code block | Confusion about what is code and what is request | Use delimiters and keep prose outside |
Adapting the template
The template works for other languages and other kinds of unfamiliar code. Here is the same structure applied to a shell one-liner:
I'm maintaining a deployment script in a Linux CI pipeline and I don't understand the "${var:-default}" syntax.
Explain what this line does, and show what it produces when DEPLOY_ENV is unset, empty, and set to "prod". Tell me which behavior is specific to bash and which works in any POSIX shell.
<code>
target="${DEPLOY_ENV:-staging}"
</code>The same five parts are there. The request asks for three specific inputs, which makes the answer concrete and checkable. The honesty clause is tuned to the likely trap, portability between shells.
You can verify that answer in about thirty seconds by running the line in a shell with each of the three cases.
A reusable skeleton
Situation: <language, project type, whether I'm reading or modifying>
Gap: <the exact thing I don't understand>
Request: <summary | walkthrough with input X | rationale | edge cases>
Certainty: <mark what you're sure of vs. what depends on version or config>
<code>
<exact code, secrets removed>
</code>Fill it in before sending. If you cannot fill in the Gap line, return to the first step of the process and define your question. The prompt cannot be sharper than your question.
Summary of the chapter's method
- Name the situation in one sentence.
- Name the gap in one sentence.
- Ask for one or two specific kinds of explanation, with a concrete input where possible.
- Ask the assistant to separate certain claims from conditional ones.
- Paste the exact code in a delimited block.
- Run something to check the answer.
Exercise
You find this line in a Python script: result = [x for x in data if x] or None. You understand list comprehensions but not the or None at the end. Write a prompt using the five parts (situation, gap, request, honesty clause, code). Make the request ask for a trace with a concrete input, and name two inputs that would test the behavior.
Show answer
A good answer looks like this. Situation: "I'm reading a Python data-cleaning script and I'm only reading it, not changing it." Gap: "I understand the list comprehension, but I don't understand what or None does after it." Request: "Explain what the whole line does, then trace it with data = [0, 1, '', 'a'] and with data = [0, '', None]." Honesty clause: "Say which parts are guaranteed by the Python language and which depend on the contents of data." Code: the line inside a delimited block. The two inputs matter because the first gives a non-empty list, and the second gives an empty list, which is falsy, so the expression evaluates to None. Running both inputs lets you check the assistant's trace.
Exercise
A colleague's prompt reads: "why is this slow?" followed by 300 lines of pasted code. Name three problems with it as an explanation prompt, and rewrite the first sentence so it is better.
Show answer
Problems: there is no situation (language, project, runtime), no named gap (which part is slow or unclear), and no request type or honesty clause. Pasting 300 lines also buries the relevant section, and "slow" is not tied to a measurement. A better first sentence: "I'm reading a Python 3.11 service that processes CSV uploads, and I don't understand why the loop in process_rows() takes longer as the file grows; here is that function and the helper it calls." The rewrite narrows the code, states the language and version, and names the confusing behavior.