Sub topic 12
Turn good prompts into reusable templates
This chapter assumes you can already write a prompt with a goal, context, constraints, and examples. It also assumes you can run tests on generated code and have used a chat-based or editor-integrated assistant. Here you will turn a prompt that worked into a template you can reuse.
Why templates help
A prompt that worked once usually contains structure you did not plan: a clear goal, a version number, a constraint that blocked a bad answer. Rewriting that from memory each time means you forget parts. Forgotten parts are the usual cause of vague or off-target output.
A template fixes the order and the slots. You fill in the parts that change and keep the parts that don't. Templates also make results easier to compare. If two answers differ, you can see which slot differed.
The five slots
The template below reuses the order from earlier chapters: goal, context, constraints, examples, output format.
Task: <one-sentence goal>
Context: <language and version, framework, relevant existing code, error message>
Constraints: <allowed libraries, style rules, performance limits, what not to change>
Examples: <input -> expected output pairs>
Output format: <for example, only a unified diff, or only the function>Each slot answers one question the assistant cannot answer by itself.
| Slot | Question it answers | Typical mistake |
|---|---|---|
| Task | What should exist when this is done? | A goal you cannot test |
| Context | What does the assistant not see? | Leaving out the version or the error message |
| Constraints | What is allowed and what is off limits? | Not saying "do not change other files" |
| Examples | What does correct behavior look like? | Examples that skip edge cases |
| Output format | What shape should the reply take? | Getting prose when you wanted a diff |
Worked example: from a good prompt to a template
Here is the slug prompt that worked earlier in this tutorial.
Write a Python 3.10 function slugify(text: str) -> str that converts an article title into a URL slug.
Context: it will be used in a small blog tool; titles are plain ASCII.
Constraints: use only the standard library; lowercase the result; replace each run of non-alphanumeric characters with a single hyphen; no leading or trailing hyphens.
Return only the function, with no explanation.To turn it into a template, separate what is specific to this job from what is not.
- Specific to this job: the function name, the slug rules, the blog tool, the ASCII note. These become slots.
- General: the order of the sections, the habit of naming the language version, the instruction to return only code. These stay fixed.
- Add the slot you skipped: examples. The slug prompt gains its strongest part when you include input and output pairs.
Filled in for the same job, the template looks like this:
Task: Write slugify(text: str) -> str that converts an article title into a URL slug.
Context: Python 3.10, small blog tool, titles are plain ASCII.
Constraints: standard library only; lowercase; each run of non-alphanumeric characters becomes one hyphen; no leading or trailing hyphens.
Examples:
slugify("Hello, World!") -> "hello-world"
slugify(" Multiple spaces ") -> "multiple-spaces"
slugify("C++ & Rust") -> "c-rust"
slugify("Already-slugged") -> "already-slugged"
Output format: only the function, with no explanation.The examples double as a test. Keep them in the template workflow so the same pairs check the result:
import pytest
from slugify_module import slugify
@pytest.mark.parametrize(
"text, expected",
[
("Hello, World!", "hello-world"),
(" Multiple spaces ", "multiple-spaces"),
("C++ & Rust", "c-rust"),
("Already-slugged", "already-slugged"),
],
)
def test_slugify(text, expected):
assert slugify(text) == expectedKeep the template current
A template is only useful if it reflects what you learned. Update it when:
- A review or test catches a failure the prompt could have prevented. Add that rule to Constraints.
- The assistant keeps adding explanations you did not want. Tighten the Output format slot.
- Your project changes versions or libraries. Update the Context defaults.
Do not let a template grow into a long list of rules. Each added line costs reading time and can conflict with another line. Remove constraints that never changed an answer.
Do not put secrets in templates
A template gets copied, shared, and committed. Use placeholders such as <paste error message> and never store API keys, customer data, or internal hostnames in it. If a slot needs real data, fill it in at use time, and redact it first.
Common errors
- Too many slots. A template with fifteen fields gets skipped. Keep to the five above and add one only when you reuse it often.
- Filling slots with filler. "Constraints: be good" adds nothing. Leave a slot empty rather than padding it.
- Treating the template as proof. A template improves prompts. It does not verify output. You still run the tests.
The reuse loop
Exercise
You have a prompt that fixes failing tests: "Here is a failing test and the function it calls. Fix the function." It sometimes returns a rewritten file when you wanted a small change. Which template slot do you edit, and what do you add?
Show answer
Edit the Constraints slot to say what not to change (for example, "do not modify the test or other functions"), and the Output format slot to request only a unified diff of the changed function. The template should also have a Context slot for the error message and Python version.
Exercise
Why should the examples from a template's prompt also appear in your tests?
Show answer
The same input and output pairs that define the behavior for the assistant can check the generated code. That lets you verify the result yourself instead of trusting it.