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 me a few-shot prompting system. Architecture: implement a pipeline with example retrieval, prompt construction, and output extraction. Given a task description and N examples (input-output pairs), dynamically select the most relevant examples for the given input using embedding similarity search against stored example embeddings. Use a formatting template engine that renders examples consistently per task type. Construct the final prompt with task description, formatted examples, and the new input. Support both classification and generation tasks. Store example sets with embeddings in a database for reuse. Error handling: handle cases where fewer than K examples exist by using all available examples. If embedding-based selection fails, fall back to random selection. Detect and warn when examples are contradictory (same input, different output). Edge cases: support examples with variable-length inputs and outputs. Handle extremely long example sets by truncating to fit context window. Support zero-example mode when no examples match a query. Best practices: make K configurable (default 3) with automatic adjustment based on context limits. Log which examples were selected and their similarity scores. Cache example embeddings for performance. Testing: unit test example selection accuracy. Test prompt construction with various example counts (0 to N). Verify formatting consistency across task types. TypeScript.