Skip to content
AI Dev Toolkit.
Esc
  • Loading…

Prompt template · Code

Explain code prompt template

A good explain code prompt names the reader and what they need to know, then asks for the purpose first, the flow in the order it runs, the side effects and anything surprising. This template does that in about 400 words or fewer, and tells the model to say what it would need rather than guess.

Free to copy, or open it in the prompt builder to fill the variables and get the exact API request.

234 tokens unfilled · 305 with the example below · o200k_base (GPT-4o and later)

Template

The code explanation prompt template (copy it)

Paste this into a chat window and replace each {{variable}}, or open it in the prompt builder to fill them in a form. Sections are marked with XML tags, which every major model reads well.

Prompt (one message, plain text)
You are a patient senior engineer explaining code to a colleague who is {{audience}}.

<context>
<code>
{{code}}
</code>
</context>

<constraints>
- Start with the purpose in one or two sentences before any detail.
- Explain the flow in the order it runs, not line by line. Group lines that do one job.
- Point out side effects (I/O, network, global state, mutation of arguments) and anything surprising: implicit conversions, error handling that swallows failures, order-dependent behaviour.
- If the purpose isn’t clear from the code alone, say what you would need to know rather than guessing.
- Match the depth to the reader. Define a term the first time you use it if they might not know it.
</constraints>

<output_format>
Short paragraphs with a few headings: Purpose, How it works, Side effects, Watch out for. Quote identifiers in backticks. No more than about 400 words unless the code needs it.
</output_format>

<task>
Explain what this code does and how it works, so the reader could safely change it. Focus on {{focus}}.
</task>
For an API call: system prompt and user message

With a system prompt, the role, constraints and output format stay fixed across calls (and can be cached), while the context and task go in the user message.

System prompt
You are a patient senior engineer explaining code to a colleague who is {{audience}}.

<constraints>
- Start with the purpose in one or two sentences before any detail.
- Explain the flow in the order it runs, not line by line. Group lines that do one job.
- Point out side effects (I/O, network, global state, mutation of arguments) and anything surprising: implicit conversions, error handling that swallows failures, order-dependent behaviour.
- If the purpose isn’t clear from the code alone, say what you would need to know rather than guessing.
- Match the depth to the reader. Define a term the first time you use it if they might not know it.
</constraints>

<output_format>
Short paragraphs with a few headings: Purpose, How it works, Side effects, Watch out for. Quote identifiers in backticks. No more than about 400 words unless the code needs it.
</output_format>
User message
<context>
<code>
{{code}}
</code>
</context>

<task>
Explain what this code does and how it works, so the reader could safely change it. Focus on {{focus}}.
</task>

Variables

What do the variables mean?

{{audience}}
Who is reading, such as “new to Rust but experienced in Python” or “a product manager”. It sets the depth and vocabulary.
{{code}}
The function, file or snippet to explain. Include imports and type definitions it relies on when you can.
{{focus}}
What you need from the explanation, such as “how errors are handled” or “why it’s slow”. Use “the overall behaviour” if you have no specific question.

Use

When should I use this code explanation prompt?

Use it when you inherit code you didn’t write: a legacy module, a regex-heavy parser, a dependency you need to patch, or a pull request in a language you rarely use. It is also useful for writing onboarding notes, because the output is shaped around what someone needs to change the code safely.

For a quick question about one line, a plain question is faster. This template earns its keep when the code is long enough that an unstructured explanation would turn into a line-by-line narration.

Structure

Why is the prompt structured this way?

The audience is in the role because the same code needs a very different explanation for a Rust beginner and for a staff engineer. Anthropic’s guide notes that even one sentence of role sets tone and depth, and naming the reader is the most useful role you can give for an explanation.

The constraints fix the two common failures of AI explanations. Line-by-line narration (“this line declares a variable”) is long and unhelpful, so the template asks for the flow grouped by job. And explanations often miss what matters for safe changes, so side effects and surprises get their own section.

