domains / agent-tooling / prompt-engineering / prompt-engineering-patterns

prompt-engineering-patterns

Skill card

combined

The first two copies are near-identical, but the third copy (LeoYeAI/openclaw-master-skills) diverges significantly. It replaces the generic 'prompt_optimizer' library with specific LangChain and Anthropic SDK code, adds a 'Structured Outputs' capability section (Pydantic/JSON mode) that the others lack, and provides different key patterns (e.g., Pydantic schema enforcement, SemanticSimilarityExampleSelector). Because the third copy contains material steps and constraints (specific library usage, structured output patterns) that the others do not, and the others contain a different 'Quick Start' implementation, crowning one would hide a real alternative implementation approach.

Repositories

RepositoryPath
rmyndharis/antigravity-skillsskills/prompt-engineering-patterns/SKILL.md
sickn33/agentic-awesome-skillsplugins/agentic-awesome-skills-claude/skills/prompt-engineering-patterns/SKILL.md
LeoYeAI/openclaw-master-skillsskills/prompt-engineering-patterns/SKILL.md

Other copies

RoleRepositoryPath
peerrmyndharis/antigravity-skillsskills/prompt-engineering-patterns/SKILL.md
peersickn33/agentic-awesome-skillsplugins/agentic-awesome-skills-claude/skills/prompt-engineering-patterns/SKILL.md
peerLeoYeAI/openclaw-master-skillsskills/prompt-engineering-patterns/SKILL.md
adaptersickn33/agentic-awesome-skillsplugins/agentic-awesome-skills/skills/prompt-engineering-patterns/SKILL.md
adaptersickn33/agentic-awesome-skillsskills/prompt-engineering-patterns/SKILL.md

Combined skill

---
name: prompt-engineering-patterns
description: Master advanced prompt engineering techniques to maximize LLM performance, reliability, and controllability in production.
---

# 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

### Option A: Generic Template Approach
```python
from prompt_optimizer import PromptTemplate, FewShotSelector

template = PromptTemplate(
    system="You are an expert SQL developer.",
    instruction="Convert the following natural language query to SQL:\n{query}",
    few_shot_examples=True,
    output_format="SQL code block"
)

selector = FewShotSelector(
    examples_db="sql_examples.jsonl",
    selection_strategy="semantic_similarity",
    max_examples=3
)

prompt = template.render(
    query="Find all users who registered in the last 30 days",
    examples=selector.select(query="user registration date filter")
)
```

### Option B: LangChain & Structured Outputs
```python
from langchain_anthropic import ChatAnthropic
from langchain_core.prompts import ChatPromptTemplate
from pydantic import BaseModel, Field

class SQLQuery(BaseModel):
    query: str = Field(description="The SQL query")
    explanation: str = Field(description="Brief explanation")

llm = ChatAnthropic(model="claude-sonnet-4-6")
structured_llm = llm.with_structured_output(SQLQuery)

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are an expert SQL developer."),
    ("user", "Convert this to SQL: {query}")
])

chain = prompt | structured_llm
result = await chain.ainvoke({"query": "Find all users who registered in the last 30 days"})
```

## Key Patterns

### Progressive Disclosure
1. **Level 1**: Direct instruction
2. **Level 2**: Add constraints
3. **Level 3**: Add reasoning
4. **Level 4**: Add examples

### Instruction Hierarchy
```
[System Context] → [Task Instruction] → [Examples] → [Input Data] → [Output Format]
```

### Error Recovery
- Include fallback instructions
- Request confidence scores
- Ask for alternative interpretations when uncertain

## Best Practices

1. **Be Specific**: Vague prompts produce inconsistent results
2. **Show, Don't Tell**: Examples are more effective than descriptions
3. **Test Extensively**: Evaluate on diverse, representative inputs
4. **Iterate Rapidly**: Small changes can have large impacts
5. **Monitor Performance**: Track metrics in production
6. **Version Control**: Treat prompts as code
7. **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

## Integration Patterns

### With RAG Systems
Combine retrieved context with prompt engineering, ensuring the model states if context is insufficient.

### With Validation
Add a self-verification step where the model checks its response against specific criteria (directness, source usage, uncertainty acknowledgment).