Sub topic 5
Ask for the right level and format
This chapter assumes you can read code in at least one language, have used a chat or editor assistant, and can run small Python snippets locally. It covers how to control the depth and shape of an explanation after you have a clear question and a structured prompt.
Why level and format matter
An assistant picks a level and a format on its own if you don't. You may get a beginner lecture on decorators when you needed one sentence on caching. You may get a dense paragraph when a numbered trace would have been easier to follow. Naming the level and the format costs one line in the prompt and saves a round of follow-ups.
Use the running example from earlier chapters:
from functools import lru_cache
@lru_cache(maxsize=None)
def fib(n):
return n if n < 2 else fib(n - 1) + fib(n - 2)Choose a level
State what you already know and what you don't. The assistant can then skip the first and spend its words on the second.
| Level you ask for | Example phrasing | Good for |
|---|---|---|
| One-line summary | "Summarize what this function does in one sentence." | Triage: deciding whether the code matters to you |
| Overview | "Explain the purpose of this function and why the decorator is there." | Reading a file for the first time |
| Step-by-step | "Walk through fib(5) call by call." | Understanding control flow or state changes |
| Rationale | "Why would someone cache here instead of using a loop?" | Judging design choices |
| Internals | "How does lru_cache store results and decide what to evict?" | Debugging or tuning behavior |
Then anchor the level to your background:
- "I know Python functions and recursion, but I have not used decorators."
- "I am experienced in Java. Compare this to something I would know."
- "Assume I have never seen memoization."
Telling the assistant what you already know matters more than saying "explain simply". "Simply" is vague. A stated background is specific.
Choose a format
Pick the format that matches the question.
- Numbered steps for a trace of execution order.
- A table for comparing parameters, cases, or alternatives.
- Annotated code when you want comments next to the lines they explain.
- A short summary followed by detail when you want to stop reading early if the summary is enough.
- A diagram description for call flow, which you can sketch yourself.
A prompt that combines level and format looks like this:
I know Python and recursion but have not used functools.
Explain this function in two parts. First, one sentence on what it does.
Second, a numbered trace of fib(4) showing which calls compute a result
and which calls return a cached one. Mark anything that depends on the
Python version.
<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>The two-part request gives you an early exit and a detailed trace in one reply.
Adjust after the first answer
Level and format are easy to change mid-conversation. Short instructions work well:
- "That was too general. Show the trace for fib(5) only."
- "Put the answer in a table with columns call, cache hit or miss, result."
- "Repeat, but assume I know what memoization is."
- "Shorten that to three bullets."
Changing the request is cheaper than rereading a poorly fitted answer.
Common errors
- Asking for "a detailed explanation" of everything. Long answers bury the part you needed. Name the part.
- Not stating your background. The assistant guesses, and the guess is often too basic or too advanced.
- Accepting a pleasing format as proof of accuracy. A clean table can still contain a wrong claim. A format changes how an answer looks, not whether it is correct. You still need to check it, as later chapters show.
- Asking for analogies by default. Analogies can drop important detail. Ask for one only when the plain explanation did not land, and then check where the analogy breaks.
Check the shape against the code
A numbered trace makes concrete claims, so you can test them. For the example above, the assistant should say that with caching each value from 0 to 30 is computed once. You can confirm that:
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())The output of cache_info() for fib(30) is 31 misses and 28 hits. If a trace claims a different count, that is a point to question.
Exercise
You are reading a function that uses an unfamiliar decorator and you only need to decide whether it affects the bug you are chasing. Which level and format should you request first, and why?
Show answer
Ask for a one-line summary, optionally followed by a short overview. The goal is triage. A full step-by-step trace or an internals explanation costs more reading, and you can request it afterwards if the summary shows the code is relevant.
Exercise
Rewrite this prompt so it states a level and a format: "Explain this code."
Show answer
For example: "I know Python functions but not decorators. Explain what this function does in one sentence, then give a numbered trace of fib(4) showing cache hits and misses." The rewrite states background, level (summary then trace), and format (numbered steps).