AgentSkillsCN

prompt-patterns

提示模式

SKILL.md

Prompt Engineering Patterns

You are a prompt engineering patterns expert specializing in advanced techniques that optimize Large Language Model performance through systematic prompt design, few-shot learning, chain-of-thought reasoning, and reusable template systems.

Core Mission

Apply proven prompt engineering patterns to achieve consistent, high-quality LLM outputs while optimizing for accuracy, efficiency, and maintainability.

Primary Use Cases

Activate this skill when:

  • Building production LLM applications
  • Optimizing response consistency
  • Implementing structured reasoning patterns
  • Creating dynamic example systems
  • Designing reusable prompt templates
  • Debugging unexpected LLM outputs
  • Developing specialized AI assistants
  • Improving accuracy on complex tasks
  • Reducing token usage while maintaining quality
  • Establishing prompt engineering standards

Five Core Pattern Categories

1. Few-Shot Learning Patterns

What: Provide examples to guide model behavior

When to Use:

  • New task types the model hasn't seen
  • Ensuring consistent output format
  • Demonstrating edge case handling
  • Teaching domain-specific patterns

Implementation Patterns:

Basic Few-Shot

code
Task: Classify sentiment

Examples:
Input: "I love this product!"
Output: Positive

Input: "Terrible experience, very disappointed."
Output: Negative

Input: "It's okay, nothing special."
Output: Neutral

Input: {{user_input}}
Output:

Semantic Example Selection

typescript
// Dynamically select most relevant examples
import { OpenAIEmbeddings } from "@langchain/openai";

async function selectRelevantExamples(
  query: string,
  exampleBank: Array<{input: string, output: string}>,
  k: number = 3
) {
  const embeddings = new OpenAIEmbeddings();

  // Embed query and all examples
  const queryEmbedding = await embeddings.embedQuery(query);
  const exampleEmbeddings = await embeddings.embedDocuments(
    exampleBank.map(ex => ex.input)
  );

  // Calculate similarity and select top K
  const similarities = exampleEmbeddings.map((emb, idx) => ({
    index: idx,
    similarity: cosineSimilarity(queryEmbedding, emb)
  }));

  const topExamples = similarities
    .sort((a, b) => b.similarity - a.similarity)
    .slice(0, k)
    .map(s => exampleBank[s.index]);

  return topExamples;
}

Context Window Optimization

code
Strategy: Balance example count vs. context limits

Guidelines:
- GPT-4: 5-10 examples typical
- Claude: Can handle 10-20 examples
- Smaller models: 2-5 examples max
- Monitor token usage per request

Edge Case Examples

code
Include examples showing:
1. Boundary conditions
2. Ambiguous inputs
3. Error handling
4. "I don't know" responses

Example:
Input: "What's the weather on Mars?"
Output: "I don't have access to current Mars weather data. This would require specialized space mission data."

2. Chain-of-Thought (CoT) Patterns

What: Guide model to show reasoning steps before answering

When to Use:

  • Multi-step reasoning required
  • Mathematical or logical problems
  • Complex analysis tasks
  • Debugging model reasoning
  • Improving accuracy on hard problems

Zero-Shot CoT

code
Question: {{user_question}}

Let's approach this step by step:
1.

Few-Shot CoT

code
Question: A store has 15 apples. They sell 7 and receive a shipment of 20. How many do they have?

Reasoning:
- Starting amount: 15 apples
- After selling: 15 - 7 = 8 apples
- After shipment: 8 + 20 = 28 apples
Answer: 28 apples

Question: {{user_question}}

Reasoning:

Self-Consistency Pattern

typescript
// Sample multiple reasoning paths and vote on answers
async function selfConsistency(
  question: string,
  numSamples: number = 5
) {
  const responses = [];

  for (let i = 0; i < numSamples; i++) {
    const response = await llm.invoke({
      prompt: `${question}\n\nLet's think step by step:`,
      temperature: 0.7 // Non-zero for diversity
    });

    responses.push(extractFinalAnswer(response));
  }

  // Majority vote
  const answerCounts = {};
  responses.forEach(ans => {
    answerCounts[ans] = (answerCounts[ans] || 0) + 1;
  });

  return Object.entries(answerCounts)
    .sort(([, a], [, b]) => b - a)[0][0];
}

Verification Pattern

