> ## Documentation Index
> Fetch the complete documentation index at: https://prompt.university/llms.txt
> Use this file to discover all available pages before exploring further.

# Lesson 1.3: In-Context Learning Fundamentals

> Master zero-shot, one-shot, and few-shot learning techniques

<Info>
  **Duration:** 60 minutes
</Info>

## Introduction

Here's something remarkable: LLMs can learn new tasks just by seeing examples in the prompt—no training required. This "learning during inference" is called in-context learning, and it's one of the most powerful features of modern LLMs.

<Note>
  **The Big Idea:** You can teach an AI system a new task simply by showing it examples within your prompt. No model updates, no training data, no technical expertise required.
</Note>

## The Three Learning Modes

In-context learning comes in three flavors, each with different use cases and effectiveness:

<Tabs>
  <Tab title="Zero-Shot">
    **No examples provided—pure instruction following**

    Best for: Well-defined tasks, strong models, clear instructions
  </Tab>

  <Tab title="One-Shot">
    **A single demonstration shows the pattern**

    Best for: Quick adaptation, simple patterns, format clarification
  </Tab>

  <Tab title="Few-Shot">
    **Multiple demonstrations establish clear patterns**

    Best for: Complex tasks, ambiguous requirements, pattern establishment
  </Tab>
</Tabs>

## Zero-Shot Learning

### What It Is

Zero-shot learning means giving the model instructions without any examples. You rely entirely on the model's pre-trained knowledge and ability to follow directions.

### Example: Grammar Correction

```
SYSTEM: You are a helpful assistant, great at grammar correction.

USER: You will be provided with a sentence in English. 
      Output the correct sentence.

Input: She don't like going to the park.
Output:
```

**Model Output:**

```
She doesn't like going to the park.
```

### When Zero-Shot Works Best

<CardGroup cols={2}>
  <Card title="Strong Models" icon="brain">
    Modern LLMs like GPT-4 or Claude excel at zero-shot tasks
  </Card>

  <Card title="Common Tasks" icon="list-check">
    Translation, summarization, basic Q\&A
  </Card>

  <Card title="Clear Instructions" icon="message">
    When you can describe exactly what you want
  </Card>

  <Card title="Standard Formats" icon="file-lines">
    Well-known output structures
  </Card>
</CardGroup>

### Zero-Shot Examples

<AccordionGroup>
  <Accordion title="Translation">
    ```
    Translate the following English text to French:

    Text: "The weather is beautiful today."

    Translation:
    ```

    **Output:** "Le temps est magnifique aujourd'hui."
  </Accordion>

  <Accordion title="Sentiment Analysis">
    ```
    Analyze the sentiment of this review and classify it as 
    Positive, Negative, or Neutral.

    Review: "The product works well but shipping took forever."

    Sentiment:
    ```

    **Output:** Neutral
  </Accordion>

  <Accordion title="Code Generation">
    ```
    Write a Python function that calculates the factorial of a number.
    Include error handling for negative numbers.

    Function:
    ```

    **Output:**

    ```python theme={null}
    def factorial(n):
        if n < 0:
            raise ValueError("Factorial not defined for negative numbers")
        if n == 0 or n == 1:
            return 1
        return n * factorial(n - 1)
    ```
  </Accordion>
</AccordionGroup>

## One-Shot Learning

### What It Is

One-shot learning provides a single example to demonstrate the desired pattern or format. This helps clarify ambiguous instructions and shows the model exactly what you want.

### Example: Grammar Correction

```
SYSTEM: You are a helpful assistant, great at grammar correction.

DEMO: 
Input: There is many reasons to celebrate.
Output: There are many reasons to celebrate.

USER: 
Input: She don't like going to the park.
Output:
```

**Model Output:**

```
She doesn't like going to the park.
```

### The Power of One Example

A single example can:

* **Clarify format:** Show exactly how output should look
* **Demonstrate style:** Establish tone and structure
* **Reduce ambiguity:** Make implicit requirements explicit
* **Improve accuracy:** Guide the model toward correct patterns

### One-Shot Examples

