In-Context Learning Demystified: Zero, One, and Few-Shot Patterns
In-context learning is arguably the most powerful capability unlocked by large language models. The ability to teach a model a new task simply by showing examples — without updating a single weight — transforms how we build AI applications. But behind this apparent simplicity lies a complex design space that separates effective systems from brittle ones.
The Prompting Spectrum
Zero-shot prompting asks the model to perform a task with no examples. The prompt relies entirely on the model's pretraining knowledge: "Classify this review as positive or negative." Performance hinges on how well the pretraining data covers the task domain. Simple tasks like sentiment analysis work reasonably well; complex reasoning tasks often fail.
One-shot prompting provides a single example before the test input. This tiny bit of context dramatically improves performance on structured tasks by establishing output format, demonstrating the expected reasoning pattern, and signaling the task domain. Formatting becomes more reliable when the model sees an example.
Few-shot prompting typically uses 3-8 examples. Performance often improves up to a point — then plateaus or degrades. The examples create a pattern that the model follows, effectively implementing a simple learning algorithm through attention mechanisms.
Building a Systematic Comparison Framework
Designing systematic comparisons requires controlling variables. Keep the prompt template identical across all conditions, use the exact same test examples, and run multiple trials to account for generation variance. The key metrics: accuracy, consistency across different seeds, and sensitivity to example ordering.
# Pseudocode for systematic comparison
configs = {
'zero_shot': {'num_examples': 0, 'template': 'zero_shot.j2'},
'one_shot': {'num_examples': 1, 'template': 'few_shot.j2'},
'few_shot_3': {'num_examples': 3, 'template': 'few_shot.j2'}
}
results = {}
for config in configs:
results[config] = evaluate(prompt_template, examples, test_set)
The Modular Prompt Library
Building a prompt library using Jinja2 templating separates content from structure. Define reusable templates for each task type, parameterize the number of examples, and maintain versioned templates. This approach makes experimentation reproducible and team collaboration smoother.
{# few_shot.j2 #}
You are a {{ role }}. Classify the following text.
{% for example in examples %}
Input: {{ example.text }}
Output: {{ example.label }}
{% endfor %}
Input: {{ test_text }}
Output:
Example Selection Strategies
Random selection is the baseline but performs poorly. k-NN selection chooses examples semantically closest to the test input using embedding similarity. Semantic similarity selection uses the same approach but with a different distance metric. Recent research shows that selecting diverse examples often outperforms simply choosing the most similar ones.
The Research Gap That Matters
The field desperately needs guidance on automated example selection. Should selection optimize for diversity, relevance, or both? Does the optimal strategy vary by task type? How many examples are truly necessary? Current approaches remain heuristic-driven. Until we have systematic benchmarking, practitioners must experiment with each new task — a costly and unscalable process.