Prompt
Prompt templates with variables and reuse
Latest observation
Here’s a practical guide to prompt templates with variables and reuse.
1) What a prompt template is
A prompt template is a reusable prompt with placeholders for values that change each time.
Example:
Write a {tone} email to {recipient} about {topic}.
You can reuse the same structure by filling in different values.
2) Why use templates
- Consistency: same format every time
- Reuse: avoid rewriting prompts
- Automation: easy to plug into code/workflows
- Maintainability: update once, apply everywhere
3) Basic variable placeholders
Common placeholder styles:
Braces
Summarize {document_type} for {audience}.
Double braces
Summarize {{document_type}} for {{audience}}.
Dollar syntax
Summarize $document_type for $audience.
Choose the style that fits your system or framework.
4) Example templates
A. Email generator
Template:
You are a helpful assistant.
Write a {tone} email to {recipient} about {subject}.
Keep it under {word_count} words.
Filled:
You are a helpful assistant.
Write a professional email to the finance team about the Q4 budget.
Keep it under 120 words.
B. Summarizer
Template:
Summarize the following {content_type} for a {target_audience}:
{content}
C. Classification
Template:
Classify this text into one of these categories: {categories}.
Text:
{text}
5) Reusing templates effectively
A good pattern is to separate:
- template text
- variable values
- rendering logic
Example in pseudocode:
template = "Write a {tone} email to {recipient} about {topic}."
prompt = template.format(
tone="friendly",
recipient="Alex",
topic="the new schedule"
)
6) Tips for robust templates
- Keep variables clear and specific
- Use defaults when possible
- Validate inputs before filling the template
- Avoid overly long templates with too many variables
- Include instructions for output format if needed
Example with explicit format:
Return your answer as JSON with keys: title, summary, action_items.
7) Template with optional sections
You can design templates to support optional content.
Example:
Task: {task}
Context: {context}
{extra_instructions}
If there are no extra instructions, leave that part blank or omit it in your rendering logic.
8) Nested or reusable sub-templates
You can break prompts into reusable pieces:
SYSTEM: {system_instruction}
USER TASK:
{task}
OUTPUT RULES:
{output_rules}
This makes it easier to reuse common instructions across many prompts.
9) Example using a reusable library structure
If you manage prompts in a project, organize them like this:
prompts/
email_template.txt
summary_template.txt
classify_template.txt
And keep variables in code or config:
{
"tone": "professional",
"recipient": "HR",
"topic": "policy update"
}
10) Best practice example
A strong reusable template often looks like:
You are an expert assistant.
Goal: {goal}
Constraints:
- Audience: {audience}
- Tone: {tone}
- Length: {length}
Input:
{input_text}
Output format:
{format}
This makes the prompt flexible and easy to reuse.
If you want, I can also give you:
- Python examples for prompt templating
- JavaScript/TypeScript examples
- A templating pattern for ChatGPT-style system/user messages
- A reusable prompt template library structure