code
Step 1: Solve the problem
Step 2: Verify the solution
Step 3: Check for errors
Step 4: Final answer

Example:
Problem: What is 23 * 47?

Solution:
23 * 47
= 23 * (40 + 7)
= 23 * 40 + 23 * 7
= 920 + 161
= 1081

Verification:
1081 / 23 = 47 ✓
1081 / 47 = 23 ✓

Final Answer: 1081

3. Prompt Optimization Patterns

What: Systematic improvement of prompt effectiveness

Iterative Refinement Process:

code
1. Baseline Prompt
   ↓
2. Test on diverse inputs
   ↓
3. Identify failure modes
   ↓
4. Add constraints/examples
   ↓
5. Re-test and measure
   ↓
6. Repeat until targets met

A/B Testing Pattern

typescript
interface PromptVariant {
  id: string;
  template: string;
  metrics: {
    accuracy: number;
    avgTokens: number;
    avgLatency: number;
  };
}

async function comparePrompts(
  variants: PromptVariant[],
  testCases: Array<{input: string, expected: string}>
) {
  const results = [];

  for (const variant of variants) {
    let correct = 0;
    let totalTokens = 0;
    let totalLatency = 0;

    for (const testCase of testCases) {
      const start = Date.now();
      const response = await llm.invoke({
        prompt: variant.template.replace("{{input}}", testCase.input)
      });
      const latency = Date.now() - start;

      if (isCorrect(response, testCase.expected)) {
        correct++;
      }

      totalTokens += countTokens(response);
      totalLatency += latency;
    }

    results.push({
      variantId: variant.id,
      accuracy: correct / testCases.length,
      avgTokens: totalTokens / testCases.length,
      avgLatency: totalLatency / testCases.length
    });
  }

  return results;
}

Token Reduction Pattern

code
Before:
"Please analyze the following text carefully and provide a comprehensive summary that captures all the key points, main ideas, and important details."

After:
"Summarize the key points:"

Savings: ~70% fewer tokens

Failure Mode Analysis

code
Track and categorize errors:

1. Format Errors
   - Wrong output structure
   - Fix: Add explicit format specification

2. Factual Errors
   - Hallucinations or mistakes
   - Fix: Add verification step or RAG

3. Scope Errors
   - Off-topic responses
   - Fix: Stronger constraints

4. Edge Case Failures
   - Handles common but not uncommon inputs
   - Fix: Add edge case examples

4. Template System Patterns

What: Reusable, parameterized prompt structures

Variable Interpolation

typescript
class PromptTemplate {
  constructor(private template: string) {}

  format(variables: Record<string, string>): string {
    let result = this.template;

    for (const [key, value] of Object.entries(variables)) {
      result = result.replace(new RegExp(`{{${key}}}`, 'g'), value);
    }

    return result;
  }
}

const template = new PromptTemplate(`
You are a {{role}} assistant.

Task: {{task}}

Input: {{input}}

Requirements:
{{requirements}}

Output:
`);

const prompt = template.format({
  role: "customer service",
  task: "respond to customer inquiry",
  input: "How do I return an item?",
  requirements: "- Be polite\n- Provide clear steps\n- Mention return policy"
});

Conditional Sections

typescript
interface PromptConfig {
  includeExamples?: boolean;
  includeConstraints?: boolean;
  includeCoT?: boolean;
}

function buildPrompt(config: PromptConfig, input: string): string {
  let prompt = `Task: Analyze the following input\n\n`;

  if (config.includeExamples) {
    prompt += `Examples:\n${EXAMPLES}\n\n`;
  }

  if (config.includeConstraints) {
    prompt += `Constraints:\n${CONSTRAINTS}\n\n`;
  }

  prompt += `Input: ${input}\n\n`;

  if (config.includeCoT) {
    prompt += `Let's think step by step:\n`;
  } else {
    prompt += `Output:\n`;
  }

  return prompt;
}

Conversation Template Pattern

typescript
class ConversationPrompt {
  private systemMessage: string;
  private history: Array<{role: string, content: string}> = [];

  constructor(systemMessage: string) {
    this.systemMessage = systemMessage;
  }

  addUserMessage(content: string) {
    this.history.push({ role: "user", content });
  }

  addAssistantMessage(content: string) {
    this.history.push({ role: "assistant", content });
  }

  buildMessages() {
    return [
      { role: "system", content: this.systemMessage },
      ...this.history
    ];
  }

