Few-Shot Prompting (In-Context Learning)
A prompt engineering methodology where the model adapts to a specific format, style, or logic during inference (In-Context Learning) by showing 2–5 reference examples.
1. Concept Overview & Systemic Problem
One of the most common challenges when working with language models is the ambiguity of natural language. When a developer writes in the instruction: “Write a concise TypeScript interface in the style of our project,” the model must rely on averaged representations from the internet:
- Subjectivity of Formulations: Terms like “concise,” “nice,” or “strict” are interpreted too broadly by the model, leading to instability in results from run to run.
- Inability to Describe All Rules Textually: Describing the conventions of a codebase can take 10 pages of rules that the model will inevitably begin to violate.
- “Better to See Once”: Just like a human programmer, a language model finds it significantly easier to replicate a style from 2–4 concrete examples than to parse abstract descriptions.
Few-Shot prompting eliminates ambiguity through demonstration: you show the model 2–4 contrasting pairs of “Input -> Reference Output,” establishing a clear canon of behavior.
2. Architectural Taxonomy & Mental Model
Gradation of learning methods in context (In-Context Learning):
- 1. Zero-Shot: The model receives a purely textual instruction and a new task. Suitable for basic routine tasks but yields the highest variability in output.
- 2. One-Shot: One ideal example is added to the prompt. This is sufficient to set a syntactic framework (e.g., the shape of the output JSON).
- 3. Few-Shot (Classic Set of 2–5 Examples): The golden balance. Includes samples from various scenarios: a simple case, a complex case with nested data, and one edge case (Edge Case / Error Handling).
- 4. Retrieval-Augmented (Dynamic) Few-Shot: An advanced production pattern: a library of 500 examples is indexed in a vector database. For each user query, vector search pulls the top 3 most relevant samples.
3. Technical Pipeline & Internal Mechanics
Lifecycle of preparing and executing a Few-Shot prompt:
- Exemplar Selection & Curation: A compact set of pairs is formed, demonstrating the desired result without errors and unnecessary verbosity.
- Structural Delimitation:
Examples are wrapped in strict XML or Markdown tags to avoid mixing instructions with examples:
<example id="1"><input>...</input><output>...</output></example>. - Induction Head Pattern Activation:
During the attention layer calculations, the transformer captures the transition from the
<input>tag to the<output>tag as a deterministic data transformation. - Target Inference Alignment: The model generates a response to a new query, subconsciously copying the syntax, length, naming conventions, and keys used in the samples.
4. Production Engineering Scenarios
01. Strict Parsing of Unstructured Data into JSON
You need to convert textual descriptions of bank transactions into valid JSON. Zero-shot often confuses date fields or copies cents. Few-shot with three samples (including refunds and multi-currency payments) ensures 99.8% schema stability.
02. Adhering to Company Code Style
The team requires the code agent to write unit tests strictly according to internal standards: using describe.concurrent, a specific user factory helper makeUser(), and prohibiting mocks via jest.spyOn. Two examples in the system prompt completely resolve the issue.
03. Canonization and Translation of Technical Terms
Translating documentation into Ukrainian while requiring the preservation of specific terms in their original form (e.g., Checkpointer, Race condition) and translating others according to the corporate glossary.
5. Pitfalls, Common Mistakes & Security
- Majority & Recency Bias: Models are extremely sensitive to the order of examples. If 3 out of 4 examples resulted in the answer “Yes,” the model will have a strong bias towards “Yes” for any new input. Always balance classes in examples (50/50).
- Error Mimicry: If one of the samples accidentally omitted a closing bracket or contained a spelling mistake, the model will reproduce this error in new results.
- Inflating Input Context: Ten detailed Few-shot examples can consume 15,000 tokens. Always use Prompt Caching for system prompts with examples.
FAQ: Few-Shot Prompting (In-Context Learning)
Related terms
Prompt Engineering (Context Architecture & Prompt Engineering)
An engineering discipline focused on structuring system directives, XML markup, semantic delimiters, and examples to achieve deterministic, predictable outcomes from probabilistic models.
System Prompt (System Instructions & Metaprompting)
The primary metacontext block of instructions passed at the zero position of the context window, defining the agent's role, safety rules, available tools, and behavioral boundaries.
Chain of Thought (CoT)
A methodology that prompts a language model to generate sequential intermediate reasoning steps before producing a final answer, converting additional tokens (Test-Time Compute) into quality and accuracy of the output.
Markdown AST for Agents (Abstract Syntax Tree)
A hierarchical tree-like representation of Markdown markup (mdast / Unified.js) that enables software systems and AI agents to deterministically analyze, transform, and safely edit technical content without fragile regular expressions.
Related Ecosystem Resources
Explore practical workflows, core terminology, and engineering tools
In-Context Learning
An astonishing emergent property of large language models that allows them to instantly grasp new rules, unknown words, or unique formats directly from the ongoing conversation text without altering neural network weights or programming.
Zero-Shot vs. Few-Shot (The Power of Examples)
A comparison of two fundamental prompting techniques. Zero-Shot involves a direct query without examples, while Few-Shot provides the model with several samples of the desired response before executing the task for precise calibration of format and tone.
Loop Engineering: Architecture of Autonomous Agentic Loops and the Post-Prompting Era
A deep dive into Loop Engineering: self-sustaining execution loops for Claude Code and autonomous agents, the DOER/CHECKER topology, deterministic verification, the /goal primitive, and reward hacking mitigation.
Hermes Profile: Safe Agent Creator & Context Engineering
Specialized Hermes profile for safe agent design and AGENTS.md operational contracts via read-only auditing and context engineering.
OpenHuman Project Gains Over 33,000 Stars on GitHub in Less Than a Month, Driven by New Super Context Feature 🐸
OpenHuman, a new open-source AI assistant, is rapidly gaining popularity on GitHub thanks to its unique Super Context feature.
OpenAI Offers Free Resets to Subscribers
OpenAI is providing free resets to all subscribers daily until access to Astra is available.