Study guides / CCAR-F / Domain 4

Prompt Engineering & Structured Output · Lesson 2 of 6

4.2 - Few-Shot Prompting

Apply few-shot prompting to improve output consistency and quality

Few-shot examples are the most effective technique for achieving consistent, well-formatted output from Claude. Not more instructions. Not confidence thresholds. Not temperature adjustments. When your output is inconsistent, few-shot examples are the first tool to reach for.

This is a direct exam principle. The exam presents scenarios where detailed instructions produce inconsistent results and tests whether you choose "add more instructions" or "add few-shot examples." The correct answer is almost always the latter.

When to Deploy Few-Shot Examples

Three specific triggers tell you few-shot examples are needed:

1. Detailed instructions alone produce inconsistent formatting. You have written a thorough prompt specifying the output format, but the model produces different structures across invocations - sometimes a bulleted list, sometimes a table, sometimes prose. More instructions will not fix this. A few examples showing the exact format you want will.

2. The model makes inconsistent judgement calls on ambiguous cases. For a code review tool, the model flags variable shadowing as "critical" in one file and "minor" in another. For a tool selection agent, it routes "check my order" to different tools depending on phrasing. These ambiguous cases need examples demonstrating the correct judgement, with reasoning.

3. Extraction tasks produce empty/null fields for information that exists in the document. The information is present but in an unexpected format - embedded in narrative text rather than a structured table, or split across multiple paragraphs. Few-shot examples showing extraction from varied document structures resolve this.

How to Construct Effective Examples

The rules are tight:

Use 2-4 targeted examples. Fewer than 2 doesn't establish a pattern. More than 4 wastes tokens without proportional benefit. Point your examples at the specific ambiguous scenarios causing problems.

Each example must show reasoning. Don't just show input-output pairs. Show why one action was chosen over plausible alternatives. That teaches the model to generalise its judgement to novel patterns, not just match the specific cases in your examples.

