Prompt Engineering Codex Β· CH 5

Chapter 5: Few-Shot and Chain-of-Thought Prompting

⏱️ 5 min read·Updated Jul 2026

The Two Most Research-Validated Techniques

Of all the prompting techniques that have emerged since 2020, two stand out for the volume of research evidence behind them:

    • Few-shot prompting: Providing examples of desired input-output pairs before your actual request
    • Chain-of-thought (CoT) prompting: Asking the model to reason step-by-step before producing a final answer
Both techniques work reliably across model families and task types. This chapter gives you the deep understanding of why they work and the practical frameworks for applying them.

---

Few-Shot Prompting: The Science

Few-shot prompting exploits the model's in-context learning capability. Large language models can infer the pattern of a task from examples alone, without any parameter updates. When you show the model examples, you are effectively teaching it the task in the prompt.

Zero-Shot vs One-Shot vs Few-Shot

code
ZERO-SHOT (no examples):
"Classify the sentiment of this review: 'The delivery was fast but the product was disappointing.'"

ONE-SHOT (one example):
"Classify sentiment. Example:
Review: 'Amazing product, arrived quickly!'
Sentiment: POSITIVE

Now classify:
Review: 'The delivery was fast but the product was disappointing.'"

FEW-SHOT (multiple examples):
"Classify sentiment. Examples:
Review: 'Amazing product, arrived quickly!'
Sentiment: POSITIVE

Review: 'Complete waste of money. Broke after two days.'
Sentiment: NEGATIVE

Review: 'It works as described, nothing special.'
Sentiment: NEUTRAL

Now classify:
Review: 'The delivery was fast but the product was disappointing.'"

The few-shot version makes it clear what mixed sentiment means and how to handle it β€” the model can infer from the NEUTRAL example that mixed reviews do not default to NEGATIVE.

Designing Effective Examples

Principle 1: Cover edge cases, not just the easy cases. If your task has ambiguous or boundary cases, include examples that demonstrate how to handle them. Easy cases the model handles fine without examples. Edge cases are where few-shot prompting earns its value.

Principle 2: Keep examples structurally consistent. Every example should follow the exact same format. Inconsistency in example structure confuses the pattern inference.

Principle 3: Use real examples, not invented ones. When possible, use actual inputs and their correct outputs. Invented examples often contain subtle inconsistencies that the model learns from unintentionally.

Principle 4: 3-5 examples is usually optimal. More examples cost more tokens. Beyond 5-7 examples, the marginal quality improvement drops significantly. For classification tasks, aim for at least one example per class.

Few-Shot for Code Generation

``` Convert these natural language specifications to TypeScript function signatures with JSDoc comments.

EXAMPLE 1: Spec: "Takes a user ID and returns all their published posts sorted by date" Output: /* Retrieves all published posts for a user, sorted by publication date descending. @param userId - The UUID of the user @returns Promise resolving to an array of Post objects, newest first @throws DatabaseError if the query fails / async function getUserPublishedPosts(userId: string): Promise

EXAMPLE 2: Spec: "Takes an email and password, validates them, creates a user account, and returns the new user or an error"

Output: /* Creates a new user account with the provided credentials. Validates email format and password strength before creation. @param email - Valid email address for the new account @param password - Password (minimum 8 characters, at least one number) @returns Promise resolving to the created User or an AuthError */ async function createUserAccount(email: string, password: string): Promise

Now convert: Spec: "Takes a product ID and quantity, checks inventory, reserves the stock if available, and returns a reservation ID or throws if insufficient stock"

code
---

## Chain-of-Thought Prompting: The Science

Standard prompting asks the model for an answer. Chain-of-thought prompting asks the model to show its work β€” to reason through the problem before producing the answer.

This works because:
1. Intermediate reasoning steps constrain subsequent reasoning (each step narrows the space of valid next steps)
2. It prevents the model from jumping to conclusions on multi-step problems
3. It makes the model's reasoning visible, so you can catch errors mid-chain

### The Three CoT Triggers

**Trigger 1: Explicit instruction**
"Think through this step by step before giving your final answer." "Reason through this carefully before responding." "Work through this problem, showing each step."
code
**Trigger 2: Few-shot with reasoning examples**
Q: A store sells apples for $1.20 each. If I buy 7 apples and pay with a $10 bill, how much change do I get? A: Let me work through this step by step. Cost per apple: $1.20 Number of apples: 7 Total cost: $1.20 x 7 = $8.40 Payment: $10.00 Change: $10.00 - $8.40 = $1.60 Answer: $1.60 change.

Q: [your actual question]

code
**Trigger 3: Zero-shot CoT ("Let's think step by step")**
Research (Kojima et al., 2022) showed that appending "Let's think step by step" to a question significantly improves performance on reasoning tasks, even without examples. Simple but surprisingly effective.

### When Chain-of-Thought Helps Most

CoT shows the largest gains on:
- Mathematical reasoning (multi-step calculations)
- Logical reasoning (if-then chains, deduction)
- Code debugging (tracing execution paths)
- Complex planning (multi-constraint scheduling, architecture decisions)
- Ambiguous classification (tasks where the right answer depends on understanding the full context)

CoT shows minimal or no gains on:
- Simple fact retrieval
- Single-step tasks
- Tasks that are primarily pattern-matching rather than reasoning

### CoT for Code Debugging
Debug this function by tracing its execution step by step for the given input. Identify exactly where the logic goes wrong.

Function: function findSecondLargest(arr) { let largest = -Infinity; let second = -Infinity; for (let i = 0; i < arr.length; i++) { if (arr[i] > largest) { second = largest; largest = arr[i]; } else if (arr[i] > second) { second = arr[i]; } } return second; }

Input: [5, 5, 3] Expected output: 3 Actual output: -Infinity

Trace the execution step by step, then explain the bug and fix it.

code
The CoT trace reveals the bug: when the second 5 is processed, it equals largest (5), so neither branch executes. second never gets updated. The fix requires handling arr[i] === largest correctly.

---

## Combining Few-Shot and CoT: The Optimal Pattern

The strongest approach for complex tasks combines both techniques:
EXAMPLE: Input: [task input] Reasoning: [step-by-step reasoning] Output: [final answer]

EXAMPLE: Input: [task input] Reasoning: [step-by-step reasoning] Output: [final answer]

Now solve: Input: [your actual input] Reasoning: ```

By showing examples that include the reasoning chain, you teach the model both what kind of reasoning to apply and what format to produce. This is called "few-shot chain-of-thought" and is the most powerful baseline for complex, high-stakes tasks.

Finished reading this chapter?

Mark it completed to update your AI Builder skill profile score on your dashboard.

πŸ“‹

Download Printable Chapter Checklist

Get a print-friendly, formatted checklist of workspace actions to apply this chapter offline.