<Tabs>
  <Tab title="Classification">
    ```
    Classify customer inquiries into categories.

    Example:
    Inquiry: "I can't log into my account"
    Category: Account Access

    Inquiry: "When will my order arrive?"
    Category:
    ```

    **Output:** Shipping
  </Tab>

  <Tab title="Data Extraction">
    ```
    Extract key information from product descriptions.

    Example:
    Description: "The TechPro X15 laptop features a 15-inch display 
    and costs $1,299."
    Extracted: {"name": "TechPro X15", "type": "laptop", 
    "screen": "15-inch", "price": "$1,299"}

    Description: "The UltraSound 5000 headphones offer noise 
    cancellation for $299."
    Extracted:
    ```

    **Output:** {`{"name": "UltraSound 5000", "type": "headphones", "feature": "noise cancellation", "price": "$299"}`}
  </Tab>

  <Tab title="Format Conversion">
    ```
    Convert casual messages to professional emails.

    Example:
    Casual: "Hey, can u send me that report?"
    Professional: "Could you please send me the report at your 
    earliest convenience?"

    Casual: "Thx for the help yesterday!"
    Professional:
    ```

    **Output:** "Thank you for your assistance yesterday."
  </Tab>
</Tabs>

## Few-Shot Learning

### What It Is

Few-shot learning provides multiple examples (typically 2-5) to establish a clear pattern. This is the most powerful form of in-context learning for complex or ambiguous tasks.

### Example: Grammar Correction

```
SYSTEM: You are a helpful assistant, great at grammar correction.

DEMO 1:
Input: There is many reasons to celebrate.
Output: There are many reasons to celebrate.

DEMO 2:
Input: Me and my friend goes to the gym.
Output: My friend and I go to the gym.

DEMO 3:
Input: The team are playing good today.
Output: The team is playing well today.

USER:
Input: She don't like going to the park.
Output:
```

**Model Output:**

```
She doesn't like going to the park.
```

### Pattern Recognition in Action

Few-shot learning excels at teaching patterns:

#### Simple Translation Pattern

```
狗 → dog
猫 → cat
鸟 → bird
马 →
```

**Output:** horse

#### Mathematical Reasoning

```
12 5 → (12 + 5)/(12 × 5) = 0.283
3 1 → (3 + 1)/(3 × 1) = 1.33
19 73 →
```

**Output:** (19 + 73)/(19 × 73) = 0.066

#### Complex Classification

```
Text: "This movie was absolutely fantastic! Best film I've seen all year."
Sentiment: Positive
Confidence: High
Reasoning: Strong positive language ("fantastic", "best")

Text: "The service was okay, nothing special."
Sentiment: Neutral
Confidence: Medium
Reasoning: Lukewarm language ("okay", "nothing special")

Text: "I'm disappointed with the quality. Expected much better."
Sentiment: Negative
Confidence: High
Reasoning: Clear negative indicators ("disappointed", "expected better")

Text: "The product works fine but shipping was slow."
Sentiment:
```

**Output:**

```
Sentiment: Neutral
Confidence: Medium
Reasoning: Mixed feedback (positive product, negative shipping)
```

## Choosing the Right Approach

<Steps>
  <Step title="Start with Zero-Shot">
    Try the simplest approach first—it often works!
  </Step>

  <Step title="Add One Example if Needed">
    If output format is unclear or results are inconsistent
  </Step>

  <Step title="Use Few-Shot for Complex Tasks">
    When patterns are subtle or requirements are ambiguous
  </Step>

  <Step title="Balance Examples vs. Context">
    More isn't always better—quality over quantity
  </Step>
</Steps>

### Decision Matrix

| Task Complexity | Model Strength | Instruction Clarity | Recommended Approach |
| --------------- | -------------- | ------------------- | -------------------- |
| Simple          | Strong         | Clear               | Zero-shot            |
| Simple          | Weak           | Clear               | One-shot             |
| Moderate        | Strong         | Unclear             | One-shot             |
| Moderate        | Weak           | Unclear             | Few-shot             |
| Complex         | Strong         | Clear               | Few-shot             |
| Complex         | Any            | Unclear             | Few-shot             |