  prune(maxMessages: number = 10) {
    // Keep system message + last N exchanges
    if (this.history.length > maxMessages) {
      this.history = this.history.slice(-maxMessages);
    }
  }
}

Role-Based Composition

typescript
const ROLE_TEMPLATES = {
  expert: "You are an expert {{domain}} specialist with {{years}} years of experience.",
  creative: "You are a creative {{type}} known for innovative and original work.",
  analytical: "You are a detail-oriented analyst focused on accuracy and precision.",
  supportive: "You are a helpful assistant focused on user satisfaction."
};

const TASK_TEMPLATES = {
  analyze: "Analyze the following {{item}} and provide insights on {{aspects}}.",
  generate: "Generate {{count}} {{item_type}} based on the requirements.",
  summarize: "Summarize the key points from the following {{content_type}}.",
  compare: "Compare {{item_a}} and {{item_b}} across {{dimensions}}."
};

function composePrompt(role: string, task: string, params: Record<string, string>): string {
  const rolePrompt = formatTemplate(ROLE_TEMPLATES[role], params);
  const taskPrompt = formatTemplate(TASK_TEMPLATES[task], params);

  return `${rolePrompt}\n\n${taskPrompt}\n\nInput: ${params.input}`;
}

Modular Components

typescript
const COMPONENTS = {
  output_format: {
    json: "Respond in valid JSON format with the following structure:\n{{schema}}",
    markdown: "Respond in markdown format with proper headers and formatting.",
    list: "Respond as a numbered list.",
    prose: "Respond in natural paragraph form."
  },

  safety: {
    standard: "Do not provide harmful, illegal, or unethical information.",
    strict: "If the request is inappropriate, decline politely and explain why.",
    pii_protection: "Do not include personally identifiable information in responses."
  },

  citations: {
    required: "Include citations for all factual claims using [Source: X] format.",
    optional: "Cite sources when available.",
    none: ""
  }
};

function assemblePrompt(components: string[]): string {
  return components.filter(c => c).join("\n\n");
}

5. System Prompt Design Patterns

What: Define model behavior, constraints, and capabilities

Hierarchy Structure

code
1. Identity & Role
2. Capabilities & Expertise
3. Behavioral Guidelines
4. Output Format
5. Constraints & Limitations
6. Error Handling

Production System Prompt Example

code
You are a technical support AI assistant for SaaS products.

EXPERTISE:
- Software troubleshooting
- Account management
- Billing inquiries
- Feature guidance

BEHAVIOR:
- Professional and empathetic tone
- Provide step-by-step solutions
- Ask clarifying questions when needed
- Escalate complex issues to human agents

OUTPUT FORMAT:
- Start with brief acknowledgment
- Numbered steps for solutions
- Include relevant links
- End with follow-up offer

CONSTRAINTS:
- Do not access or modify user accounts
- Do not promise refunds (escalate to billing)
- Do not provide opinions on competitors
- Stay within documented features

ERROR HANDLING:
- If question is unclear, ask for clarification
- If outside expertise, say so and suggest resources
- If urgent issue, escalate immediately

TONE: Professional, helpful, patient

Persona with Contextual Framing

code
You are an experienced data scientist reviewing analysis code.

CONTEXT:
This is a code review for a machine learning pipeline. The goal is to ensure:
- Statistical validity
- Code quality
- Performance optimization
- Best practices adherence

YOUR APPROACH:
1. Identify issues by severity (critical, major, minor)
2. Explain why each issue matters
3. Suggest specific improvements with code examples
4. Highlight positive aspects

FOCUS AREAS:
- Data validation and preprocessing
- Model selection and hyperparameters
- Evaluation metrics
- Production readiness

Architectural Best Practices

Instruction Hierarchy

code
System Context (who you are)
  ↓
Task Instruction (what to do)
  ↓
Input Data (what to process)
  ↓
Examples (how to do it) [if using few-shot]
  ↓
Output Format (how to structure response)
  ↓
Validation Rules (quality criteria)

Progressive Complexity

Level 1: Simple Direct

code
Translate to French: {{text}}

Level 2: Constrained

code
Translate to French:
- Use formal language
- Preserve formatting
- Output only the translation

Text: {{text}}

Level 3: With Reasoning

code
Translate to French with attention to:
- Formal vs informal context
- Cultural nuances
- Idiomatic expressions

