1What Are Prompt Variables?
Prompt variables are placeholders that get replaced with user input before sending to the AI.
**The pattern:** ``` Write a {{tone}} email to {{recipient}} about {{topic}}. Keep it under {{word_count}} words. ```
**Becomes:** ``` Write a professional email to my boss about requesting time off. Keep it under 150 words. ```
**Why use variables?** - Reusable templates - Consistent structure - User customization - Easier testing
2Variable Naming Best Practices
Good variable names make templates self-documenting.
**Do:** - `{{customer_name}}` - descriptive - `{{output_format}}` - clear purpose - `{{tone_style}}` - specific
**Don't:** - `{{x}}` - meaningless - `{{input}}` - too vague - `{{customerNameForEmailSignature}}` - too long
**Conventions:** - Use snake_case: `{{first_name}}` - Keep under 20 characters - Use nouns, not verbs
3Required vs Optional Variables
Not all variables should be required.
**Required variables:** ``` Write a {{document_type}} about {{main_topic}}. ```
**Optional with defaults:** ``` Write a {{document_type}} about {{main_topic}}. Tone: {{tone:professional}} Length: {{word_count:500}} words ```
**Implementation pattern:** ```typescript const defaults = { tone: "professional", word_count: "500" };
function fillTemplate(template, variables) { return template.replace(/{{(\w+)(?::(\w+))?}}/g, (match, name, defaultVal) => { return variables[name] || defaultVal || match; }); } ```
4Complex Variable Patterns
For advanced templates, you may need conditional sections.
**List variables:** ``` Include these topics: {{#topics}} - {{.}} {{/topics}} ```
**Conditional sections:** ``` {{#include_examples}} Here are some examples: {{examples}} {{/include_examples}} ```
**Nested variables:** ``` From: {{sender.name}} <{{sender.email}}> To: {{recipient.name}} ```
For complex patterns, consider using a template engine like Handlebars or Mustache.
5Documenting Your Variables
Good documentation increases sales and reduces support requests.
**For each variable, document:** 1. **Name**: The variable identifier 2. **Description**: What it controls 3. **Type**: text, number, select, etc. 4. **Required**: Yes/No 5. **Default**: If optional 6. **Examples**: 2-3 sample values
**Example documentation:** | Variable | Description | Type | Required | Default | Examples | |----------|-------------|------|----------|---------|----------| | tone | Email formality level | select | No | professional | casual, professional, formal | | recipient | Who receives the email | text | Yes | - | "my manager", "the client" | | word_count | Maximum length | number | No | 200 | 100, 200, 500 |