In-context learning (ICL) is a gradient-free machine learning paradigm where a pre-trained foundation model performs a new task by conditioning its output on a few input-output examples, called demonstrations, provided within the prompt. The model's internal parameters remain frozen; adaptation occurs solely through attention mechanisms over the provided context. This enables rapid, parameter-free adaptation to diverse tasks using the same base model, making it a cornerstone of prompt engineering and flexible AI application development.
Glossary
In-Context Learning (ICL)

What is In-Context Learning (ICL)?
In-context learning (ICL) is a core paradigm in modern AI where a pre-trained model learns to perform a new task from examples provided within its prompt, without any parameter updates.
ICL's effectiveness hinges on demonstration selection and example formatting. The model performs conditional generation, inferring the input-output mapping and label space from the seed examples to generalize to a new query. Performance is influenced by exemplar quality, demonstration ordering, and semantic similarity between examples and the target task. Advanced techniques like retrieval-augmented ICL dynamically fetch relevant demonstrations, optimizing the context window for each specific inference request.
Key Characteristics of In-Context Learning
In-context learning (ICL) enables a pre-trained model to perform new tasks by conditioning on examples within its prompt, without updating its internal weights. The following cards detail its defining operational mechanisms and constraints.
Parameter-Free Adaptation
In-context learning is a gradient-free method where the model's pre-trained parameters remain completely frozen. Adaptation occurs solely through the information presented in the prompt's context window. This makes ICL distinct from fine-tuning, as it:
- Requires no backpropagation or weight updates.
- Provides immediate task adaptation at inference time.
- Is inherently reversible; changing the prompt changes the behavior. This characteristic is why ICL is also described as parameter-free adaptation or frozen model inference.
Demonstration-Driven Generalization
The model infers the task by generalizing from the input-output mappings shown in the provided seed examples. It must deduce the underlying pattern, rule, or label space from these demonstrations. Key aspects include:
- Exemplar Quality: Clear, correct examples are critical for reliable generalization.
- Demonstration Diversity: A set covering varied inputs improves robustness.
- Semantic Similarity Selection: Performance often improves when demonstrations are semantically similar to the target query. The model performs conditional generation, where its output is probabilistically conditioned on this demonstrated context.
Context Window Constraints
All learning occurs within the model's fixed context window. This finite token limit imposes critical trade-offs:
- Demonstration Quantity: The number of examples (k) is limited by their length. Many-shot learning pushes this limit.
- Information Prioritization: The prompt must balance instructions, examples, and the user's query.
- Recency Bias: Models often weight later examples more heavily, making demonstration ordering a key optimization lever.
- Context Priming: Early content in the window sets a frame that biases subsequent processing. Effective context window management is essential for complex tasks.
Task Specification & Formatting
Performance hinges on how the task is communicated. This combines explicit task specification with implicit cues from example formatting.
- Instruction-Example Pairs: A natural language instruction followed by demonstrations is standard.
- Structured Demonstrations: Using tables, schemas, or consistent delimiters makes the mapping explicit.
- Structured Output Generation: Examples often showcase the desired JSON, XML, or YAML format for the model to replicate. Poor formatting can lead to misinterpretation of the label space or the required input-output mapping.
Dynamic & Retrieval-Augmented ICL
Advanced ICL systems do not use a static set of examples. Instead, they employ dynamic few-shot prompting where demonstrations are selected per query.
- Retrieval-Augmented ICL: Dynamically fetches relevant examples from a datastore for each input.
- k-NN Demonstration Retrieval / Embedding-Based Retrieval: Finds the nearest neighbors to the query in an embedding space.
- Query-Example Matching: Scores candidate examples by semantic relevance. This adaptive demonstration strategy tailors the context to the specific query, often improving performance over fixed sets.
Limits vs. Fine-Tuning
ICL is powerful but has inherent limitations compared to parameter updating methods like fine-tuning.
- Context Capacity: Limited by the model's context window, unlike fine-tuning which can leverage vast datasets.
- Inference Cost: Longer prompts with many examples increase token usage and latency.
- Task Complexity: May struggle with highly complex or nuanced tasks that require deep weight adjustment.
- Demonstration Sensitivity: Performance can vary significantly with the choice and order of examples. Thus, ICL is ideal for rapid prototyping and tasks where model weights must remain static, while fine-tuning is used for permanent, high-performance specialization.
In-Context Learning vs. Other Adaptation Methods
A technical comparison of how In-Context Learning (ICL) differs from other major approaches for adapting a pre-trained language model to a new task.
| Adaptation Feature | In-Context Learning (ICL) | Full Fine-Tuning | Parameter-Efficient Fine-Tuning (PEFT) | Prompt Tuning |
|---|---|---|---|---|
Core Mechanism | Dynamically conditions the model on demonstrations within the prompt. | Updates all model parameters via gradient descent on a task-specific dataset. | Updates a small subset of parameters (e.g., adapters, LoRA) while freezing the core model. | Learns continuous vector embeddings for the prompt while keeping the model frozen. |
Parameter Updates | ||||
Inference-Time Compute | Higher (longer context processing) | Standard | Standard | Standard |
Task-Specific Data Required | Small (0-100s of examples) | Large (1000s-1M+ examples) | Moderate (100s-10,000s examples) | Moderate (100s-10,000s examples) |
Training/Adaptation Cost | None (gradient-free) | Very High (full backpropagation) | Low to Moderate | Low |
Catastrophic Forgetting Risk | High | Low | Very Low | |
Task Switching Speed | < 1 sec (dynamic per prompt) | Minutes to Hours (reloading checkpoints) | Seconds to Minutes (switching adapters) | Seconds (switching prompt embeddings) |
Primary Use Case | Rapid prototyping, dynamic tasks, black-box models. | Maximum performance on a single, stable task. | Efficient adaptation of large models to multiple domains. | Lightweight, scalable adaptation for many tasks. |
Common Applications and Examples
In-context learning enables a frozen language model to perform new tasks by conditioning on a few examples within the prompt. Below are key applications and implementation patterns.
Text Classification & Sentiment Analysis
ICL is highly effective for classification tasks where the model must map an input to a discrete label. A typical prompt includes 3-5 examples showing the input text and its correct label (e.g., Positive, Negative, Neutral).
Key Implementation Patterns:
- Clear Delimiters: Use symbols like
###or---to separate examples. - Consistent Labeling: Always present the label in the same format (e.g.,
Sentiment: Positive). - Diverse Examples: Ensure the seed examples cover different linguistic styles and edge cases.
Example Prompt Structure:
codeClassify the sentiment of the review. Review: "The battery life is exceptional." Sentiment: Positive Review: "The interface feels clunky and outdated." Sentiment: Negative Review: "It arrived on time." Sentiment: Neutral Review: "{user_review}" Sentiment:
Information Extraction & Structured Output
This application involves prompting the model to extract specific entities or facts from unstructured text and format them into a structured schema like JSON or XML. The few-shot examples demonstrate the precise extraction and formatting rules.
Common Use Cases:
- Extracting
person,organization,dateentities from news articles. - Parsing product specifications from descriptions into key-value pairs.
- Converting free-form meeting notes into a structured agenda with
topic,owner,deadline.
Critical for Success:
- The output format must be explicitly shown in every example.
- Use placeholder values in the examples to teach the schema, not just specific data.
- For complex schemas, start with a system instruction defining the required JSON structure before the examples.
Code Generation & Translation
ICL is a cornerstone of modern AI coding assistants. By providing examples of a natural language intent paired with the corresponding correct code, the model learns the mapping for a new query.
Application Patterns:
- Syntax Translation: Convert code from one language to another (e.g., Python to JavaScript).
- Function Generation: Write a function based on a docstring and example I/O.
- Bug Fixing: Show examples of buggy code and the corrected version.
Example for a SQL Query:
codeWrite a SQL query that finds all active users. Question: Find all customers from New York. SQL: SELECT * FROM customers WHERE state = 'NY'; Question: Get the total sales for 2023. SQL: SELECT SUM(amount) FROM sales WHERE YEAR(date) = 2023; Question: {new_natural_language_query} SQL:
Performance Tip: Including comments in the example code that explain the logic can significantly improve the model's reasoning.
Question Answering & Reasoning (Chain-of-Thought)
For complex reasoning tasks, Chain-of-Thought (CoT) prompting is used within ICL. Examples show a step-by-step reasoning process leading to the final answer, teaching the model to "think aloud."
Process:
- The demonstration includes both the reasoning steps and the final answer.
- The model replicates this decomposable reasoning for the new query.
Example Format:
codeQ: A bakery sells cookies for $2 each. If they sold 15 cookies in the morning and 10 in the afternoon, how much money did they make? A: Morning revenue: 15 cookies * $2 = $30. Afternoon revenue: 10 cookies * $2 = $20. Total revenue: $30 + $20 = $50. Q: {new_multi_step_question} A:
Advanced Technique: For mathematical or logical problems, examples should use consistent notation and unit management. This technique is critical for overcoming models' tendency to guess answers without calculation.
Text Style Transfer & Rewriting
ICL can guide a model to rewrite text according to a specific style, tone, or format by showing clear before-and-after pairs.
Typical Applications:
- Formalization: Converting casual notes into professional emails.
- Simplification: Rewriting complex technical jargon for a general audience.
- Tone Adjustment: Making text more concise, persuasive, or empathetic.
Prompt Design Strategy:
- Examples should highlight the transformation rule. For instance, if the goal is conciseness, the output should be visibly shorter.
- Label the pairs clearly (e.g.,
Original:andRevised:). - If multiple styles are needed (e.g., formal vs. casual), use separate demonstration sets for each style to avoid confusing the model.
Retrieval-Augmented ICL (RA-ICL)
This advanced pattern dynamically retrieves the most relevant examples for a given query from a large datastore, rather than using a static set. It bridges semantic search with ICL.
How It Works:
- A query embedding is generated for the user's input.
- A vector database performs a nearest-neighbor (k-NN) search to find the most semantically similar
{input, output}pairs from a historical corpus. - These retrieved pairs are formatted into the few-shot prompt.
Benefits:
- Dynamically Adaptive: The context is always relevant to the specific query.
- Scalable: Can leverage thousands of potential examples without manual curation for each task.
- Performance: Often matches or exceeds the performance of carefully hand-picked static demonstrations.
Implementation Note: This requires an embedding model (e.g., text-embedding-ada-002) and a vector database (e.g., Pinecone, Weaviate) as part of the inference pipeline.
Frequently Asked Questions
In-context learning (ICL) is a core paradigm in modern AI where a pre-trained model performs a new task by conditioning on examples provided within the prompt itself, without any parameter updates. These FAQs address its mechanisms, applications, and optimization.
In-context learning (ICL) is a gradient-free learning paradigm where a pre-trained language model performs a new task by conditioning its output on a few input-output demonstrations provided within its prompt, without updating its internal weights. The model uses frozen model inference, meaning its parameters remain locked. During inference-time adaptation, the model identifies the pattern or mapping presented in the seed examples and generalizes this input-output mapping to the new query. This process is a form of parameter-free adaptation, relying entirely on the model's pre-existing knowledge and the contextual priming from the demonstrations to steer its generation.
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
In-context learning (ICL) operates within a broader ecosystem of techniques for conditioning pre-trained models. These related concepts define the mechanics, strategies, and boundaries of gradient-free task adaptation.
Few-Shot Prompting
Few-shot prompting is the practical implementation technique for ICL, where a model is given a small number of task demonstrations (typically 2-10 examples) within its input context to guide its response for a new, similar query. It is the primary mechanism by which ICL is operationalized.
- Core Mechanism: Provides explicit input-output mappings as a conditional context.
- Contrast with Zero-Shot: Explicitly provides examples, whereas zero-shot relies solely on instruction.
- Performance Curve: Accuracy generally improves with the number of examples, but with diminishing returns and context window limits.
Demonstration Selection
Demonstration selection is the strategic process of choosing which specific few-shot examples to include in a prompt to maximize a model's in-context learning performance on a target task. The choice of examples is a critical engineering decision.
- Key Criteria: Selection is based on exemplar quality, demonstration diversity, and relevance to the target query.
- Common Strategies: Include random selection, semantic similarity selection (retrieving examples closest to the query embedding), and coverage-based selection.
- Impact: Poorly chosen examples can hurt performance more than having no examples, due to misleading context priming.
Retrieval-Augmented ICL
Retrieval-augmented in-context learning (Retrieval-Augmented ICL) is an advanced technique that dynamically retrieves the most relevant task demonstrations from a large datastore to construct the few-shot prompt for each individual query. This makes ICL adaptive and scalable.
- Dynamic Context: Replaces static, hand-picked examples with a retrieval system (e.g., a vector database).
- Retrieval Method: Typically uses embedding-based retrieval or k-NN demonstration retrieval to find examples semantically similar to the query.
- Benefit: Dramatically improves performance on heterogeneous tasks by ensuring context is always relevant, approaching the quality of task-specific fine-tuning.
Inference-Time Adaptation
Inference-time adaptation is the broad category of techniques that modify a model's behavior dynamically during the forward pass (inference) based on provided context, without updating its stored weights. ICL is the most prominent example of this paradigm.
- Parameter-Free: Encompasses all gradient-free learning methods where the model's parameters remain frozen.
- Contrast with Fine-Tuning: Unlike fine-tuning, which permanently alters model weights via backpropagation, inference-time adaptation is transient and specific to a single prompt.
- Other Techniques: Includes test-time prompting, input conditioning, and certain forms of prefix tuning applied at runtime.
Instruction-Example Pair
An instruction-example pair is the fundamental compositional unit of an effective ICL prompt. It combines a natural language task specification with one or more concrete structured demonstrations of that task.
- Structure: Typically formatted as:
[Instruction]\n\nExample 1:\nInput: <x1>\nOutput: <y1>\n\nExample 2: ... - Purpose: The instruction frames the task abstractly, while the examples provide concrete input-output mapping patterns for the model to emulate.
- Design Principle: Clarity and consistency in formatting (e.g., using clear delimiters like
Input:/Output:) are essential for reliable model parsing.
Conditional Generation
Conditional generation is the core probabilistic process underlying ICL, where a language model produces an output sequence that is conditioned on both the original input query and the provided context of few-shot demonstrations. The model learns the task distribution from the context.
- Mathematical Basis: The model computes
P(output | instruction, examples, query), where the examples act as additional conditioning context that shifts the output distribution. - Mechanism: The model's attention mechanism attends to both the query and the example sequences, identifying and applying the latent pattern demonstrated.
- Contrast: Differs from unconditional generation (e.g., creative writing) or single-input conditional generation, as it relies on a multi-example context to define the task.

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