1. What Are Zero-shot and Few-shot?
Zero-shot and Few-shot are two important in-context learning strategies (In-Context Learning, ICL) in Prompt Engineering.
They primarily guide the model to complete a specific task by adjusting the number of examples in the Prompt.
- Zero-shot: Provides no task examples and only describes the task and requirements.
- One-shot: Provides one task example.
- Few-shot: Provides a small number of task examples to help the model understand the mapping between inputs and outputs.
None of these three approaches requires updating model parameters; instead, they influence the model's output through context during inference.
1.1 Zero-shot: Zero-shot Prompting
Zero-shot means not providing the model with specific examples of inputs and target outputs, but instead describing the task directly in natural language.
For example:
Please determine the sentiment of the following comment.
Comment:
This phone runs smoothly and takes good photos.
Please choose one of the following categories:
- Positive
- Negative
- Neutral
Output only the category name.
The model needs to complete the sentiment classification directly based on the language knowledge it has already learned and the task instructions.
Its characteristics:
- No example data needs to be prepared.
- The Prompt structure is simple.
- Token consumption is relatively low.
- Suitable for common, clearly defined tasks.
- May have insufficient adaptability to complex label systems and special business rules.
1.2 One-shot: Single-example Prompting
One-shot provides one example so that the model can understand the task by referring to the example.
For example:
Please determine the sentiment of the comment.
Example:
Input:
This computer has good performance and runs very fast.
Output:
Positive
Now determine:
Input:
This phone runs smoothly and takes good photos.
Output:
The model can obtain information about the input format, output format, and task execution method from the example.
One-shot is suitable for scenarios where the task rules are relatively clear but an example is needed to help the model understand the output format.
1.3 Few-shot: Few-shot Prompting
Few-shot provides the model with a small number of input and output examples, allowing the model to learn the task pattern from the context.
For example:
Please determine the sentiment of the comment.
Example 1:
Input:
This computer has good performance and runs very fast.
Output:
Positive
Example 2:
Input:
The battery life is terrible, and the user experience is awful.
Output:
Negative
Example 3:
Input:
This phone was officially released today.
Output:
Neutral
Now determine:
Input:
This phone runs smoothly and takes good photos.
Output:
Through the three examples, the model can observe:
- The structure of the input data.
- How the classification task is judged.
- The meanings of the Positive, Negative, and Neutral labels.
- The expected output format.
Few-shot does not make the model retrain its parameters through a few examples; rather, it lets the model use these examples in the current context to complete the task.
1.4 Differences Among the Three Prompting Strategies
| Dimension | Zero-shot | One-shot | Few-shot |
|---|---|---|---|
| Number of examples | 0 | 1 | A few, usually multiple |
| Task description | Required | Required | Required |
| Input-output examples | None | 1 set | Multiple sets |
| Prompt length | Relatively short | Relatively short to medium | Usually relatively long |
| Token consumption | Relatively low | Relatively low to medium | Usually relatively high |
| Task adaptability | Depends on instruction clarity | Can refer to a single example | Can learn patterns from multiple examples |
| Applicable scenarios | Common standard tasks | Format demonstration | Special rules, complex classification |
Note that Few-shot does not necessarily perform better than Zero-shot. Example quality, task difficulty, model capability, and context length all affect the final result.
2. In-Context Learning: The Mechanism Behind It
To understand Zero-shot and Few-shot, you first need to understand In-Context Learning.
2.1 What Is In-Context Learning?
In-Context Learning, abbreviated as ICL, is commonly translated as "contextual learning."
It refers to the model's ability, during inference, to adapt to the current task pattern through the task description, examples, and contextual information provided in the Prompt.
For example, the model is originally a general-purpose language model.
Provide it with:
Input: apple
Output: fruit
Input: car
Output: transportation
Input: dog
Output:
The model might output:
animal
The model does not update its parameters through gradient descent; instead, it uses the examples in the current context to infer the mapping between inputs and outputs.
2.2 Differences Between ICL and Traditional Machine Learning Training
Traditional supervised learning usually requires preparing training data and updating model parameters through optimization algorithms.
Its process can be represented as:
flowchart TD
A["Prepare training data"]
B["Model forward pass"]
C["Calculate loss function"]
D["Backpropagation"]
E["Update model parameters"]
F["Repeat training"]
G["Obtain trained model"]
H["Input new sample"]
I["Model prediction"]
A --> B
B --> C
C --> D
D --> E
E --> F
F -->|"Continue training"| B
F -->|"Training complete"| G
G --> H
H --> I
style A fill:#DCEFE5,stroke:#4F8B70,color:#24563F
style B fill:#EAF3EC,stroke:#83A995,color:#315D49
style C fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style D fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style E fill:#EAF3EC,stroke:#83A995,color:#315D49
style F fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
style G fill:#DCEFE5,stroke:#4F8B70,color:#24563F
style H fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style I fill:#DCEFE5,stroke:#4F8B70,color:#24563F
Few-shot works differently.
Examples are fed directly into the model as context; no parameter updates are performed.
flowchart TD
A["Prepare task description"]
B["Prepare a few examples"]
C["Assemble Few-shot prompt"]
D["Tokenizer encoding"]
E["Build model context"]
F["Transformer inference"]
G["Generate results based on context"]
H["Output prediction"]
A --> C
B --> C
C --> D
D --> E
E --> F
F --> G
G --> H
style A fill:#EAF3EC,stroke:#83A995,color:#315D49
style B fill:#EAF3EC,stroke:#83A995,color:#315D49
style C fill:#DCEFE5,stroke:#4F8B70,color:#24563F
style D fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style E fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style F fill:#EAF3EC,stroke:#83A995,color:#315D49
style G fill:#EAF3EC,stroke:#83A995,color:#315D49
style H fill:#DCEFE5,stroke:#4F8B70,color:#24563F
The core differences between the two are:
| Dimension | Traditional supervised learning | In-Context Learning |
|---|---|---|
| How data is used | Training data | Prompt context |
| Parameter updates | Usually required | Not required |
| Gradient computation | Required during training | Not required during standard inference |
| Optimizer | Used during training | Not required |
| Where examples are stored | Training dataset | Current request context |
| Main role | Learn model parameters | Guide current inference behavior |
Here, “no parameter updates” refers to ICL during standard inference; it does not mean that all large-model applications involve no fine-tuning or other training processes.
2.3 Why can models complete tasks with only a few examples?
This is the most noteworthy mechanistic question about In-Context Learning.
A language model that has undergone large-scale pretraining has already learned a large amount of linguistic structure, knowledge associations, and task patterns.
When a prompt provides a few examples, the model can use its existing capabilities to identify the relationships among the examples in context.
For example:
Input: Beijing
Output: China
Input: Tokyo
Output: Japan
Input: Paris
Output:
The model may infer that the task is to establish a mapping between cities and their countries.
However, two levels need to be distinguished:
- Use of existing knowledge: The model may already know the relationships between these cities and countries.
- Recognition of the task pattern: The model infers from the examples which mapping task should be performed at present.
The effectiveness of Few-shot usually comes from a combination of these two abilities.
At present, research offers multiple explanations for the specific internal mechanisms of ICL, including implicit learning algorithms, attention patterns, and changes in the model’s internal representations. It cannot simply be assumed that the model truly learns entirely new knowledge from a few examples in the same way humans do.
III. Design Methods for Zero-shot
The focus of Zero-shot is to make the task instruction clear enough that the model can understand the task without relying on examples.
3.1 Define the Task Objective
Not recommended:
Analyze the following content.
The problem is that the task objective is unclear.
The model cannot determine whether it should perform sentiment analysis, keyword extraction, summarization, or content classification.
Recommended:
Analyze the following user review and determine its sentiment.
Choose only from the following three categories:
- Positive
- Negative
- Neutral
Output only the category name.
A clear task objective reduces the model's freedom to infer the task on its own.
3.2 Define the Input Scope
For complex tasks, you need to specify which content the model should analyze.
For example:
Please analyze the following product review.
Analysis scope:
- The user's evaluation of product performance.
- The user's evaluation of product quality.
- The user's evaluation of the product experience.
Ignore:
- Personal experiences unrelated to the product.
- Background information that cannot be judged.
The clearer the input scope, the easier it is for the model to focus on the target information.
3.3 Define the Output Format
Zero-shot does not provide examples, but you can still specify the output structure.
For example:
Please analyze the following user review.
Output JSON:
{
"sentiment": "positive, negative, or neutral",
"reason": "the basis for the judgment",
"confidence": "high, medium, or low"
}
Note that requesting JSON output in an ordinary prompt does not mean structured output constraints have been enabled at the API level.
For strict machine interfaces, you should also combine JSON Schema, Structured Outputs, or server-side validation.
3.4 Define the Judgment Criteria
For tasks with fuzzy boundaries, you should define what each label means.
For example:
Sentiment classification criteria:
Positive:
Expresses satisfaction, approval, praise, or a positive evaluation.
Negative:
Expresses dissatisfaction, complaint, criticism, or a negative evaluation.
Neutral:
Only describes facts, with no obvious positive or negative attitude.
If label boundaries are not defined, different models may adopt different judgment criteria.
3.5 Zero-shot Design Template
# Task
{{task_description}}
# Input
{{input_data}}
# Judgment rules
{{decision_rules}}
# Output requirements
{{output_format}}
# Constraints
{{constraints}}
The focus of Zero-shot optimization is not to keep adding more text, but to make the task definition, judgment criteria, and output requirements sufficiently clear.
IV. How to Design Few-shot Prompts
The core of Few-shot is not simply adding more examples, but choosing high-quality examples that represent the patterns of the task.
4.1 Example Structure
A standard Few-shot example usually contains:
- Input: the input content.
- Output: the expected output.
- Optional Explanation: the basis for the judgment.
For example:
Task: determine the sentiment of a user review.
Example 1:
Input:
This phone runs very fast, and the screen display is also very clear.
Output:
Positive
Example 2:
Input:
The phone freezes frequently, and the battery doesn't last long either.
Output:
Negative
Example 3:
Input:
This phone officially goes on sale today.
Output:
Neutral
Now process:
Input:
The phone takes great photos, but the price is a bit high.
Output:
The model can observe the task rules through the examples, without the developer having to write detailed judgment procedures for every case.
4.2 Examples Must Be Representative
If all the examples are simple, obvious cases, the model may be unable to handle complex boundary samples.
For example:
| Example | Sentiment |
|---|---|
| Very easy to use, highly recommended | Positive |
| Terrible, completely unusable | Negative |
| Officially released today | Neutral |
These examples are easy to distinguish, but real business data may contain mixed sentiment:
This phone takes great photos, but the battery life is terrible and the price is rather high.
For samples like this, the judgment rules must be clearly defined based on the task definition, for example:
- Judge the overall sentiment.
- Judge the dominant sentiment.
- Output multi-label sentiment.
- Analyze the sentiment of different aspects separately.
If the task definition is unclear, simply increasing the number of examples will not solve the problem.
4.3 Examples Should Cover Different Types
Few-shot examples should cover the main input types and key boundary cases as much as possible.
For example, for technical issue classification:
| Input Type | Example Content | Target Label |
|---|---|---|
| Database issue | MySQL queries are very slow | Database |
| Network issue | Services cannot establish a connection with each other | Network |
| Performance issue | CPU usage of the Java service is too high | Performance |
| Cache issue | Redis data is not updated in time | Cache |
| Boundary case | Database connection pool exhausted | Database / Performance |
The last example may involve both database and performance, so you need to define in advance whether this is single-label or multi-label classification.
The value of examples lies in covering the task boundaries, not just in their quantity.
4.4 Keep the Example Format Consistent
Not recommended:
Example 1:
Input: The phone is easy to use.
Result: Positive
Example 2:
Text: The battery doesn't last long.
Category: Negative
Third example:
The user says the phone goes on sale today.
Judgment result: Neutral
Although the model may understand this content, inconsistent formats increase structural differences in the context.
Recommended:
Example 1:
Input: The phone is easy to use.
Output: Positive
Example 2:
Input: The battery doesn't last long.
Output: Negative
Example 3:
Input: The phone goes on sale today.
Output: Neutral
A consistent format helps the model recognize the common structure across examples.
4.5 Example Order May Affect Results
Few-shot is affected not only by the content of the examples but also potentially by their order.
For example:
Example 1: A → X
Example 2: B → Y
Example 3: C → Z
Compared with:
Example 1: C → Z
Example 2: A → X
Example 3: B → Y
Even when the example set is exactly the same, the model's output may differ.
The reason is that autoregressive models process context as an ordered token sequence, so the position, adjacency relationships, and ending position of examples all affect subsequent computation.
In engineering practice, you can experiment with example order rather than assuming that one particular arrangement is always optimal.
4.6 More Examples Are Not Always Better
The number of few-shot examples needs to balance task performance against context cost.
Adding more examples may bring:
- More task-pattern information.
- Broader input coverage.
- Clearer label boundaries.
But it may also bring:
- Increased token consumption.
- Increased context window usage.
- More redundant information.
- Conflicts among different examples.
- Dilution of important instructions.
- Increased inference latency.
Therefore, the number of examples should be determined experimentally, rather than assuming that 3, 5, or 10 is always optimal.
5. Advanced Few-shot Strategies
In addition to providing examples directly, you can improve performance through how examples are organized, retrieved, and designed.
5.1 Example Selection: Selecting Examples
Example Selection refers to selecting the samples from an existing example library that best fit the current task.
For example, suppose the system has 10,000 labeled data items, but the context window can hold only a small number of examples.
There is no need to put all the data into the prompt; instead, select samples relevant to the current input.
Common selection methods:
| Method | Selection basis |
|---|---|
| Random Selection | Random selection |
| Similarity-based Selection | Selected based on semantic similarity |
| Diversity-based Selection | Selected based on sample diversity |
| Cluster-based Selection | Selected based on cluster distribution |
| Hybrid Selection | Combines similarity and diversity |
5.2 Similarity-based Selection: Selecting by Similarity
Suppose the current user input is:
The CPU usage of the Java service suddenly spikes.
The example library contains:
Example A: MySQL queries are very slow.
Example B: The Java service has a large amount of thread contention.
Example C: Redis cache hit rate drops.
Example D: The server is running out of disk space.
You can use embeddings to convert the current input and candidate examples into vectors, then calculate similarity.
Common cosine similarity:
The higher the similarity, the closer the two texts usually are in direction in vector space.
Example retrieval process:
flowchart TD
A["Receive current task input"]
B["Generate input embedding"]
C["Load example vector store"]
D["Calculate semantic similarity"]
E["Sort by similarity"]
F["Select Top-K examples"]
G["Build Few-shot Prompt"]
H["Call large model"]
A --> B
B --> D
C --> D
D --> E
E --> F
F --> G
G --> H
style A fill:#DCEFE5,stroke:#4F8B70,color:#24563F
style B fill:#EAF3EC,stroke:#83A995,color:#315D49
style C fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style D fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style E fill:#EAF3EC,stroke:#83A995,color:#315D49
style F fill:#EAF3EC,stroke:#83A995,color:#315D49
style G fill:#DCEFE5,stroke:#4F8B70,color:#24563F
style H fill:#DCEFE5,stroke:#4F8B70,color:#24563F
This approach allows few-shot examples to change dynamically with the user input.
But note: high semantic similarity does not necessarily mean that an example is best suited to the current task.
For example, two inputs may be semantically similar but belong to different labels or require different processing rules.
5.3 Diversity-based Selection: Diversity Selection
If you select only the examples with the highest similarity, you may end up with multiple highly similar samples.
For example:
- Java CPU usage is high.
- Java CPU usage is too high.
- Java service CPU keeps soaring.
The information covered by these three examples may be highly redundant.
Diversity selection aims for complementarity among examples.
For example:
- A performance issue.
- A database issue.
- A network issue.
- An abnormal input.
This increases the example set's coverage of different task types.
In practical systems, you can combine similarity and diversity when selecting examples.
5.4 Dynamic Few-shot: Dynamic Example Selection
Traditional Few-shot usually writes a fixed set of examples into the Prompt.
Dynamic Few-shot instead dynamically retrieves suitable samples from an example library based on the current input.
The complete process:
- The user submits a task.
- The system extracts task features.
- Retrieve candidate samples from the example library.
- Rank and filter the candidate samples.
- Build a dynamic Few-shot Prompt.
- Call the model to complete the task.
This approach is suitable for business systems with complex input types and large example libraries.
5.5 Chain-of-Thought Examples
Chain-of-Thought (CoT) is a prompting strategy that guides the model to generate intermediate reasoning steps.
Few-shot can guide the model to adopt a similar problem-solving structure by providing examples with reasoning processes.
For example:
Example:
Question:
A product has an original price of 200 yuan. What is the price after a 20% discount?
Reasoning:
A 20% discount means paying 80% of the original price.
200 × 0.8 = 160.
Answer:
160 yuan.
Now answer:
Question:
A product has an original price of 300 yuan. What is the price after a 30% discount?
This approach does not merely add input-output mappings; it adds examples of the problem-solving process.
Note the following:
- CoT examples may help the model organize complex tasks.
- Not all tasks require the reasoning process to be output.
- The explanations generated by the model do not necessarily fully reflect its actual internal computation process.
- For factual tasks, the reasoning process cannot replace external evidence and result verification.
6. Applicable Scenarios for Zero-shot and Few-shot
Neither strategy is absolutely better; the choice should be based on task characteristics.
6.1 Scenarios Suitable for Zero-shot
Common Standard Tasks
For example:
- Text summarization.
- General translation.
- Common sentiment classification.
- Information extraction.
- General question answering.
- Code explanation.
These tasks are usually highly general, and the model may be able to complete them based only on the task description.
Rapid Prototype Validation
When the task is still in an early stage and labeled examples are not ready, you can first use Zero-shot to verify whether the model has the basic capability.
Context Cost Sensitivity
When the number of requests is large and the context budget is limited, you can try Zero-shot first.
6.2 Scenarios Suitable for Few-shot
Special Business Rules
For example, internal enterprise ticket classification, business risk identification, and customer service intent classification.
These tasks may have specific label systems that general-purpose models may not accurately understand.
Non-standard Output Formats
For example, requiring the model to generate a special JSON structure, a specific field order, or internal business classification codes.
Complex Classification Boundaries
For example:
- Complaints vs. general inquiries.
- Faults vs. feature suggestions.
- Business anomalies vs. system anomalies.
- High risk vs. ordinary risk.
Examples can help the model understand label boundaries in real business scenarios.
Small-scale Labeled Data Scenarios
When only a small amount of high-quality labeled data is available, it can be used as in-context examples without immediately starting fine-tuning.