Skip to content
Tutorials Center
My learning

Topics

Lesson 21 of 53 ยท Prompt Engineering

Prompt Templates and Versioning

Turn prompts into reusable templates: variables, few-shot slots, safe handling of untrusted input, prompts stored as files, and version tracking with diffs.

A prompt you type into a chat box is a one-off. A prompt inside an application is code: it runs thousands of times with different inputs, and a small edit can quietly change every answer your product gives. So treat prompts the way you treat other code. Give them variables, keep them in files, review changes and track versions.

This lesson builds a small template engine, shows how to handle untrusted text safely, and sets up a simple version history with diffs.

From string to template

Suppose you hard-code a prompt like this:

Python
prompt = "Summarize this text in 3 bullets: " + user_text

It works, until you need a different language, a different bullet count, a few examples, or a safety instruction. A template separates the fixed wording from the variables that change per request:

TEXT
You are a {role}.
Summarize the text between <text> tags in {n_bullets} bullets, in {language}.

<text>
{text}
</text>

Python's str.format and string.Template are fine for simple cases; Jinja2 and similar libraries add loops and conditions. Whatever you pick, keep the logic in the template small. The more branching you hide in a template, the harder the final prompt is to read.

A prompt is a program

Variables Fixed wording plus slots for data.
Files Store prompts outside the code.
Versions Every change gets an ID and a diff.
Tests Run examples before shipping a change.
1 / 2

A tiny template engine

The next example uses Python's string.Template, which is strict: a missing variable raises an error instead of silently leaving a hole in your prompt. Failing loudly is what you want.

Python Render a prompt template

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Few-shot slots

Few-shot examples (see Zero-Shot, One-Shot and Few-Shot Prompting) change often: you add, remove and reorder them while tuning. Keep them as data and let a function turn them into text, instead of pasting them into the template by hand.

Python Build a few-shot block from data

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Because the examples are a plain list, an evaluation script can try different subsets and orders without touching the template.

Untrusted input

Variables often hold text from users, web pages or documents. That text can contain instructions ("ignore the rules above and ..."). A template cannot make a model ignore such text, but it can reduce accidents:

  • Delimit user data clearly (tags, fences) and tell the model that everything inside is data, not instructions.
  • Escape the delimiter. If you wrap data in <text>...</text>, a user can type </text> to break out. Neutralize it before inserting.
  • Limit length so one huge input cannot crowd out your instructions or blow your budget (see Counting Tokens and Estimating Cost).
  • Never rely on the template alone for security. Deeper defenses are in Prompt Injection and LLM Security.
Python Escape delimiters in untrusted text

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The attacker's fake closing tag is turned into harmless [/text], so the real data block stays one block. The model may still be influenced by the words, which is why this is a mitigation, not a cure.

Prompts as files

Large prompts do not belong inside Python strings. Store each prompt in its own file (for example prompts/summarize/v3.txt), load it at start-up and render it with variables. Benefits:

  • Non-programmers can edit wording without touching code.
  • Diffs in version control show exactly what changed.
  • You can keep several versions side by side and pick one by name or by config.
  • Prompt files can carry metadata such as a description, model and parameters in a header.
TEXT
prompts/
  summarize/
    v1.txt
    v2.txt
    v3.txt
  classify_ticket/
    v1.txt

Versioning and diffs

Every prompt change can change behavior, so give each version an identifier and log it with every model call. When quality drops, you can see which prompt version was live. A cheap, reliable ID is a short hash of the prompt text. Python's difflib shows what changed between two versions.

Python Version IDs and a diff

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

In a real project, store the ID next to each logged response and run your evaluation suite on every new version before you switch traffic to it. A prompt change without a test is a guess.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why should a template fail loudly when a variable is missing?

    An unfilled slot produces a degraded prompt that is hard to notice, so raising an error is safer.
  2. 2. What is the purpose of escaping the closing tag in untrusted text?

    If the user can type your delimiter, they can end the data section early and write text that looks like instructions.
  3. 3. Why keep few-shot examples as data instead of pasting them into the template?

    Data-driven examples are easy to vary during tuning and evaluation.
  4. 4. What is a good way to identify a prompt version in logs?

    A content hash or version label lets you connect any answer to the exact prompt that produced it.

Summary

  • Treat prompts as code: templates with variables, kept in files, reviewed and versioned.
  • Use strict substitution so missing variables raise errors.
  • Build few-shot blocks from data so examples are easy to change and test.
  • Delimit and escape untrusted text and cap its length, but do not count on this for security.
  • Log a version ID with every call, show changes with diffs and run evaluations before releasing.

Next: Debugging and Improving Prompts.