Prompt Engineering Patterns
Seth HobsonCommunityCreated by Seth Hobson
- Skill
- Guidelines
- Agent tooling
- MIT
- Low risk
- Has scripts
AutoGineer summary
Written by AutoGineer from the skill’s own files, not by the author
This skill provides a framework for developing and refining prompts for LLM applications. It covers methodologies such as few-shot learning, chain-of-thought reasoning, and structured output generation. The content includes guidance on creating reusable templates, implementing system prompts, and debugging prompt behavior to achieve consistent results.
What it does
- Provides patterns for few-shot learning and dynamic example selection
- Explains techniques for eliciting step-by-step reasoning via chain-of-thought
- Demonstrates how to enforce structured outputs using schemas
- Offers strategies for iterative prompt optimization and testing
- Includes methods for building modular and conditional prompt templates
- Defines approaches for setting model behavior through system prompts
When to use it
- When designing prompts for complex LLM tasks
- When needing to improve the consistency of model outputs
- When implementing reasoning chains for logic or math problems
- When requiring reliable, machine-readable output formats
- When creating reusable prompt libraries for applications
Advantages
- Standardizes prompt development through established patterns
- Enables more predictable model behavior
- Supports integration with structured data workflows
- Facilitates modular design for complex prompt systems
Usage examples
- “How can I use few-shot learning to improve my model's classification accuracy?”
- The agent provides strategies for selecting relevant examples and constructing effective input-output pairs to guide the model.
- “Show me how to force an LLM to return JSON.”
- The agent demonstrates how to use schema enforcement and structured output patterns to ensure the model returns valid JSON.
- “My prompt is producing inconsistent results, how do I debug it?”
- The agent suggests iterative refinement techniques, such as adding reasoning steps or providing better examples to stabilize output.
- “How do I implement chain-of-thought in my prompt?”
- The agent explains how to use trigger phrases or few-shot reasoning traces to elicit step-by-step logic from the model.
Install
Download the skill as a ZIP:
https://mp-staging.autogineer.com/agent-skills/wshobson/prompt-engineering-patterns/downloadUnzip it into the cross-client skills folder of your project:
mkdir -p .agents/skills && unzip prompt-engineering-patterns.zip -d .agents/skills
Documentation by Seth Hobson
Prompt Engineering Patterns
Master advanced prompt engineering techniques to maximize LLM performance, reliability, and controllability.
When to Use This Skill
Designing complex prompts for production LLM applications
Optimizing prompt performance and consistency
Implementing structured reasoning patterns (chain-of-thought, tree-of-thought)
Building few-shot learning systems with dynamic example selection
Creating reusable prompt templates with variable interpolation
Debugging and refining prompts that produce inconsistent outputs
Implementing system prompts for specialized AI assistants
Using structured outputs (JSON mode) for reliable parsing
Core Capabilities
1. Few-Shot Learning
Example selection strategies (semantic similarity, diversity sampling)
Balancing example count with context window constraints
Constructing effective demonstrations with input-output pairs
Dynamic example retrieval from knowledge bases
Handling edge cases through strategic example selection
2. Chain-of-Thought Prompting
Step-by-step reasoning elicitation
Zero-shot CoT with "Let's think step by step"
Few-shot CoT with reasoning traces
Self-consistency techniques (sampling multiple reasoning paths)
Verification and validation steps
3. Structured Outputs
JSON mode for reliable parsing
Pydantic schema enforcement
Type-safe response handling
Error handling for malformed outputs
4. Prompt Optimization
Iterative refinement workflows
A/B testing prompt variations
Measuring prompt performance metrics (accuracy, consistency, latency)
Reducing token usage while maintaining quality
Handling edge cases and failure modes
5. Template Systems
Variable interpolation and formatting
Conditional prompt sections
Multi-turn conversation templates
Role-based prompt composition
Modular prompt components
6. System Prompt Design
Setting model behavior and constraints
Defining output formats and structure
Establishing role and expertise
Safety guidelines and content policies
Context setting and background information
Quick Start
from langchain_anthropic import ChatAnthropic
from langchain_core.prompts import ChatPromptTemplate
from pydantic import BaseModel, Field
# Define structured output schema
class SQLQuery(BaseModel):
query: str = Field(description="The SQL query")
explanation: str = Field(description="Brief explanation of what the query does")
tables_used: list[str] = Field(description="List of tables referenced")
# Initialize model with structured output
llm = ChatAnthropic(model="claude-sonnet-5")
structured_llm = llm.with_structured_output(SQLQuery)
# Create prompt template
prompt = ChatPromptTemplate.from_messages([
("system", """You are an expert SQL developer. Generate efficient, secure SQL queries.
Always use parameterized queries to prevent SQL injection.
Explain your reasoning briefly."""),
("user", "Convert this to SQL: {query}")
])
# Create chain
chain = prompt | structured_llm
# Use
result = await chain.ainvoke({
"query": "Find all users who registered in the last 30 days"
})
print(result.query)
print(result.explanation)Detailed patterns and worked examples
Detailed pattern documentation lives in references/details.md. Read that file when the navigation tier above is insufficient.
Best Practices
Be Specific: Vague prompts produce inconsistent results
Show, Don't Tell: Examples are more effective than descriptions
Use Structured Outputs: Enforce schemas with Pydantic for reliability
Test Extensively: Evaluate on diverse, representative inputs
Iterate Rapidly: Small changes can have large impacts
Monitor Performance: Track metrics in production
Version Control: Treat prompts as code with proper versioning
Document Intent: Explain why prompts are structured as they are
Common Pitfalls
Over-engineering: Starting with complex prompts before trying simple ones
Example pollution: Using examples that don't match the target task
Context overflow: Exceeding token limits with excessive examples
Ambiguous instructions: Leaving room for multiple interpretations
Ignoring edge cases: Not testing on unusual or boundary inputs
No error handling: Assuming outputs will always be well-formed
Hardcoded values: Not parameterizing prompts for reuse
Success Metrics
Track these KPIs for your prompts:
Accuracy: Correctness of outputs
Consistency: Reproducibility across similar inputs
Latency: Response time (P50, P95, P99)
Token Usage: Average tokens per request
Success Rate: Percentage of valid, parseable outputs
User Satisfaction: Ratings and feedback
Files
- ATTRIBUTION.mdAdded by AutoGineer1019 B
- LICENSELicence file added by AutoGineer1.0 KiB
- SKILL.md5.1 KiB
- assets/few-shot-examples.json4.1 KiB
- assets/prompt-template-library.md3.2 KiB
- references/chain-of-thought.md9.2 KiB
- references/details.md9.0 KiB
- references/few-shot-learning.md10.9 KiB
- references/prompt-optimization.md12.5 KiB
- references/prompt-templates.md11.1 KiB
- references/system-prompts.md5.3 KiB
- scripts/optimize-prompt.pyScript9.3 KiB
references/prompt-templates.md
Prompt Template Systems
Template Architecture
Basic Template Structure
class PromptTemplate:
def __init__(self, template_string, variables=None):
self.template = template_string
self.variables = variables or []
def render(self, **kwargs):
missing = set(self.variables) - set(kwargs.keys())
if missing:
raise ValueError(f"Missing required variables: {missing}")
return self.template.format(**kwargs)
# Usage
template = PromptTemplate(
template_string="Translate {text} from {source_lang} to {target_lang}",
variables=['text', 'source_lang', 'target_lang']
)
prompt = template.render(
text="Hello world",
source_lang="English",
target_lang="Spanish"
)Conditional Templates
class ConditionalTemplate(PromptTemplate):
def render(self, **kwargs):
# Process conditional blocks
result = self.template
# Handle if-blocks: {{#if variable}}content{{/if}}
import re
if_pattern = r'\{\{#if (\w+)\}\}(.*?)\{\{/if\}\}'
def replace_if(match):
var_name = match.group(1)
content = match.group(2)
return content if kwargs.get(var_name) else ''
result = re.sub(if_pattern, replace_if, result, flags=re.DOTALL)
# Handle for-loops: {{#each items}}{{this}}{{/each}}
each_pattern = r'\{\{#each (\w+)\}\}(.*?)\{\{/each\}\}'
def replace_each(match):
var_name = match.group(1)
content = match.group(2)
items = kwargs.get(var_name, [])
return '\\n'.join(content.replace('{{this}}', str(item)) for item in items)
result = re.sub(each_pattern, replace_each, result, flags=re.DOTALL)
# Finally, render remaining variables
return result.format(**kwargs)
# Usage
template = ConditionalTemplate("""
Analyze the following text:
{text}
{{#if include_sentiment}}
Provide sentiment analysis.
{{/if}}
{{#if include_entities}}
Extract named entities.
{{/if}}
{{#if examples}}
Reference examples:
{{#each examples}}
- {{this}}
{{/each}}
{{/if}}
""")Modular Template Composition
class ModularTemplate:
def __init__(self):
self.components = {}
def register_component(self, name, template):
self.components[name] = template
def render(self, structure, **kwargs):
parts = []
for component_name in structure:
if component_name in self.components:
component = self.components[component_name]
parts.append(component.format(**kwargs))
return '\\n\\n'.join(parts)
# Usage
builder = ModularTemplate()
builder.register_component('system', "You are a {role}.")
builder.register_component('context', "Context: {context}")
builder.register_component('instruction', "Task: {task}")
builder.register_component('examples', "Examples:\\n{examples}")
builder.register_component('input', "Input: {input}")
builder.register_component('format', "Output format: {format}")
# Compose different templates for different scenarios
basic_prompt = builder.render(
['system', 'instruction', 'input'],
role='helpful assistant',
instruction='Summarize the text',
input='...'
)
advanced_prompt = builder.render(
['system', 'context', 'examples', 'instruction', 'input', 'format'],
role='expert analyst',
context='Financial analysis',
examples='...',
instruction='Analyze sentiment',
input='...',
format='JSON'
)Common Template Patterns
Classification Template
CLASSIFICATION_TEMPLATE = """
Classify the following {content_type} into one of these categories: {categories}
{{#if description}}
Category descriptions:
{description}
{{/if}}
{{#if examples}}
Examples:
{examples}
{{/if}}
{content_type}: {input}
Category:"""Extraction Template
EXTRACTION_TEMPLATE = """
Extract structured information from the {content_type}.
Required fields:
{field_definitions}
{{#if examples}}
Example extraction:
{examples}
{{/if}}
{content_type}: {input}
Extracted information (JSON):"""Generation Template
GENERATION_TEMPLATE = """
Generate {output_type} based on the following {input_type}.
Requirements:
{requirements}
{{#if style}}
Style: {style}
{{/if}}
{{#if constraints}}
Constraints:
{constraints}
{{/if}}
{{#if examples}}
Examples:
{examples}
{{/if}}
{input_type}: {input}
{output_type}:"""Transformation Template
TRANSFORMATION_TEMPLATE = """
Transform the input {source_format} to {target_format}.
Transformation rules:
{rules}
{{#if examples}}
Example transformations:
{examples}
{{/if}}
Input {source_format}:
{input}
Output {target_format}:"""Advanced Features
Template Inheritance
class TemplateRegistry:
def __init__(self):
self.templates = {}
def register(self, name, template, parent=None):
if parent and parent in self.templates:
# Inherit from parent
base = self.templates[parent]
template = self.merge_templates(base, template)
self.templates[name] = template
def merge_templates(self, parent, child):
# Child overwrites parent sections
return {**parent, **child}
# Usage
registry = TemplateRegistry()
registry.register('base_analysis', {
'system': 'You are an expert analyst.',
'format': 'Provide analysis in structured format.'
})
registry.register('sentiment_analysis', {
'instruction': 'Analyze sentiment',
'format': 'Provide sentiment score from -1 to 1.'
}, parent='base_analysis')Variable Validation
class ValidatedTemplate:
def __init__(self, template, schema):
self.template = template
self.schema = schema
def validate_vars(self, **kwargs):
for var_name, var_schema in self.schema.items():
if var_name in kwargs:
value = kwargs[var_name]
# Type validation
if 'type' in var_schema:
expected_type = var_schema['type']
if not isinstance(value, expected_type):
raise TypeError(f"{var_name} must be {expected_type}")
# Range validation
if 'min' in var_schema and value < var_schema['min']:
raise ValueError(f"{var_name} must be >= {var_schema['min']}")
if 'max' in var_schema and value > var_schema['max']:
raise ValueError(f"{var_name} must be <= {var_schema['max']}")
# Enum validation
if 'choices' in var_schema and value not in var_schema['choices']:
raise ValueError(f"{var_name} must be one of {var_schema['choices']}")
def render(self, **kwargs):
self.validate_vars(**kwargs)
return self.template.format(**kwargs)
# Usage
template = ValidatedTemplate(
template="Summarize in {length} words with {tone} tone",
schema={
'length': {'type': int, 'min': 10, 'max': 500},
'tone': {'type': str, 'choices': ['formal', 'casual', 'technical']}
}
)Template Caching
class CachedTemplate:
def __init__(self, template):
self.template = template
self.cache = {}
def render(self, use_cache=True, **kwargs):
if use_cache:
cache_key = self.get_cache_key(kwargs)
if cache_key in self.cache:
return self.cache[cache_key]
result = self.template.format(**kwargs)
if use_cache:
self.cache[cache_key] = result
return result
def get_cache_key(self, kwargs):
return hash(frozenset(kwargs.items()))
def clear_cache(self):
self.cache = {}Multi-Turn Templates
Conversation Template
class ConversationTemplate:
def __init__(self, system_prompt):
self.system_prompt = system_prompt
self.history = []
def add_user_message(self, message):
self.history.append({'role': 'user', 'content': message})
def add_assistant_message(self, message):
self.history.append({'role': 'assistant', 'content': message})
def render_for_api(self):
messages = [{'role': 'system', 'content': self.system_prompt}]
messages.extend(self.history)
return messages
def render_as_text(self):
result = f"System: {self.system_prompt}\\n\\n"
for msg in self.history:
role = msg['role'].capitalize()
result += f"{role}: {msg['content']}\\n\\n"
return resultState-Based Templates
class StatefulTemplate:
def __init__(self):
self.state = {}
self.templates = {}
def set_state(self, **kwargs):
self.state.update(kwargs)
def register_state_template(self, state_name, template):
self.templates[state_name] = template
def render(self):
current_state = self.state.get('current_state', 'default')
template = self.templates.get(current_state)
if not template:
raise ValueError(f"No template for state: {current_state}")
return template.format(**self.state)
# Usage for multi-step workflows
workflow = StatefulTemplate()
workflow.register_state_template('init', """
Welcome! Let's {task}.
What is your {first_input}?
""")
workflow.register_state_template('processing', """
Thanks! Processing {first_input}.
Now, what is your {second_input}?
""")
workflow.register_state_template('complete', """
Great! Based on:
- {first_input}
- {second_input}
Here's the result: {result}
""")Best Practices
Keep It DRY: Use templates to avoid repetition
Validate Early: Check variables before rendering
Version Templates: Track changes like code
Test Variations: Ensure templates work with diverse inputs
Document Variables: Clearly specify required/optional variables
Use Type Hints: Make variable types explicit
Provide Defaults: Set sensible default values where appropriate
Cache Wisely: Cache static templates, not dynamic ones
Template Libraries
Question Answering
QA_TEMPLATES = {
'factual': """Answer the question based on the context.
Context: {context}
Question: {question}
Answer:""",
'multi_hop': """Answer the question by reasoning across multiple facts.
Facts: {facts}
Question: {question}
Reasoning:""",
'conversational': """Continue the conversation naturally.
Previous conversation:
{history}
User: {question}
Assistant:"""
}Content Generation
GENERATION_TEMPLATES = {
'blog_post': """Write a blog post about {topic}.
Requirements:
- Length: {word_count} words
- Tone: {tone}
- Include: {key_points}
Blog post:""",
'product_description': """Write a product description for {product}.
Features: {features}
Benefits: {benefits}
Target audience: {audience}
Description:""",
'email': """Write a {type} email.
To: {recipient}
Context: {context}
Key points: {key_points}
Email:"""
}Performance Considerations
Pre-compile templates for repeated use
Cache rendered templates when variables are static
Minimize string concatenation in loops
Use efficient string formatting (f-strings, .format())
Profile template rendering for bottlenecks
Requirements and compatibility
Works with: Claude Code, Claude apps, OpenAI Codex, GitHub Copilot, Cursor, Gemini CLI, Kiro, OpenCode, Goose, Amp, Roo Code, JetBrains Junie, Factory Droid, Devin (ex-Windsurf), Mistral Vibe, OpenHands, Letta
“Works with” means the tool reads the open Agent Skills format; a skill written for one tool may use features others lack.
Needs: Needs Python
Security
What our scanner noticed· Low risk
- Scripts that your agent may run
This skill includes scripts that your agent may run. Read them before you use it.
A static scan is not a guarantee. Skills are third-party content: review a skill before you let an agent use it.
Attribution and licence
- Created by
- Seth Hobson
- Publisher
- Seth Hobson (Community)
- Source
- wshobson/agents · plugins/llm-application-dev/skills/prompt-engineering-patterns · commit 4bde316
- Licence
- MIT License (redistribution allowed)
- LICENSE (added by AutoGineer from LICENSE)
- ATTRIBUTION.md (written by AutoGineer)
- Synced
AutoGineer is not affiliated with or endorsed by Seth Hobson.
Download ZIP
The ZIP holds the skill’s files, its licence files and an ATTRIBUTION.md. Download prompt-engineering-patterns.zip