← AI/ML Resources Generative AI
Browse Topics

Few-Shot and Zero-Shot Prompting

  • Zero-shot prompting enables Large Language Models (LLMs) to perform tasks without prior examples by leveraging pre-trained knowledge.
  • Few-shot prompting improves model accuracy by providing a small set of input-output demonstrations within the prompt context.
  • These techniques shift the paradigm from traditional fine-tuning to "in-context learning," where the model adapts dynamically at inference time.
  • Effectiveness depends heavily on prompt structure, the quality of examples, and the underlying model's reasoning capabilities.

Why It Matters

01
Financial sector

In the financial sector, firms like Bloomberg utilize few-shot prompting to classify sentiment in news headlines for algorithmic trading. By providing a few examples of "hawkish" versus "dovish" central bank commentary, the model can categorize real-time market news with high accuracy, allowing traders to react to subtle changes in policy language. This approach is preferred over fine-tuning because financial language evolves rapidly, and updating a prompt is significantly faster than retraining a model.

02
Legal domain

In the legal domain, companies like Harvey AI use zero-shot and few-shot prompting to assist lawyers in contract review. A lawyer might provide a few examples of "indemnity clauses" that are considered unfavorable to their client, and then ask the model to scan a 50-page document for similar patterns. This allows legal professionals to identify risks in minutes rather than hours, leveraging the model's ability to generalize legal concepts across different jurisdictions.

03
Healthcare industry

In the healthcare industry, researchers are applying few-shot prompting to extract structured data from unstructured clinical notes. By including a few examples of how to map a doctor's narrative to standard medical codes (like ICD-10), hospitals can automate the billing and documentation process. This reduces the administrative burden on physicians, allowing them to spend more time on patient care while ensuring that medical records remain consistent and searchable.

How it Works

The Intuition of In-Context Learning

At the heart of modern Generative AI lies a shift in how we interact with models. Traditionally, if you wanted a model to perform a specific task—like classifying legal documents—you would collect a labeled dataset and perform "fine-tuning," which involves updating the model's internal weights. Few-shot and zero-shot prompting represent a departure from this. Instead of changing the model, we change the input.

Think of a zero-shot prompt as asking a well-read student a question they have never studied before. Because they have read millions of books, they can use their general knowledge to infer the answer. A few-shot prompt is like giving that same student three examples of how you want a report formatted before asking them to write one. By seeing the pattern, the student understands the "rules of the game" without needing a formal lecture.


Zero-Shot Prompting: The Power of Generalization

Zero-shot prompting occurs when you provide a task description to an LLM without any accompanying examples. The model relies entirely on its pre-trained semantic representations to understand the intent. For instance, if you input, "Classify the sentiment of the following text: 'The movie was mediocre.' Sentiment:", the model uses its internal associations between the word "mediocre" and the concept of "negative" to output the correct label.

The primary advantage here is efficiency. You do not need to curate a dataset or spend time crafting examples. However, zero-shot performance is highly sensitive to the ambiguity of the prompt. If the task is nuanced or requires a specific output format, the model may struggle because it lacks a concrete template to follow.


Few-Shot Prompting: Pattern Matching at Scale

Few-shot prompting involves including a small number of input-output pairs within the prompt. This technique exploits the model's ability to perform "pattern completion." When a model sees a sequence like Input: A, Output: B; Input: C, Output: D; Input: E, Output:, it recognizes the structural pattern and predicts the continuation (F).

This is not "learning" in the sense of updating weights; it is an emergent property of the Transformer architecture. By providing examples, you effectively "prime" the model's attention mechanism to focus on the specific features of your task. Research has shown that even providing just two or three high-quality examples can drastically reduce the error rate on complex reasoning tasks, such as logical deduction or code generation.


Edge Cases and Limitations

While powerful, these prompting strategies are not silver bullets. One major edge case is "recency bias," where models tend to prioritize the information provided at the very end of the prompt. If you provide five examples, the model might pay more attention to the fifth one than the first.

Furthermore, "label bias" can occur if your few-shot examples are unbalanced (e.g., providing four positive examples and only one negative example). The model may conclude that the output is always positive, regardless of the input. Additionally, as you increase the number of shots, you consume more of the context window. If your prompt exceeds the context limit, the model will truncate the beginning of the prompt, potentially losing the very instructions or examples you intended to guide it.

Common Pitfalls

  • "Few-shot prompting is the same as fine-tuning." Many learners confuse in-context learning with weight updates. Fine-tuning changes the model's parameters permanently, whereas few-shot prompting is a temporary, inference-time technique that disappears once the prompt window is cleared.
  • "More examples are always better." While examples help, there is a point of diminishing returns and a risk of hitting the context window limit. Adding too many examples can introduce noise or distract the model from the primary task instruction.
  • "The model 'understands' the examples like a human." LLMs do not possess human-like comprehension; they are sophisticated statistical engines. They perform few-shot tasks by identifying high-probability token continuations based on the patterns in the prompt, not by reasoning through the logic of your examples.
  • "Zero-shot is always less accurate than few-shot." While few-shot generally improves performance, for simple tasks or highly generic queries, zero-shot can be equally effective. Adding unnecessary examples to a simple task can sometimes confuse the model or lead to "overfitting" the prompt to a specific style.

Sample Code

Python
from transformers import AutoModelForCausalLM, AutoTokenizer

# Load a pre-trained model (e.g., GPT-2 for demonstration)
model_name = "gpt2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)

# Few-shot prompt construction
examples = [
    ("The stock market crashed.", "Negative"),
    ("The new product launch was a success.", "Positive")
]
query = "The company reported record-breaking losses."

# Format the prompt
prompt = ""
for input_text, label in examples:
    prompt += f"Text: {input_text}\nSentiment: {label}\n\n"
prompt += f"Text: {query}\nSentiment:"

# Tokenize and generate
inputs = tokenizer(prompt, return_tensors="pt")
output = model.generate(**inputs, max_new_tokens=5)

# Output: "Negative"
print(tokenizer.decode(output[0], skip_special_tokens=True))

Key Terms

Large Language Model (LLM)
A deep learning model trained on massive datasets to predict the next token in a sequence. These models utilize the Transformer architecture to capture long-range dependencies and semantic relationships across vast corpora of text.
In-Context Learning (ICL)
The ability of a pre-trained model to learn a new task simply by observing examples provided in the prompt. Unlike traditional machine learning, no weight updates (gradient descent) occur during this process.
Prompt Engineering
The systematic process of designing and refining input text to guide an LLM toward a desired output. It involves balancing constraints, context, and instructions to optimize model performance for specific tasks.
Inference
The stage of the machine learning lifecycle where a trained model is used to make predictions on new, unseen data. In the context of LLMs, this involves generating text based on a provided prompt without modifying the model’s internal parameters.
Token
The fundamental unit of text processed by an LLM, which can represent a word, part of a word, or a character. Models have a fixed "context window" (a maximum number of tokens) that limits how much information can be provided in a single prompt.
Hallucination
A phenomenon where an LLM generates information that is factually incorrect or nonsensical while maintaining a confident tone. This often occurs when a model lacks sufficient context or is pushed to generate content beyond its training distribution.
Context Window
The maximum number of tokens a model can consider at once during the generation process. This limit dictates the upper bound for how many examples can be included in a few-shot prompt.