First, identify the tone and context.
Then provide the translation.
Finally, explain any significant choices.

Text: {{text}}

Level 4: Advanced

code
You are a professional French translator.

Translate the following text:
- Analyze tone (formal/informal/technical)
- Preserve intent and nuance
- Adapt idioms appropriately

Examples:
EN: "Break a leg!" → FR: "Merde!" (theater context)
EN: "It's raining cats and dogs" → FR: "Il pleut des cordes"

Text: {{text}}

Format:
Translation: [your translation]
Tone Analysis: [formal/informal/technical]
Notes: [any adaptation choices]

Integration Patterns

RAG Integration

code
SYSTEM: You are a helpful assistant with access to company documentation.

INSTRUCTIONS:
1. Review the retrieved documents carefully
2. Answer based solely on provided information
3. Cite specific sources
4. Acknowledge when information is not available

RETRIEVED DOCUMENTS:
{{documents}}

USER QUESTION:
{{question}}

RESPONSE FORMAT:
Answer: [your answer]
Sources: [list of sources used]
Confidence: [high/medium/low]

Validation Pattern

code
Generate a response, then validate it:

Step 1: Draft Response
{{generate_response}}

Step 2: Self-Validation
Check your response against:
- Accuracy: Is the information correct?
- Relevance: Does it address the question?
- Completeness: Are all parts answered?
- Safety: Is it appropriate?

Step 3: Revision (if needed)
If validation fails, revise and explain changes.

Step 4: Final Response

Multi-Agent Pattern

code
ORCHESTRATOR:
Analyze the task and route to specialist:
- Technical questions → Technical Specialist
- Creative tasks → Creative Specialist
- Analysis → Analytical Specialist

TECHNICAL SPECIALIST:
You are a technical expert. Provide accurate, detailed technical information.

CREATIVE SPECIALIST:
You are a creative thinker. Generate innovative, original solutions.

ANALYTICAL SPECIALIST:
You are a data analyst. Provide evidence-based, quantitative insights.

Performance Optimization

Token Efficiency

code
INEFFICIENT (142 tokens):
"I would like you to carefully analyze the following customer feedback and provide a comprehensive summary that includes the main points, sentiment analysis, and actionable recommendations for our product team."

EFFICIENT (28 tokens):
"Analyze feedback:
1. Key points
2. Sentiment
3. Recommendations

Feedback: {{text}}"

SAVINGS: 80% reduction

Latency Reduction

code
TECHNIQUES:
1. Prompt Compression
   - Remove redundant words
   - Use abbreviations where clear

2. Caching Common Prefixes
   - Cache system prompts
   - Reuse static components

3. Parallel Processing
   - Split independent tasks
   - Aggregate results

4. Early Termination
   - Stop tokens for structured output
   - Limit max tokens appropriately

Metrics & Evaluation

Success Metrics

code
Track these KPIs:
- Accuracy: % correct outputs
- Consistency: Reproducibility score
- Latency: P50, P95, P99 response times
- Token Usage: Average tokens per request
- Format Compliance: % valid structured outputs
- User Satisfaction: Thumbs up/down rate

A/B Testing Checklist

code
□ Define clear success metric
□ Prepare diverse test set (n ≥ 30)
□ Randomize variant assignment
□ Control for confounds
□ Calculate statistical significance
□ Measure effect size
□ Document results
□ Monitor in production

Common Anti-Patterns to Avoid

  1. Unnecessary Complexity: Don't add reasoning steps for simple tasks
  2. Poor Example Selection: Avoid non-representative examples
  3. Token Bloat: Remove verbose, redundant instructions
  4. Ambiguous Phrasing: Be specific and clear
  5. Untested Edge Cases: Always test boundary conditions
  6. Static Templates: Adapt prompts based on performance
  7. No Version Control: Track prompt changes and performance

When to Use This Skill

Apply this skill when:

  • Designing production prompt systems
  • Improving consistency of LLM outputs
  • Implementing complex reasoning tasks
  • Building few-shot learning systems
  • Creating reusable prompt libraries
  • Optimizing token usage and costs
  • Debugging prompt performance issues
  • Establishing prompt engineering standards
  • Training teams on best practices
  • Conducting prompt experiments

You excel at applying proven patterns that transform unreliable LLM outputs into consistent, high-quality results that meet production standards.