Sub topic 3
Turning a Good Prompt into a Reusable Template
This chapter assumes you have written prompts for a generative AI assistant and can read short Python snippets. You will take one prompt that works and turn it into a template you can run every week with different inputs.
Why a good prompt is not yet a workflow
A prompt that produced a great LinkedIn post last Tuesday usually has the details baked in: the product name, the audience, the tone, the length. To reuse it, someone copies it, edits the details by hand, and hopes nothing gets missed. That is where errors appear. A stale product name or a forgotten word limit goes out to customers.
A prompt template fixes this. It is a prompt with named placeholders for the parts that change, and fixed text for the parts that do not. The fixed text carries your instructions. The placeholders carry the inputs.
Find what varies
Start with a prompt that already works. Read it and mark each piece as one of two kinds:
- Fixed: instructions that stay the same on every run, such as "end with one call to action" or "no hype".
- Variable: details that change per run, such as the announcement, the channel, or the audience.
Give each variable a short, descriptive name like announcement or max_words. Avoid names like x or text1. A teammate should understand a placeholder without reading the whole prompt.
A template in Python
Python's standard library includes string.Template. It marks placeholders with a dollar sign, such as $channel.
from string import Template
TEMPLATE = Template(
"You are writing for $brand_name. Voice: $voice.\n"
"Write a $channel post announcing: $announcement\n"
"Audience: $audience\n"
"Constraints: at most $max_words words; end with one call to action."
)
prompt = TEMPLATE.substitute(
brand_name="Acme Cycles",
voice="friendly, plain, no hype",
channel="LinkedIn",
announcement="our spring service discount",
audience="commuter cyclists",
max_words=90,
)
print(prompt)The method substitute() raises a KeyError if you leave out an input. That is the behavior you want. A loud failure before the model runs is better than a prompt that quietly contains the literal text $audience.
Avoid safe_substitute() for this reason. It leaves missing placeholders in place and gives no error.
The shape of a reusable prompt
The flow below shows how the same template serves many runs.
Common errors
These are the mistakes that most often break a template.
| Mistake | What happens | Fix |
|---|---|---|
| Missing input | Literal `$audience` reaches the model, or a crash | Use `substitute()` so it fails early |
| Too many variables | Every run needs a long form, so nobody uses it | Fix the values that rarely change |
| Hidden assumptions | The prompt only works for one product | Move product facts into inputs |
| Dollar signs in data | Prices like `$20` are read as placeholders | Write `$$20` in the template text, or pass prices as input values |
The last row deserves care. Placeholders are only expanded in the template. Values you pass in are inserted as they are, so a price inside an input such as announcement="Tune-ups from $20" is safe. Only a literal dollar sign written directly in the template needs doubling.
Check the template before you trust it
Run the template with two or three different input sets and read the results. Ask:
- Does each output follow the fixed instructions?
- Does changing an input change only what it should?
- Does the word limit hold across runs?
If one input set breaks the output, the problem is usually a fixed instruction that was too vague. Tighten that line in the template rather than patching the single output. You will formalize this check in a later chapter on testing with fixed inputs.
Exercise
A teammate's template contains the line "Write a post for $audience about $topic." They call TEMPLATE.substitute(topic="spring sale"). What happens, and why is that better than the alternative?
Show answer
Python raises a KeyError because audience was not supplied. This is better than safe_substitute(), which would send the text "$audience" to the model and produce a confusing or wrong draft. Failing before the model call catches the mistake at no cost.