Few-Shot Prompting provides the LLM with a few examples of the desired input-output behavior before asking it to solve a new problem. This technique leverages the model's ability to recognize patterns and adapt to specific tasks without explicit instructions. It's particularly effective for tasks with consistent formats or when you want the model to follow a specific style or approach.
Implementation
Select diverse, representative examples that cover the range of inputs and outputs you expect.
Use a consistent format for all examples, with clear separation between input and output.
Order matters - place examples in increasing order of complexity or arrange them to highlight important patterns.
Include 3-5 examples for most tasks; more complex tasks may benefit from additional examples.
Match the format of your examples exactly when presenting the new problem to solve.
Example selection strategies
Random: Simple baseline selection
Semantic: Examples similar to query
Diversity: Cover different categories
Difficulty: Start simple, increase complexity
Automated: Use embedding similarity for retrieval
Best for
Classification tasks with clear categories
Translation between formats or languages
Summarization with specific style requirements
Style transfer tasks
Build This Pattern
Copy this prompt and paste it into Claude Code, OpenCode, Codex, or Cursor to implement this pattern.
Build a few-shot prompting system with dynamic example selection.
ROLE: You are an in-context learning system that retrieves and presents relevant examples to guide LLM performance on classification and generation tasks.
CONSTRAINTS:
- Default K: 3 examples per prompt; configurable based on context window
- Examples must be stored with embeddings for similarity-based retrieval
- Maximum 100 examples per task type; larger sets require clustering
- Example formatting must be consistent per task type (classification vs generation)
- Handle contradictory examples (same input, different output) by flagging and excluding
TOOL CALLING:
- Use function calling for: store_example(task_type, input, output, metadata?), retrieve_examples(query, task_type, k?), validate_examples(examples[]), format_examples(examples[], task_type)
- Each tool returns structured JSON with example data and metadata
STRUCTURED OUTPUT:
- Example entry must return JSON: { id: string, task_type: string, input: string, output: string, embedding?: number[], metadata: Record<string, any>, created_at: string, usage_count: number }
- Retrieved examples must return JSON: { query: string, task_type: string, examples: [{ id: string, input: string, output: string, similarity: number }], k: number, total_available: number }
- Formatted prompt must return JSON: { task_description: string, examples: [{ input: string, output: string }], new_input: string, full_prompt: string, token_count: number }
CHAIN OF THOUGHT:
- Storage: receive example → generate embedding → store with metadata → update index
- Retrieval: embed query → search similar examples → rank by similarity → select top-k
- Validation: check for contradictions → verify format consistency → ensure diversity
- Formatting: select template → insert examples → add new input → construct prompt
FEW-SHOT EXAMPLES:
Task: Email classification (support, billing, sales)
Examples: [
{ input: 'My invoice is wrong', output: 'billing', similarity: 0.92 },
{ input: 'I need help with my password', output: 'support', similarity: 0.85 },
{ input: 'I want to upgrade my plan', output: 'sales', similarity: 0.88 }
]
Formatted: 'Classify the email into one of: support, billing, sales.\n\nExamples:\nEmail: My invoice is wrong\nClassification: billing\n\nEmail: I need help with my password\nClassification: support\n\nEmail: I want to upgrade my plan\nClassification: sales\n\nEmail: Can you send me a quote?\nClassification:'
EVALUATION CRITERIA:
- Example relevance: percentage of retrieved examples that are topically similar to query
- Format consistency: percentage of examples that follow the expected format
- Prediction accuracy: improvement in LLM accuracy with few-shot examples vs zero-shot
- Token efficiency: percentage of context window used by examples vs remaining for new input
The system should: 1) Implement pipeline with example retrieval, prompt construction, and output extraction, 2) Given task description and N examples (input-output pairs), dynamically select most relevant examples using embedding similarity search, 3) Use formatting template engine that renders examples consistently per task type, 4) Construct final prompt with task description, formatted examples, and new input, 5) Support both classification and generation tasks, 6) Store example sets with embeddings in database for reuse, 7) Handle cases where fewer than K examples exist by using all available, 8) If embedding-based selection fails, fall back to random selection, 9) Detect and warn when examples are contradictory (same input, different output), 10) Support examples with variable-length inputs and outputs, 11) Handle extremely long example sets by truncating to fit context window.