A prompt template is a prompt with blanks. The fixed text carries everything you have learned about getting a good answer for one kind of task: the role, the format, the limits. The blanks carry what changes each time: the topic, the language, the text to work on. Filling in three blanks is faster than writing a prompt, and it gives you answers of a consistent quality and shape.
The template below explains any programming concept at your level. Type your own values into the fields, switch off a part to see what it contributes, then copy it or open it in a chat app.
The idea: A closure is a function that remembers the variables from the place where it was created, even after the outer function has finished running.
Example:
function makeCounter() {
let count = 0;
return function () {
count += 1; // still sees count from makeCounter
return count;
};
}
const next = makeCounter();
console.log(next()); // 1
console.log(next()); // 2
Common mistake: expecting two counters to share one total. Every call to makeCounter() creates a new count, so each counter keeps its own.
What makes a good template
A template is only as good as the prompt it came from, so the fixed text should be a prompt you have already run and liked. Three choices separate a useful template from a form that produces mediocre answers faster.
Blank only what changes. If you always want the answer under 150 words, write 150 into the fixed text. Every blank is a decision you have to make again on every use, and a template with ten blanks is slower to fill than writing the prompt fresh.
Give every blank a clear name and a sensible default. {{known}} tells you what to type; {{x}} does not. A default shows the kind of value expected and lets you run the template once without typing anything.
Keep one template per job. A template that explains concepts, reviews code and writes quiz questions depending on a blank does all three worse than three separate templates. The more specific the fixed text, the more it can say about format and standard.
The six parts from how to write a prompt are a good checklist for the fixed text: role, task, context, input, format, constraints. The blanks usually fall in the task, the context and the input.
Prompt template examples
These three cover common everyday jobs. Each has named fields to fill in. For a larger collection of ready prompts, see prompt examples.
def median(numbers: list[float]) -> float:
if not numbers:
raise ValueError("median() needs at least one number")
ordered = sorted(numbers)
middle = len(ordered) // 2
if len(ordered) % 2 == 1:
return ordered[middle]
return (ordered[middle - 1] + ordered[middle]) / 2
# median([3, 1, 2]) returns 2
# median([4, 1, 3, 2]) returns 2.5
# median([]) raises ValueError
Hi, thank you for the invitation, I'm glad you thought of me! Unfortunately I can't make it on the 14th. Would a date next month work instead? I'd be happy to talk about the project then.
Best, [Your name]
The tags around the pasted text are there for a reason. Filled-in text can contain sentences that look like instructions, and delimiters keep the model from confusing your template's instructions with the material it should work on.
How to build your own template
- Start from a prompt that worked. Take a conversation where the answer came out right, and collect the final version of the prompt, including any corrections you had to add in follow-up messages. Those corrections are the most valuable part, because they fix what the first version got wrong.
- Mark what would change next time. Read the prompt and underline every word you would edit for the next use. Usually it is two to four things: a subject, an audience, a length, a pasted input.
- Replace each one with a named blank. Write the blank the way your tool expects it, such as
[audience]for a prompt you paste by hand or{audience}for code. - Test with three different fillings. Use one typical case, one very short or very long input, and one unusual case. If one of them fails, the fixed text is still assuming something about the first use. Change one sentence at a time, as described in iterating on prompts.
- Save it where you will find it. A notes file, a text expander or a snippet manager all work. A template you have to search for is a template you will stop using.
Templates in code
In a program, a template is a string with named fields. Python's str.format() fills them in:
SUMMARY_TEMPLATE = """Summarize the text inside the <text> tags for {audience}.
Use at most {bullets} bullet points and lead with any action the reader must take.
<text>
{text}
</text>"""
with open("notes.txt", encoding="utf-8") as f:
meeting_notes = f.read()
prompt = SUMMARY_TEMPLATE.format(
audience="a manager who has two minutes",
bullets=3,
text=meeting_notes,
)
With str.format(), every literal curly brace in the template has to be doubled ({{ and }}), which matters when the template includes a JSON example. Braces inside the values you pass in, such as code in meeting_notes, need no escaping. Some libraries for building AI apps have their own template classes, but they do the same job as a format string with extra features around it.
When the values come from users, remember that the template sends their text to the model. Keep it inside delimiters, and do not give the model tools or data that a malicious input could misuse.
Template or system prompt
A template and a system prompt solve different problems. The system prompt holds rules that apply to every message in a conversation or an app: the persona, the tone, what the assistant must never do. A template holds one task with blanks. In an app you often use both: a fixed system prompt that sets the ground rules, and a template that builds each user message.
Frequently Asked Questions
What is a prompt template?
A prompt template is a reusable prompt with placeholders, such as [topic] or {language}, for the parts that change between uses. The fixed text holds the instructions you have already tested; you fill in the blanks each time instead of writing the prompt from scratch.
How do I make my own prompt template?
Start from a prompt that already gave you a good answer. Mark the words you would change for the next use, replace each with a named blank, and test the template with three different fillings, including one unusual case. Keep the instructions that made the first answer good exactly as they were.
Where should I save prompt templates?
Anywhere you can copy from quickly: a notes file, a text expander, or a snippet manager. Chat apps also have places for text you reuse, such as custom instructions and project instructions, which suit rules that apply to every message more than one-off tasks with blanks.
How do I use a prompt template in Python?
Store the template as a string with named fields and fill it with str.format() or an f-string. With str.format(), write any literal curly brace in the template twice ({{ and }}), for example when the template contains a JSON example. Put user-supplied text inside delimiters so it stays separate from the instructions.