Zero-Shot, Few-Shot & Chain-of-Thought Prompting
Table of Contents
In Prompt Engineering Fundamentals, we covered the basics of writing effective prompts. Now let’s look at three specific techniques that can dramatically improve the quality and accuracy of LLM responses: zero-shot, few-shot, and chain-of-thought prompting.
These aren’t just academic concepts — they’re practical tools you’ll use every day when working with LLMs, whether you’re building applications or just getting better answers from a chat interface.
Zero-Shot Prompting
Zero-shot means asking the model to perform a task without giving it any examples. You rely entirely on the model’s pre-trained knowledge and your instructions.
This is what most people do by default:
Classify the following customer review as "positive", "negative", or "neutral":
"The product arrived on time but the packaging was damaged.
The item itself works fine though."
The model has never seen this specific review, but it understands the concept of sentiment classification from its training data. It can apply that knowledge to new inputs without any examples — hence “zero-shot.”
When Zero-Shot Works Well
- The task is common and well-understood (summarization, translation, classification)
- The instructions are clear and specific
- The expected output format is straightforward
When Zero-Shot Falls Short
- The task requires a specific output format the model might not guess
- The classification categories are domain-specific or ambiguous
- You need consistent, structured output across many inputs
When zero-shot isn’t giving you good results, the next step is usually few-shot prompting.
Few-Shot Prompting
Few-shot prompting means including a few examples of the desired input-output behavior in your prompt. The model uses these examples to understand the pattern and apply it to new inputs.
Here’s the same classification task, but with examples:
Classify each customer review as "positive", "negative", or "neutral".
Review: "Absolutely love this product! Best purchase I've made all year."
Classification: positive
Review: "Terrible quality. Broke after two days. Want a refund."
Classification: negative
Review: "It's okay. Does what it says but nothing special."
Classification: neutral
Review: "The product arrived on time but the packaging was damaged.
The item itself works fine though."
Classification:
By showing the model three examples, you’ve established:
- The exact output format (a single word: positive, negative, or neutral)
- The classification criteria (through implicit demonstration)
- The expected tone and style of the response
The model will follow this pattern and respond with just the classification label.
How Many Examples?
There’s no magic number, but here are practical guidelines:
- 1–3 examples — Usually enough for straightforward tasks like classification, formatting, or extraction
- 3–5 examples — Better for tasks where the pattern is more nuanced or the output format is complex
- 5+ examples — Useful for highly specific or unusual tasks, but watch your token usage
Few-Shot for Code Generation
Few-shot is particularly powerful for code generation when you want a specific style or pattern:
Convert each English description to a SQL query.
Description: Get all users who signed up in 2025
SQL: SELECT * FROM users WHERE EXTRACT(YEAR FROM created_at) = 2025;
Description: Count orders by status
SQL: SELECT status, COUNT(*) FROM orders GROUP BY status;
Description: Find the top 5 customers by total spending
SQL:
The model learns your preferred SQL style (uppercase keywords, specific function choices) from the examples and applies it consistently.
Few-Shot for Data Extraction
Few-shot excels at teaching the model to extract structured data from unstructured text:
Extract the name, role, and company from each introduction.
Respond in JSON format.
Input: "Hi, I'm Sarah Chen and I work as a backend engineer at Stripe."
Output: {"name": "Sarah Chen", "role": "backend engineer", "company": "Stripe"}
Input: "My name is James. I'm the CTO of a small startup called DataFlow."
Output: {"name": "James", "role": "CTO", "company": "DataFlow"}
Input: "Hey! I'm Maria Rodriguez, senior designer over at Figma."
Output:
The examples establish the JSON structure, how to handle variations in phrasing, and what to extract.
Chain-of-Thought (CoT) Prompting
Chain-of-thought prompting asks the model to show its reasoning step by step before giving a final answer. This significantly improves accuracy on tasks that require logic, math, or multi-step reasoning.
The Problem with Direct Answers
Consider this prompt:
A store sells apples for $2 each. If you buy 5 or more, you get a 20% discount.
How much do 7 apples cost?
A model might jump straight to an answer and get it wrong. But if you ask it to think step by step:
A store sells apples for $2 each. If you buy 5 or more, you get a 20% discount.
How much do 7 apples cost?
Think through this step by step.
The model will reason through it:
1. 7 apples at $2 each = $14
2. Since 7 ≥ 5, the 20% discount applies
3. 20% of $14 = $2.80
4. $14 - $2.80 = $11.20
The 7 apples cost $11.20.
By forcing the model to show intermediate steps, each step provides context for the next, reducing errors.
Zero-Shot CoT
The simplest form of chain-of-thought is just adding “Let’s think step by step” or “Think through this step by step” to your prompt. This is called zero-shot CoT because you don’t provide example reasoning — you just ask the model to reason.
Determine the output of this code. Think through it step by step.
let x = 5;
let y = x++ + ++x;
console.log(x, y);
This simple addition consistently improves accuracy on reasoning tasks.
Few-Shot CoT
For even better results, combine few-shot examples with chain-of-thought reasoning. Show the model how to reason, not just the final answer:
Determine the time complexity of each function. Show your reasoning.
function sum(arr) {
let total = 0;
for (let i = 0; i < arr.length; i++) {
total += arr[i];
}
return total;
}
Reasoning: The function has a single loop that iterates through all n elements
of the array. Each iteration does constant work (one addition).
Time complexity: O(n)
function hasDuplicate(arr) {
for (let i = 0; i < arr.length; i++) {
for (let j = i + 1; j < arr.length; j++) {
if (arr[i] === arr[j]) return true;
}
}
return false;
}
Reasoning: The function has two nested loops. The outer loop runs n times.
For each iteration of the outer loop, the inner loop runs up to n-1 times.
This gives us roughly n × n iterations in the worst case.
Time complexity: O(n²)
function findMax(arr) {
if (arr.length === 0) return null;
let max = arr[0];
for (let i = 1; i < arr.length; i++) {
if (arr[i] > max) max = arr[i];
}
return max;
}
Reasoning:
The model sees the pattern of reasoning and applies it to the new function.
When to Use Chain-of-Thought
CoT is most valuable for:
- Math and logic problems — Anything requiring multi-step calculation
- Code analysis — Tracing execution, finding bugs, determining complexity
- Complex classification — When the answer depends on evaluating multiple factors
- Decision-making — Weighing pros and cons, comparing options
CoT is less useful (and wastes tokens) for:
- Simple factual questions (“What is the capital of France?”)
- Straightforward text generation
- Tasks where the answer is obvious
Combining Techniques
These techniques aren’t mutually exclusive. In practice, you’ll often combine them:
You are a code reviewer. Analyze the following function for bugs.
Example:
function isEven(n) {
return n % 2 === 0;
}
Analysis:
- Step 1: The function checks if n is divisible by 2 using the modulo operator.
- Step 2: This works for positive integers and zero.
- Step 3: For negative numbers, -3 % 2 returns -1 in JavaScript, and -1 !== 0,
so isEven(-3) correctly returns false.
- Verdict: No bugs. Works correctly for all integers.
Now analyze this function:
function capitalize(str) {
return str[0].toUpperCase() + str.slice(1);
}
Analysis:
This prompt uses:
- Role (“You are a code reviewer”)
- Few-shot (one example of the expected analysis)
- Chain-of-thought (step-by-step reasoning in the example)
Quick Reference
| Technique | When to Use | Token Cost | Accuracy Boost |
|---|---|---|---|
| Zero-shot | Simple, well-defined tasks | Low | Baseline |
| Few-shot | Need consistent format or domain-specific behavior | Medium | Moderate |
| Zero-shot CoT | Reasoning tasks, quick improvement | Low | Significant |
| Few-shot CoT | Complex reasoning, maximum accuracy | High | Highest |
What’s Next?
Now that you know how to structure prompts with examples and reasoning, the next step is learning how to set up persistent instructions that shape every interaction. In System Prompts & Role Design, we’ll cover how to use system messages to define the model’s behavior, personality, and constraints for your applications.