In-Context Learning
What Is In-Context Learning?
In-context learning is a large language model capability that uses instructions and examples in a prompt to perform a task without changing the model's trained weights. The model infers the requested pattern, rule, format, or category from the current conversation.
Also called ICL, it happens during inference, when a model generates an answer, rather than during training. The adaptation is temporary: the model can follow the lesson while the relevant prompt is available, but it does not permanently learn it after the session ends. A convincing answer does not mean the model has gained a lasting skill.
What Context Means for Large Language Models
For a large language model, context is the material currently available for consideration. It can include system instructions, prior messages, documents, examples, retrieved passages, and the latest user request. Models process this material as tokens, which are small pieces of text rather than whole words.
A context window is the maximum amount of tokenized material a model can consider at once. Information that fits is accessible, but not guaranteed equal attention or perfect recall. An instruction buried in a long, cluttered prompt may be missed or outweighed by conflicting material.
How In-Context Learning Works
ICL turns a prompt into a small, temporary lesson. The model identifies the task and continues the pattern it sees.
- Set one clear task, such as classifying support messages or converting notes into a JSON record.
- State the instruction, required output format, and rules that must always apply.
- Provide representative input-output examples that demonstrate the desired mapping.
- Add the new input using the same template as the examples.
- The model predicts the most likely next tokens from the instruction, examples, and new input.
- Check the result against the task rules, because the model's weights remain unchanged and no permanent training occurs.
Researchers still debate the full internal mechanism, but pretrained models can recognize a task pattern from immediate context. Examples do not install a new capability. They guide existing capabilities toward a particular response.
The Main Forms of In-Context Learning
The amount and type of guidance can vary. More context helps only when it is relevant and well organized.
| Form | What it supplies | Best use | Main trade-off |
|---|---|---|---|
| Zero-shot | An instruction only | Simple, familiar tasks | Less control over interpretation |
| One-shot | One input-output example | Showing a format or label convention | One example may be unrepresentative |
| Few-shot | Several demonstrations | Categories, transformations, and edge cases | Uses more tokens and may introduce conflicts |
| Retrieval-augmented prompting | Relevant source passages plus instructions | Questions requiring supplied facts | Quality depends on retrieval and source reliability |
A Simple In-Context Learning Example
Suppose a support team labels messages as billing, technical support, or cancellation. A prompt could show: “I was charged twice” → billing, “The app freezes after login” → technical support, and “Please close my account” → cancellation. It then asks the model to label: “I cannot use the service and want to stop paying.”
The model may select cancellation because “stop paying” and leaving the service resemble the examples. Yet the message could also involve a technical issue. Clear label definitions and examples covering mixed cases are essential. A rule could say to choose technical support when product failure is the primary problem, even if the customer mentions leaving.
In-Context Learning vs. Prompting, Fine-Tuning, and Retrieval
These approaches often work together, but they solve different problems. Choosing the right one prevents unnecessary training and unreliable automation.
| Approach | Do weights change? | Persistence | Typical purpose |
|---|---|---|---|
| General prompting | No | Current request | Ask for a response without demonstrations |
| In-context learning | No | Current context | Teach a task pattern with examples |
| Fine-tuning | Yes | Across future uses | Stable, repeated behavior at scale |
| Retrieval-augmented generation | No, usually | Depends on the source system | Bring current or private knowledge into context |
| Tool use | No, usually | Depends on the tool | Run calculations, query systems, or take actions |
Prompting is the broad practice of giving a model input. ICL is the narrower practice of using examples within that input. Retrieval supplies evidence, while tool use lets a model obtain or act on information outside its text generation.
Common Use Cases for In-Context Learning
ICL is most useful when people can show the model what a good answer looks like.
- Classifying messages, reviews, tickets, or documents into defined categories.
- Extracting fields into a fixed schema, such as names, dates, amounts, and statuses.
- Rewriting content in a chosen tone, style, length, or formatting convention.
- Applying organization-specific summarization rules to supplied text.
- Producing translations that follow a preferred terminology style.
- Generating code that follows examples, naming conventions, or an API pattern.
- Cleaning structured data when valid before-and-after examples are available.
- Answering questions from supplied source material, with validation and human oversight for high-stakes decisions.
Benefits of In-Context Learning
The primary advantage is operational flexibility, not guaranteed correctness.
- It adapts a model quickly without building a training pipeline.
- Teams can test a changed rule or format by editing the prompt.
- It supports task-specific output structures without retraining.
- It can use private or rapidly changing session data when safely provided.
- It is practical for prototypes, low-volume workflows, and evolving requirements.
- It keeps experimentation reversible, since removing examples removes the temporary behavior.
Practical Limits and Common Pitfalls
ICL is sensitive to prompt quality and should be evaluated like any production component.
- Weak, contradictory, or mislabeled examples can teach the wrong pattern.
- Examples may omit edge cases, so performance can fail on unusual inputs.
- Example order and wording can influence outputs unexpectedly.
- Ambiguous labels or unstated priorities force the model to guess.
- Models can still hallucinate facts or produce a plausible but invalid format.
- Untrusted documents may contain prompt-injection text intended to override instructions.
- Long prompts raise cost and can reduce focus. Avoid context stuffing, which adds material without a clear job.
- Confidential material in prompts requires appropriate access controls, retention policies, and vendor review.
- Outputs are not fully deterministic, so critical workflows need tests and safeguards.
How to Design Better In-Context Examples
A disciplined prompt is easier to test, maintain, and trust. This work is part of context engineering, the practice of selecting and arranging information so a model can use it reliably.
- Define one task and one decision rule for each prompt.
- Use a stable template for every demonstration and the new input.
- Select relevant, diverse, and correctly labeled examples.
- State output constraints, including allowed labels, schema, and length.
- Separate trusted instructions from source material and clearly mark each section.
- Include boundary cases that distinguish similar categories or rules.
- Test on held-out inputs not used as examples.
- Measure accuracy, format compliance, and error types before deployment.
- Keep only high-value context, prioritizing context precision over prompt length.
When to Use In-Context Learning and When to Choose Another Method
Use in-context learning for rapidly changing tasks, prototypes, small workflow adaptations, and cases where a few examples define the desired result. Use retrieval when answers depend on current, proprietary, or source-backed facts. Consider fine-tuning when behavior is stable, high-volume, and worth maintaining through training.
For calculations, strict policy enforcement, or irreversible actions, deterministic software rules and validated tools are usually safer than example-based inference. The best choice depends on error costs, the rate of change, and test results on real inputs. Evaluation, monitoring, and human review remain necessary even when examples appear to work well.
on Emergent today

