DevInterviewMasterStart free →
AI & AutomationFree to read

Few-Shot & CoT

Unlock Deeper AI Reasoning with Proven Techniques

Master the three most powerful prompting paradigms: zero-shot (no examples), few-shot (learning from examples), and chain-of-thought (step-by-step reasoning). These techniques transform AI from a simple text generator into a genuine problem solver.

Zero-Shot, Few-Shot & Chain-of-Thought Explained

Three Paradigms That Define How We Prompt AI

Zero-Shot Prompting:

You give the AI a task with NO examples - just the instruction. The AI relies entirely on its pre-trained knowledge. This is what most people do by default.

Prompt: "Classify this review as positive or negative:
        'The food was amazing but delivery took 2 hours'"

AI: "Mixed/Negative - positive about food quality
     but negative about delivery experience."

Few-Shot Prompting:

You provide 2-5 examples of the input-output pattern before giving the actual task. The AI learns the pattern from your examples and applies it. Dramatically more accurate for specific tasks.

Prompt:
"Classify these Swiggy reviews:
 Review: 'Paneer was cold, never ordering again' -> Negative
 Review: 'Best biryani in Bangalore, 10/10!' -> Positive
 Review: 'Okay food, nothing special' -> Neutral

 Review: 'Dal makhani was heavenly but naan was stale' -> ?"

AI: "Mixed"
(AI learned the classification pattern from examples)

Chain-of-Thought (CoT) Prompting:

You ask the AI to think step by step before giving its final answer. This unlocks reasoning capabilities for math, logic, and complex problems that the AI would otherwise get wrong.

Without CoT:
Q: "If a shirt costs Rs 800 after 20% discount,
    what was the original price?"
AI: "Rs 960" (WRONG!)

With CoT:
Q: "Same question. Think step by step."
AI: "Step 1: After 20% discount means 80% of original
     Step 2: 80% of X = 800
     Step 3: X = 800 / 0.80 = Rs 1000" (CORRECT!)

Note: These are not just academic concepts. Few-shot and CoT are used in every production AI system. Learning them makes you dramatically better at getting results from any LLM.

Zero-Shot - When and How to Use It

Simple Tasks Where the AI Already Knows What to Do

When Zero-Shot Works Well:

  • Common tasks: Translation, summarization, general Q&A - the AI has seen millions of examples during training
  • Simple instructions: Tasks that are straightforward and well-defined
  • Exploration: When you are figuring out what you need and want to iterate quickly

Zero-Shot Enhancement Techniques:

  • Role priming: "You are an expert data analyst. Analyze this dataset..." - adding a role makes zero-shot much more effective
  • Format specification: "Respond as a JSON object with fields: summary, action_items, risk_level" - structure improves quality
  • Constraint addition: "In under 100 words, explain..." - limits force conciseness

When Zero-Shot Falls Short:

  • Custom classification categories unique to your domain
  • Specific output formats the AI has not seen before
  • Tasks requiring domain-specific judgment
  • Math and logic problems requiring multi-step reasoning

When zero-shot is not enough, upgrade to few-shot or chain-of-thought.

Note: Zero-shot is your starting point. Try it first. If the output is not good enough, add examples (few-shot) or ask for reasoning (CoT) before overcomplicating things.

Few-Shot - Teaching AI by Example

Show, Do Not Just Tell - The Power of Examples

Why Few-Shot Is So Powerful:

Humans learn by example. So do LLMs. When you show the AI 2-5 examples of the exact input-output pattern you want, it pattern-matches and applies the same logic to new inputs. This is especially powerful for:

  • Custom classification categories
  • Specific output formats
  • Domain-specific transformations
  • Tone and style matching

Best Practices for Few-Shot Examples:

  • Use 2-5 examples: More is not always better. 3 is often the sweet spot. Too many waste tokens.
  • Cover edge cases: If you have a "mixed" category, include a mixed example. If empty input is possible, show how to handle it.
  • Be consistent: All examples must follow the exact same format. Inconsistency confuses the AI.
  • Diverse examples: Do not show 3 examples of the same type. Show different variations.
  • Order matters: Put the most representative examples first. The last example has the most influence.

Few-Shot for Code Generation:

// Teaching AI your project style:

"Convert these API descriptions to TypeScript types:

Input: User has name (string), email (string), isActive (boolean)
Output: type User = { name: string; email: string; isActive: boolean; }

Input: Product has title (string), price (number), tags (string array)
Output: type Product = { title: string; price: number; tags: string[]; }

Input: Order has orderId (string), items (Product array), total (number),
       createdAt (Date)
Output: ?"

// AI generates: type Order = { orderId: string; items: Product[]; ... }

Note: Few-shot is the single most effective prompting upgrade you can make. Going from zero examples to 3 examples often doubles the accuracy of classification and formatting tasks.

Chain-of-Thought - Making AI Think Step by Step

Unlocking Reasoning by Showing the Work

What is Chain-of-Thought?

Chain-of-Thought prompting asks the AI to reason through a problem step by step before arriving at an answer. This dramatically improves performance on tasks requiring logic, math, multi-step reasoning, or complex analysis.

It is like the difference between a student who writes just the answer vs. one who shows their work. Showing the work catches errors and produces better results.

Three Ways to Trigger CoT:

  • Simple trigger: Add "Let us think step by step" or "Think through this carefully" to your prompt. Surprisingly effective!
  • Explicit steps: "First analyze X, then consider Y, then conclude Z" - guide the reasoning path
  • Few-shot CoT: Show examples WITH the reasoning process, then ask the AI to follow the same pattern

