Few-shot prompting is an in-context learning technique where a large language model is conditioned to perform a new task by providing a small number of input-output examples, called demonstrations, within its prompt before the target query. This method allows the model to infer the task's pattern, format, and rules without updating its internal parameters, effectively steering its generation for the final answer. The number of examples is typically small (e.g., 2 to 5) to conserve the model's limited context window.
Glossary
Few-Shot Prompting

What is Few-Shot Prompting?
Few-shot prompting is a core technique in prompt engineering that enables large language models to perform new tasks by providing contextual examples.
The technique's effectiveness hinges on demonstration selection and demonstration ordering, where strategically chosen and arranged examples significantly influence performance. Key considerations include ensuring task-example alignment and managing demonstration bias. It is a foundational method for instruction tuning and is often enhanced by retrieval-augmented in-context learning (RA-ICL), which dynamically fetches the most relevant examples for a given query.
Key Components of a Few-Shot Prompt
A few-shot prompt is a structured input that conditions a language model's response by providing a small number of task-specific examples. Its effectiveness depends on the precise engineering of several core components.
Demonstrations (Examples)
Demonstrations are the core input-output pairs that illustrate the task for the model. Each demonstration consists of an input query and its corresponding target output.
- Purpose: To establish a pattern or rule for the model to follow via in-context learning.
- Key Property: Must exhibit high task-example alignment, meaning the format and domain closely match the target task.
- Example: For a sentiment classification task, a demonstration would be:
Input: "The movie was captivating." -> Output: "positive"
Task Instructions
Task instructions are the natural language directives that explicitly state the objective, format, and constraints for the model.
- Purpose: To provide high-level guidance and disambiguate the intent of the demonstrations.
- Key Property: Should be concise, unambiguous, and work in synergy with the examples (instruction-example interplay).
- Example: "Classify the sentiment of the following movie reviews as 'positive', 'negative', or 'neutral'. Use the exact format from the examples."
Query (Test Input)
The query is the new, unseen input for which the model must generate an output, based on the pattern inferred from the demonstrations.
- Purpose: The target problem to be solved using in-context learning.
- Key Property: Should be placed clearly after the demonstrations and instructions. The model's response to this is the primary output.
- Example: Following the sentiment examples, the query would be:
Input: "The plot was predictable and dull."
Formatting & Delimiters
Formatting refers to the syntactic structure used to separate and identify the different components within the prompt.
- Purpose: To ensure the model correctly parses instructions, examples, and the query. Poor formatting is a major source of error.
- Key Elements:
- Delimiters: Clear markers like
###,---, orExample:to separate sections. - Consistency: Uniform structure across all demonstrations.
- Whitespace: Strategic use of newlines and indentation for readability.
- Delimiters: Clear markers like
Optimal K (Number of Examples)
Optimal K (or Few-Shot K) is the ideal number of demonstrations that maximizes task performance for a given model and task.
- Purpose: To balance the informational benefit of examples against the limited context window.
- Trade-off: Too few examples may not establish a clear pattern; too many may consume tokens needed for reasoning or push out relevant context.
- Finding K: Determined empirically through ablation studies. For many tasks, performance often plateaus or degrades after 4-6 well-chosen examples.
Demonstration Selection Strategy
This is the methodology for choosing which specific examples to include from a larger corpus.
- Purpose: To maximize demonstration relevance and diversity, improving model generalization.
- Common Methods:
- Embedding-Based Selection: Uses vector similarity (e.g., cosine) between the query and candidate examples.
- Diversity Sampling: Selects examples that cover different sub-tasks or input styles.
- Utility Scoring: Ranks examples by predicted positive impact on the target query.
- Avoiding Bias: Must guard against demonstration bias that teaches spurious correlations.
How Few-Shot Prompting Works
Few-shot prompting is a core technique in context engineering that conditions a large language model's response by providing task-specific examples directly within its input context.
Few-shot prompting is an in-context learning technique where a language model is conditioned for a new task by providing a small number of input-output examples, called demonstrations, within its prompt. The model infers the task's pattern from these examples without updating its internal weights. This method is foundational to prompt architecture, enabling reliable steering of model behavior for tasks like classification, translation, or structured output generation by leveraging the model's demonstrated ability to learn from context.
The efficacy of few-shot prompting depends on strategic demonstration selection and demonstration ordering. Engineers optimize for demonstration relevance and diversity to cover the task's input space, while managing the limited context window. Techniques like retrieval-augmented in-context learning dynamically fetch the most relevant examples for each query. The instruction-example interplay—how natural language instructions combine with demonstrations—is critical for guiding the model to produce deterministic, formatted outputs as required in enterprise applications.
Examples of Few-Shot Prompting
Few-shot prompting guides a model by providing a small set of task-specific examples. These cards illustrate its application across diverse domains, from code generation to creative writing.
Code Generation & Translation
Few-shot prompting is highly effective for programming tasks. Demonstrations show the model the exact input-output pattern required.
- Example Task: Convert a Python function from using lists to using NumPy arrays.
- Demonstration:
Input: def sum_list(l): return sum(l)→Output: import numpy as np; def sum_array(arr): return np.sum(arr) - Query:
def avg_list(l): return sum(l)/len(l) - Expected Output: The model, conditioned by the example, should generate:
import numpy as np; def avg_array(arr): return np.mean(arr). This technique is foundational for tools like GitHub Copilot, where in-context examples define coding style and library preferences.
Text Classification & Sentiment Analysis
Providing labeled examples teaches the model a classification schema without fine-tuning.
- Demonstrations:
"I loved the movie!" → Sentiment: Positive"The product broke immediately." → Sentiment: Negative"The meeting is at 3 PM." → Sentiment: Neutral
- Query:
"The service was adequate, nothing special." - Model Behavior: By matching patterns from the examples, the model infers the correct label is Neutral. This method is used for rapid prototyping of classifiers for custom categories like intent detection (e.g.,
"reset password" → Intent: Account_Help).
Data Formatting & Extraction
Few-shot examples act as a template for transforming unstructured text into structured formats like JSON or XML.
- Task: Extract person and company entities from a sentence.
- Demonstration:
Input: "Elon Musk is the CEO of SpaceX."Output: {"person": "Elon Musk", "company": "SpaceX"} - Query:
"Satya Nadella leads Microsoft." - Expected Output: The model generates
{"person": "Satya Nadella", "company": "Microsoft"}. This is crucial for Structured Output Generation, enabling reliable API integration. The demonstrations define the exact key names and nesting structure.
Creative Writing & Style Mimicry
Examples demonstrate a specific tone, genre, or authorial voice for the model to replicate.
- Demonstrations:
Write a haiku about coffee: "Steam rises softly, / Bitter warmth in porcelain, / Morning's quiet spark."Write a haiku about rain: "A steady rhythm, / On the windowpane it plays, / City's lullaby."
- Query:
Write a haiku about a keyboard. - Model Output: Conditioned by the 5-7-5 syllable structure and reflective tone, it might generate:
"Fingers dance lightly, / Clicks and clacks form quiet thoughts, / Words begin to flow."This showcases in-context learning generalization to new subjects within a learned stylistic framework.
Mathematical Reasoning & Chain-of-Thought
Few-shot prompts can include multi-step reasoning (Chain-of-Thought) to solve complex problems.
- Demonstration:
Q: A bakery had 12 cookies. They sold 4 in the morning and 5 in the afternoon. How many are left?A: They started with 12. Morning: 12 - 4 = 8 left. Afternoon: 8 - 5 = 3 left. The answer is 3. - Query:
Q: There are 15 books on a shelf. I add 7 new books, then loan out 10 to a friend. How many books remain? - Model Behavior: The example induces step-by-step reasoning. The model should output:
Started with 15. Add 7: 15 + 7 = 22. Loan 10: 22 - 10 = 12. The answer is 12.This technique dramatically improves accuracy on arithmetic, logic, and word problems.
Multi-Turn Dialogue & Role-Playing
Examples can establish a character's persona and conversational rules for a chat agent.
- Demonstrations:
User: Hello, who are you?Assistant: I am a helpful but sarcastic travel guide. Ask me about cities.User: Is Paris nice?Assistant: Only if you like breathtaking architecture, incredible food, and overwhelming romance. So, yes. - Query:
User: What about London? - Expected Output: The model adopts the established persona:
"London is great if you enjoy royal history, world-class museums, and discussing the weather every five minutes. Bring an umbrella."This is a core technique in system prompt design for creating consistent, character-driven chatbots.
Few-Shot vs. Other Prompting Paradigms
A comparison of prompting techniques based on their reliance on examples, parameter updates, and typical use cases for steering model behavior.
| Feature / Characteristic | Zero-Shot Prompting | Few-Shot Prompting | Fine-Tuning |
|---|---|---|---|
Examples Provided in Prompt | 0 | Typically 2-10 | |
Model Parameter Updates | |||
Primary Learning Mechanism | Instruction Following | Pattern Inference from Context | Gradient-Based Weight Adjustment |
Typical Context Window Usage | Low (< 10%) | Moderate to High (10-60%) | N/A (Occurs pre-inference) |
Task Adaptation Speed | < 1 sec | < 1 sec | Hours to Days |
Specialized Compute Required | |||
Optimal For | Well-defined, simple tasks | Tasks requiring pattern demonstration | High-volume, domain-specific tasks |
Risk of Demonstration Bias | |||
Ease of Iteration & A/B Testing |
Frequently Asked Questions
Few-shot prompting is a core technique in in-context learning, where a language model is conditioned to perform a new task by providing a small number of example demonstrations within its input. This glossary answers key technical questions about its implementation, optimization, and related concepts.
Few-shot prompting is an in-context learning technique where a large language model (LLM) is given a task description and a small number of input-output examples, called demonstrations, within its prompt to guide its response for a new, similar query. The model does not update its internal weights; instead, it infers the task pattern from the provided context and applies it to the new input.
It works by structuring a prompt with:
- Instructions: High-level task description.
- Demonstrations: Typically 2 to 10 input-output pairs showing the desired format and logic.
- Query: The new, unseen input for the model to process.
The model uses the demonstrations as a conditional template, learning by example to map the query to an appropriate output format and style.
Enabling Efficiency, Speed & Accuracy
Intelligent Analysis, Decision & Execution
We build AI systems for teams that need search across company data, workflow automation across tools, or AI features inside products and internal software.
Talk to Us
Search across company data
Give teams answers from docs, tickets, runbooks, and product data with sources and permissions.
Useful when people spend too long searching or get different answers from different systems.