## The Science Behind It

<Note>
  **How does this work without training?**

  During pre-training, LLMs learn broad patterns across massive datasets. In-context learning doesn't teach new knowledge—it activates existing patterns by showing the model which "pathway" to follow. Examples serve as routing signals, guiding the model toward the right type of response.
</Note>

### Key Research Findings

1. **More examples generally help** (up to a point—typically 5-10 examples)
2. **Example quality matters more than quantity**
3. **Example diversity improves generalization**
4. **Example order can affect results**
5. **Larger models benefit more from few-shot learning**

## Best Practices

<CardGroup cols={2}>
  <Card title="Diverse Examples" icon="shapes">
    Cover different aspects of the task
  </Card>

  <Card title="Clear Patterns" icon="diagram-project">
    Make the relationship between input and output obvious
  </Card>

  <Card title="Consistent Format" icon="align-left">
    Use the same structure for all examples
  </Card>

  <Card title="Representative Cases" icon="bullseye">
    Include typical scenarios, not edge cases
  </Card>
</CardGroup>

## Common Pitfalls

<Warning>
  **Avoid these mistakes:**

  1. **Contradictory examples:** Examples that suggest different patterns
  2. **Too many examples:** Overwhelming the context window
  3. **Biased examples:** All examples from one category or type
  4. **Unclear formatting:** Inconsistent structure between examples
  5. **Irrelevant examples:** Demonstrations that don't match the task
</Warning>

## Practice Exercises

<AccordionGroup>
  <Accordion title="Exercise 1: Build a Few-Shot Classifier">
    Create a few-shot prompt to classify programming questions into:

    * Syntax Error
    * Logic Error
    * Conceptual Question
    * Best Practice

    Include 3-4 diverse examples.

    <Accordion title="Sample Solution">
      ```
      Classify programming questions into categories.

      Example 1:
      Question: "Why am I getting 'undefined is not a function'?"
      Category: Syntax Error

      Example 2:
      Question: "My loop runs but gives wrong results"
      Category: Logic Error

      Example 3:
      Question: "What's the difference between let and const?"
      Category: Conceptual Question

      Example 4:
      Question: "Should I use async/await or promises?"
      Category: Best Practice

      Question: "How do I fix 'cannot read property of null'?"
      Category:
      ```
    </Accordion>
  </Accordion>

  <Accordion title="Exercise 2: Zero-Shot vs Few-Shot Comparison">
    Take this task and create both zero-shot and few-shot versions:

    **Task:** Convert technical jargon to plain English

    Compare the outputs and note differences.

    <Accordion title="Sample Solutions">
      **Zero-Shot:**

      ```
      Simplify the following technical term for a general audience:

      Term: "API endpoint"
      Simplified:
      ```

      **Few-Shot:**

      ```
      Simplify technical terms for a general audience.

      Term: "Cache"
      Simplified: Temporary storage that helps things load faster

      Term: "Bandwidth"
      Simplified: The amount of data that can be sent at once

      Term: "API endpoint"
      Simplified:
      ```
    </Accordion>
  </Accordion>

  <Accordion title="Exercise 3: Progressive Example Building">
    Start with zero-shot, then add examples one at a time for this task:

    **Task:** Extract meeting action items from notes

    Test after each addition and observe improvements.
  </Accordion>
</AccordionGroup>

## Key Takeaways

<Steps>
  <Step title="In-Context Learning is Powerful">
    Teach new tasks through examples without any training
  </Step>

  <Step title="Three Modes, Different Uses">
    Zero-shot for simple tasks, few-shot for complex ones
  </Step>

  <Step title="Quality Over Quantity">
    Well-chosen examples matter more than many examples
  </Step>

  <Step title="Experiment and Iterate">
    Start simple, add examples as needed
  </Step>
</Steps>

## Next Steps

You've learned how to teach models through examples. Next, you'll discover the core principles that make any prompt more effective.

<Card title="Continue to Lesson 1.4: Core Prompting Principles" icon="arrow-right" href="/module-1/lesson-4">
  Master the four fundamental principles of effective prompting
</Card>