Few-Shot CoT Example (Most Powerful):

"Determine if a user should get a loan:

User: Age 28, Income 8L/yr, Debt 1L, Credit Score 750
Thinking: Income is 8L, debt-to-income ratio is 1/8 = 12.5%
which is below 30% threshold. Credit score 750 is above 700
minimum. Age 28 is within range. All criteria met.
Decision: APPROVE

User: Age 22, Income 3L/yr, Debt 4L, Credit Score 620
Thinking: Income is 3L, debt-to-income ratio is 4/3 = 133%
which exceeds 30% threshold. Credit score 620 is below 700.
Two criteria failed.
Decision: REJECT

User: Age 35, Income 12L/yr, Debt 2L, Credit Score 780
Thinking: ?"

When CoT Helps Most:

  • Math and arithmetic problems
  • Logic puzzles and reasoning tasks
  • Multi-criteria decision making
  • Code debugging (trace through the logic)
  • Complex analysis with multiple factors

Note: Chain-of-thought is the biggest breakthrough in prompting research. Just adding 'think step by step' can improve accuracy from 30% to 80% on reasoning tasks.

Combining Techniques - The Real Power

Mix and Match for Maximum Effectiveness

Technique Selection Guide:

Task TypeBest TechniqueWhy
Simple Q&A, translationZero-shotAI already knows how
Custom classificationFew-shotShow your categories
Math, logic problemsCoTNeeds step reasoning
Complex decisionsFew-shot + CoTShow pattern + reasoning
Format-sensitive outputFew-shotShow exact format
Code debuggingCoTTrace through logic

Combined Example - Bug Triage System:

"You are a bug triage system for an e-commerce app.
Classify bugs by severity and assign to the right team.

Bug: Payment fails on UPI for amounts over 1 lakh
Analysis: Payment failure affects revenue directly.
  UPI is a major payment method in India. High amount
  threshold suggests a validation bug. Critical path.
Severity: P0-Critical
Team: Payments

Bug: Profile photo upload shows wrong orientation
Analysis: Cosmetic issue. Does not affect core
  functionality. Users can still use the app normally.
Severity: P3-Low
Team: Frontend

Bug: Search returns no results for 'laptop' but
     works for 'laptops'
Analysis: ?"

// AI provides reasoned analysis, severity, and team
// Following the exact pattern from examples

Progression Strategy:

  1. Start with zero-shot
  2. If output format is wrong, add few-shot examples
  3. If reasoning is wrong, add chain-of-thought
  4. If both format and reasoning matter, combine few-shot + CoT

Note: Few-shot + CoT is the gold standard for complex tasks. You show the pattern AND the reasoning. This is how production AI systems achieve 90%+ accuracy.

Interview Questions

Q: What is the difference between zero-shot, few-shot, and chain-of-thought prompting?

Zero-shot: Give the task with no examples - rely on the AI pre-trained knowledge. Good for common tasks. Few-shot: Provide 2-5 examples of the input-output pattern before the actual task. AI learns the pattern and applies it. Best for custom formats and classification. Chain-of-thought: Ask the AI to reason step by step before answering. Dramatically improves math, logic, and complex reasoning tasks.

Q: When should you use few-shot over zero-shot?

Use few-shot when: (1) You have custom categories or formats the AI has not seen before. (2) Zero-shot output does not match your expected format. (3) The task requires domain-specific judgment. (4) You need consistent style or tone. Start with zero-shot; if the output is wrong, add 2-3 examples. Few-shot typically doubles accuracy for classification tasks.

Q: Why does chain-of-thought improve AI reasoning?

CoT forces the AI to decompose complex problems into smaller steps rather than jumping to an answer. Each step builds on the previous one, catching errors along the way. Research shows CoT improves accuracy on reasoning tasks from 30% to 80%+. It works because LLMs generate text sequentially - making them reason through intermediate steps helps them arrive at correct conclusions.

Q: What are best practices for writing few-shot examples?

Best practices: (1) 2-5 examples (3 is optimal). (2) Cover edge cases including boundary conditions. (3) Be consistent in format across all examples. (4) Use diverse examples showing different variations. (5) Order matters - last example has most influence. (6) Keep examples representative of real-world inputs.

Q: How do you combine few-shot with chain-of-thought?

Show examples that include both the reasoning process AND the final answer. Each example demonstrates: input, thinking/analysis steps, and output. The AI learns to follow the same reasoning pattern for new inputs. This is the most powerful prompting technique - it teaches both WHAT format to output and HOW to think about the problem.

Frequently Asked Questions

What is Few-Shot & CoT?

Master the three most powerful prompting paradigms: zero-shot (no examples), few-shot (learning from examples), and chain-of-thought (step-by-step reasoning). These techniques transform AI from a simple text generator into a genuine problem solver.

How does Few-Shot & CoT work?

Three Paradigms That Define How We Prompt AI Zero-Shot Prompting: You give the AI a task with NO examples - just the instruction. The AI relies entirely on its pre-trained knowledge.

Browse all AI & Automation topics →

Practice this on DevInterviewMaster

Read the full Few-Shot & CoT breakdown with interactive demos, quizzes, and Hinglish notes.

Open the interactive topic →

800+ system-design, LLD, coding, and design-pattern topics. Unlock everything with Pro (₹499, one-time) or Ultimate (₹999, one-time) — lifetime access, no subscription.