Automate internal workflows
Use AI to route work, draft outputs, trigger actions, and keep approvals and logs in place.
Useful when repetitive work moves across multiple tools and teams.

Add AI to products and internal tools
Build assistants, guided actions, or decision support into the software your team or customers already use.
Useful when AI needs to be part of the product, not a separate tool.
Related Terms
Few-shot prompting is a core technique within the broader paradigm of in-context learning. These related terms define the specific strategies and concepts used to optimize the selection, ordering, and presentation of demonstrations.
In-Context Learning (ICL)
In-context learning is the overarching prompting paradigm where a large language model performs a new task by conditioning its response on a few provided input-output examples, called demonstrations, without updating its internal parameters. Few-shot prompting is the primary method for implementing ICL.
- Core Mechanism: The model infers the task pattern from the examples within its context window.
- Parameter-Free: Distinct from fine-tuning, as the model's weights remain frozen.
- Foundation: Enables rapid adaptation to new tasks with minimal data.
Demonstration Selection
Demonstration selection is the critical process of strategically choosing which few-shot examples to include in a prompt to maximize a model's performance. It moves beyond random selection to a principled search for the most informative examples.
- Key Criteria: Selection is typically based on demonstration relevance (semantic similarity to the query) and demonstration diversity (covering varied aspects of the task).
- Methods: Includes embedding-based selection using vector similarity and heuristic scoring.
- Goal: To achieve strong task-example alignment and improve in-context learning generalization.
Demonstration Ordering
Demonstration ordering is the strategic arrangement of the sequence of few-shot examples within a prompt. Research shows that the order can significantly influence a model's reasoning and output, making it a key optimization lever.
- Impact: Affects how the model attends to and weights information from different examples.
- Common Strategies: Ordering by similarity to the query (closest first or last) or by complexity (simplest to hardest).
- Consideration: Interacts with instruction-example interplay to shape the final model response.
Retrieval-Augmented ICL (RA-ICL)
Retrieval-augmented in-context learning is an advanced technique that dynamically retrieves the most relevant few-shot examples from a large corpus in real-time, based on the input query. This is the engine behind a dynamic demonstration retrieval pipeline.
- Process: For each query, a system performs a semantic search over a database of candidate examples.
- Advantage: Moves beyond a static, one-size-fits-all prompt to a context-aware, adaptive system.
- Infrastructure: Relies on embedding models and vector databases for low-latency retrieval.
Optimal K (Few-Shot K)
Optimal K, or Few-Shot K, refers to the ideal number of demonstrations to include in a prompt that maximizes task performance for a given model and task. It represents a trade-off in context window optimization.
- Trade-off: More examples provide more signal but consume the limited context window, potentially crowding out the query or instructions.
- Variable: The optimal K is not universal; it depends on model size, task complexity, and example length (token-efficient demonstrations are valuable here).
- Finding K: Determined empirically through ablation studies measuring an ICL performance metric like accuracy.
Demonstration Formatting
Demonstration formatting refers to the specific syntactic structure, delimiters, and visual layout used to present input-output pairs within a prompt. Clear formatting is essential for the model to correctly parse and learn from the examples.
- Elements: Includes the use of labels (e.g.,
Input:,Output:), separators (e.g.,---), line breaks, and consistent indentation. - Purpose: Reduces ambiguity, clearly distinguishes examples from instructions and the target query, and guides the model's output structure.
- Connection: Directly enables structured output generation by providing a template for the model to follow.

About the author
Prasad Kumkar
CEO & MD, Inference Systems
Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.
His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.
Partnered with leading AI, data, and software stack.
How We Work
Custom AI workflows for your Business
One-fit-all AI don't work for modern businesses. At Inferensys, we aim to understand your business & custom requirements; which we use to define most efficient agentic workflows, the data, and the tools for your business.
01
Review the use case
We understand the task, the users, and where AI can actually help.
Read more02
Pick the right approach
We define what needs search, automation, or product integration.
Read more03
Build the first useful version
We implement the part that proves the value first.
Read more04
Improve from there
We add the checks and visibility needed to keep it useful.
Read moreThe first call is a practical review of your use case and the right next step.
Talk to Us