Example: Tool selection for "check my order #12345"
Input: "check my order #12345"
Selected tool: lookup_order
Reasoning: The user provides an order number (#12345), indicating
they want order-specific information. Even though this could be
interpreted as a general customer query, the specific order
identifier makes lookup_order the correct choice over get_customer.

Without the reasoning, the model learns only "queries mentioning order numbers go to lookup_order." With the reasoning, the model learns the general principle: specific identifiers route to specific lookup tools.

Cover the failing scenarios. If your extraction works on tables but fails on narrative text, your examples should show correct extraction from narrative text. If your code review is inconsistent on variable shadowing, your examples should classify variable shadowing scenarios at different severity levels with reasoning.

The Hallucination Reduction Effect

Few-shot examples have a useful side effect: they cut hallucination in extraction tasks. When the model sees examples of correct extraction from varied document structures - inline citations vs bibliographies, narrative descriptions vs structured tables, headers vs embedded text - it learns to handle structural variety without inventing data.

This matters most for documents with inconsistent formatting. A financial report might list expenses in a table on one page and bury them in a paragraph on the next. Without examples, the model often nails the table but returns empty fields for the narrative section, or worse, fabricates values. Show it both structures and extraction quality climbs.

Few-Shot for Reducing False Positives

In code review and analysis, few-shot examples pull double duty: they show both what to flag and what to ignore. Examples that separate acceptable code patterns from genuine issues cut false positives while still catching the real problems.

Example: Variable shadowing assessment
Code: function process(items) {
  const result = items.map(item => {
    const result = transform(item);  // shadows outer 'result'
    return result;
  });
  return result;
}
Severity: minor
Reasoning: The inner 'result' shadows the outer variable but
within a limited scope (arrow function). The code is still readable
and the shadow does not cause a bug. This is a style preference,
not a defect. Flag as minor only if style consistency is in scope.

This example teaches the model to distinguish genuine bugs from benign patterns, reducing false positives while preserving the ability to generalise to genuinely problematic shadowing cases.

Key Concept

Few-shot examples are the most effective technique for consistency. Use 2-4 targeted examples that include reasoning for decisions, not just input-output pairs. Deploy them when instructions alone produce inconsistent results, ambiguous judgements, or empty extraction fields for data that exists.

Few-Shot vs Other Techniques

The exam tests whether you can distinguish when few-shot examples are the right solution versus when another technique applies:

Problem Correct Technique
Inconsistent output formatting Few-shot examples
Malformed JSON output tool_use with JSON schemas
Fabricated values for missing fields Optional/nullable schema fields
Wrong tool selection Better tool descriptions (first), then few-shot
Model misses information in narrative text Few-shot examples showing narrative extraction
Extraction sum does not match total Validation-retry loop

Exam traps

Practice question

Your extraction pipeline correctly identifies research data in structured tables but returns empty fields when the same information appears in narrative paragraphs. Detailed instructions already specify all required fields and their formats. What should you try first?

  • A Add few-shot examples showing correct extraction from both structured tables and narrative paragraphs Correct

    Few-shot examples demonstrating correct extraction from varied document structures directly address the inconsistency. The model needs to see what correct narrative extraction looks like.

  • B Increase the model context window to process more of each document

    The model already finds data in tables - context size is not the issue. The problem is inconsistent handling of different document structures, which a larger window will not fix.

  • C Add a pre-processing step to convert all narrative text into structured tables before extraction

    This adds unnecessary infrastructure complexity. The model can handle varied formats when shown examples. Pre-processing creates maintenance burden and potential data loss.

  • D Add a post-processing retry that re-extracts any fields returned as empty

    Retrying without better guidance will produce the same empty results. The model needs examples of correct extraction from narrative text, not more attempts with the same prompt.

Build exercise: Build a Few-Shot Enhanced Extraction Prompt

Intermediate · 45 minutes

You'll practice:

  1. Create a base extraction prompt with detailed instructions but no examples and test it against 10 documents with varied structures: tables, narrative paragraphs, mixed formats

    Establishing a baseline without examples demonstrates the consistency problem the exam tests. Detailed instructions alone produce inconsistent output across varied document structures, which is the exact trigger for deploying few-shot examples.

    You should see: Inconsistent extraction results across the 10 documents: fields extracted correctly from tables but empty or wrong from narrative paragraphs, different output formats across runs, and inconsistent handling of edge cases.

    Hints
    1. Include at least 3 tables, 3 narrative paragraphs, and 4 mixed-format documents in your test set.
    2. Write thorough instructions specifying every field, its format, and where to find it. The point is that even thorough instructions alone are insufficient for consistency.
    3. const basePrompt = `Extract the following fields from the document:
      - vendor_name: The company or person issuing the document
      - document_date: Date in ISO 8601 format
      - total_amount: Numeric value without currency symbols
      - line_items: Array of {description, amount} objects
      
      Return as JSON. Ensure all fields are populated.`;
      
      // Test against varied document structures
      const testDocs = [
        { type: "table", content: "| Item | Amount |\n| Widget | 50.00 |" },
        { type: "narrative", content: "We purchased widgets for fifty pounds on March 3rd." },
        { type: "mixed", content: "Invoice #123\nItems as discussed: see below table..." }
      ];
  2. Record which fields are consistently empty or inconsistent across document structures

    Identifying the specific failure patterns tells you exactly what your few-shot examples need to demonstrate. The exam tests whether you can diagnose the problem before prescribing the solution.

    You should see: A table or log showing which fields fail on which document types. Typical pattern: dates extracted correctly from tables but missed in narrative text, amounts inconsistent when written in words rather than digits, line items empty when embedded in paragraphs.

    Hints
    1. Group results by document type and field to spot structural patterns, not random failures.
    2. Look for the three triggers: inconsistent formatting, ambiguous judgement calls, and empty fields for data that exists in the document.
    3. const failureLog = testDocs.map((doc, i) => ({
        docType: doc.type,
        vendor_name: results[i].vendor_name ? "extracted" : "EMPTY",
        document_date: results[i].document_date ? "extracted" : "EMPTY",
        total_amount: results[i].total_amount ? "extracted" : "EMPTY",
        format_consistent: checkFormatConsistency(results[i])
      }));
      
      // Identify patterns: which fields fail on which doc types?
      const failuresByType = groupBy(failureLog, "docType");
  3. Create 3 few-shot examples targeting the failing patterns - each must include reasoning explaining why the extraction was done that way

    Examples with reasoning teach the model to generalise to novel patterns, not just match specific cases. Without reasoning, the model learns only surface-level pattern matching. The exam specifically tests that reasoning-included examples outperform input-output pairs.

    You should see: Three examples, each showing a different document structure (table, narrative, mixed), with the correct extraction AND a reasoning section explaining how the data was located and why the extraction decisions were made.

    Hints
    1. Target your examples at the exact failure patterns from the previous step. If narrative date extraction fails, one example must show correct narrative date extraction.
    2. Each example needs three parts: the input document, the correct extraction output, and the reasoning explaining the extraction decisions.
    3. const fewShotExamples = [
        {
          input: "We ordered fifty units at ten pounds each on the third of March.",
          output: { vendor_name: null, document_date: "2024-03-03", total_amount: 500.00, line_items: [{ description: "units", amount: 500.00 }] },
          reasoning: "Date was written in natural language (third of March) and converted to ISO 8601. Amount was calculated from quantity (50) times unit price (10). No vendor name present in text so returned null rather than fabricating."
        }
      ];
  4. Re-run the same 10 documents with the few-shot enhanced prompt and compare: empty field rate, format consistency, and extraction accuracy

    Quantifying the improvement demonstrates the effectiveness of few-shot examples as the first-choice technique for consistency problems. The exam expects you to know that few-shot examples outperform additional instructions for this class of problem.

    You should see: A measurable reduction in empty fields (especially on narrative documents), improved format consistency across document types, and higher overall extraction accuracy. The improvement should be most dramatic on the document types that previously failed.

    Hints
    1. Compare the same three metrics across both runs: empty field rate, format consistency, and accuracy against known correct values.
    2. Focus on the document types that failed in the baseline. The improvement on those types validates that few-shot examples address structural variety.
    3. const comparison = {
        baseline: { emptyFieldRate: 0.35, formatConsistency: 0.60, accuracy: 0.72 },
        fewShot: { emptyFieldRate: 0.08, formatConsistency: 0.92, accuracy: 0.91 }
      };
      
      // Per-type comparison
      const byType = {
        table: { baseline: 0.95, fewShot: 0.97 },
        narrative: { baseline: 0.45, fewShot: 0.85 },
        mixed: { baseline: 0.60, fewShot: 0.88 }
      };
  5. Document which structural patterns benefit most from few-shot examples and which require different techniques like schema changes

    The exam tests whether you can match the right technique to the right problem. Few-shot examples fix consistency and structural variety issues, but malformed JSON needs tool_use, fabricated values need nullable schemas, and sum discrepancies need validation loops.

    You should see: A decision matrix showing which problem types improved with few-shot examples and which still need other interventions. Narrative extraction and format consistency should improve. Fabrication of missing data should not improve and needs schema changes instead.

    Hints
    1. Map each remaining problem to its correct technique: tool_use for JSON syntax, nullable fields for fabrication, validation loops for sum errors.
    2. Use the technique table from the lesson: inconsistent formatting = few-shot, malformed JSON = tool_use, fabricated values = nullable fields, wrong tool selection = better descriptions.
    3. const techniqueMappings = [
        { problem: "Inconsistent output formatting", technique: "Few-shot examples", improved: true },
        { problem: "Empty fields on narrative text", technique: "Few-shot examples", improved: true },
        { problem: "Fabricated values for missing data", technique: "Optional/nullable schema fields", improved: false },
        { problem: "Malformed JSON output", technique: "tool_use with JSON schemas", improved: false },
        { problem: "Sum does not match total", technique: "Validation-retry loop", improved: false }
      ];

Sources