The focus variable turns a general walkthrough into an answer to the question you actually have. The word cap keeps the output readable; the escape hatch “unless the code needs it” stops the model cutting a genuinely complex explanation short.

Example

Example: a filled-in code explanation prompt

{{audience}}
comfortable with JavaScript but new to Go
{{code}}
func (c *Cache) Get(key string) (string, bool) {
	c.mu.RLock()
	e, ok := c.items[key]
	c.mu.RUnlock()
	if !ok || time.Now().After(e.expires) {
		return "", false
	}
	return e.value, true
}
{{focus}}
why there is a lock and what the two return values mean

What to look for. A good answer explains the read lock in terms a JavaScript developer recognises (Go runs goroutines in parallel, so a map can be read and written at the same time), explains Go’s comma-ok idiom, and points out that expired entries are never deleted here, which is a real trap for someone about to change the code.

The complete filled-in prompt (305 tokens)
Filled-in prompt
You are a patient senior engineer explaining code to a colleague who is comfortable with JavaScript but new to Go.

<context>
<code>
func (c *Cache) Get(key string) (string, bool) {
	c.mu.RLock()
	e, ok := c.items[key]
	c.mu.RUnlock()
	if !ok || time.Now().After(e.expires) {
		return "", false
	}
	return e.value, true
}
</code>
</context>

<constraints>
- Start with the purpose in one or two sentences before any detail.
- Explain the flow in the order it runs, not line by line. Group lines that do one job.
- Point out side effects (I/O, network, global state, mutation of arguments) and anything surprising: implicit conversions, error handling that swallows failures, order-dependent behaviour.
- If the purpose isn’t clear from the code alone, say what you would need to know rather than guessing.
- Match the depth to the reader. Define a term the first time you use it if they might not know it.
</constraints>

<output_format>
Short paragraphs with a few headings: Purpose, How it works, Side effects, Watch out for. Quote identifiers in backticks. No more than about 400 words unless the code needs it.
</output_format>

<task>
Explain what this code does and how it works, so the reader could safely change it. Focus on why there is a lock and what the two return values mean.
</task>

Models

Does it work the same in ChatGPT, Claude and Gemini?

Yes, the structure works in all three: tagged sections that keep your material apart from the instructions, and an explicit output format. The tips below cover where the providers’ own guides differ for this code explanation prompt.

  • Gemini

    Gemini’s prompting guide recommends always including examples. If you explain code often for the same team, add one short example of an explanation in your house style; keep its format identical to what you want back.

  • OpenAI GPT

    OpenAI’s reasoning models avoid Markdown unless asked, from o1-2024-12-17 on. Since this format uses headings, add “Formatting re-enabled” on the first line of the developer message if you call a reasoning model through the API.

  • All models

    For a whole repository, explain one entry point at a time. Context windows are large, but an explanation of everything at once is rarely one you can act on.

FAQ

Questions people ask

Can ChatGPT explain code?

Yes, and it does it better when you say who is reading. “New to Go but fluent in JavaScript” gets a different answer from “staff engineer”. The model reads only the code you paste, so include the imports and types the snippet relies on, and treat any claim about code you didn’t paste as a guess.

How do I get a line-by-line explanation?

Ask for it in the focus variable, but try the grouped version first. This template explains the flow by job, not line by line, because narration such as “this line declares a variable” is long and rarely helps you change the code. For one dense regex or expression, a line-by-line pass is worth asking for.

How do I explain a whole legacy module?

One entry point at a time. Paste the function a request or job starts from, ask for its flow and side effects, then follow the calls it makes in later runs. An explanation of a whole repository at once is rarely one you can act on, even when it fits in the model’s context window.

How do I check the explanation is right?

Test the claims that matter. Run the code with an input the explanation makes a prediction about, or read the one function it says swallows errors. The “Watch out for” section is where models add the most value and also where a wrong claim does the most harm, so check those points first.

Related

Edited the template? Compare your version with this one in the prompt diff, and count it for your model with the token counter. In the glossary: system prompt. Browse all 29 prompt templates.

Keep exploring