D4 · Prompt Engineering › Cluster 3 · Examples (Multishot)

3.4 · Covering Edge Cases with Examples

Examples steer the hard cases

CONCEPT Demonstrate the tricky inputs, don't just describe them

Diversity (3.2) isn't only about spreading examples across the common categories — its stated purpose is to cover edge cases. The hard, ambiguous, or malformed inputs are exactly where a zero-shot prompt drifts: how should Claude handle an empty field, a mixed signal, an out-of-scope request, a message in the wrong language? Instead of writing a paragraph of rules for each, show one example that demonstrates the desired handling. An example that resolves a tricky case is worth more than the prose trying to describe the same behavior.

🔑 Every edge case you can name is a candidate for one demonstrating example. The example encodes the rule and its exact output shape in one shot.

This is the same "show, don't tell" leverage from 3.1, aimed at the parts of the input space that are hardest to specify. Common edge cases worth an example:

Edge caseWhat the example demonstrates
Missing / empty fieldExactly what to output when data is absent (e.g. null, "N/A")
Ambiguous / mixed signalWhich label wins, or how to flag the ambiguity
Out-of-scope / refusalThe precise decline or "cannot classify" response
Malformed / noisy inputHow to still produce a clean, valid output
Boundary valuesWhere the line between two categories actually falls
Pair edge-case examples with your ordinary ones. The everyday examples lock the format; the edge-case examples teach the exceptions — together they cover the range Claude will actually see.
You still can't demonstrate every possible input. Examples steer the hard cases you anticipate; combine them with a clear instruction for the general rule so Claude can generalize to edge cases you didn't show.

DIAGRAM Examples cover the input space

flowchart TD A["Anticipate the input space"] --> C["Common cases"] A --> E["Edge cases:
empty · ambiguous · out-of-scope"] C --> EX1["🟢 Ordinary examples
lock the format"] E --> EX2["🟢 Edge-case examples
teach the exceptions"] EX1 --> SET["Combined example set"] EX2 --> SET SET --> GEN["Claude generalizes to
unseen inputs too"] classDef ok fill:#d4edda,stroke:#28a745,color:#155724; classDef gate fill:#ffe8cc,stroke:#ff9f40,color:#9a4a0a; class EX1 ok; class EX2 ok; class E gate;

CODE Ordinary + edge-case examples together

An extraction task where the edge cases — a missing price and an out-of-scope message — are demonstrated, not described. Claude learns to emit null for absent data and a fixed marker for irrelevant input, then applies both to the real request.

system = """Extract product and price from each message as JSON: {"product": ..., "price": ...}.
Handle the edge cases exactly as shown in the examples."""

user = """<examples>
  <example>
    <msg>Grabbed the Aero headphones for $199.</msg>
    <json>{"product": "Aero headphones", "price": 199}</json>
  </example>
  <example>   <!-- edge: price missing -->
    <msg>The Aero headphones look great but I forgot the price.</msg>
    <json>{"product": "Aero headphones", "price": null}</json>
  </example>
  <example>   <!-- edge: out of scope -->
    <msg>What time do you close on Sundays?</msg>
    <json>{"product": null, "price": null}</json>
  </example>
</examples>

<msg>Thinking about the Nova speaker, no idea what it costs.</msg>"""

message = client.messages.create(
    model="claude-opus-4-8",
    max_tokens=16000,
    system=system,
    messages=[{"role": "user", "content": user}],
)
# → {"product": "Nova speaker", "price": null}  — the demonstrated edge-case rule applied.

TRADE-OFFS Demonstrate vs. describe the exceptions

✅ Edge-case examples

  • Encode the rule and its output shape at once
  • Unambiguous — no room to misread a prose rule
  • Handle empty/ambiguous/out-of-scope consistently

⚠️ Prose rules only

  • Each rule is open to interpretation
  • Output shape for the exception still unspecified
  • Rules pile up and start to conflict
  • But: needed for cases you can't enumerate
Best of both: a short instruction for the general rule + demonstrating examples for the specific hard cases you know you'll hit.

EXAM TRAPS

Trap: "Examples are only for the typical case." → A core reason to keep examples diverse is to cover edge cases. Demonstrating tricky inputs is one of their most valuable uses.
Trap: "Just describe the edge-case rules in the instructions." → Prose rules are open to misreading and leave the output shape unspecified. An example demonstrating the exception is tighter and steers more reliably.
Trap: "Edge-case examples replace ordinary ones." → No — pair them. Ordinary examples lock the format; edge-case examples teach the exceptions. You need both for coverage.
Trap: "If I show enough edge cases I've covered everything." → You can't enumerate the whole input space. Combine examples with a clear general instruction so Claude generalizes to unseen cases.

PRACTICE

Q1 · Why demonstrate an edge case instead of describing it?

A demonstrating example encodes both the rule and the exact output shape for the exception in one unambiguous artifact, whereas a prose rule can be misread and leaves the output format unspecified. Examples steer the hard cases more reliably than descriptions.

Q2 · How do edge-case examples relate to the diversity guidance from 3.2?

Diversity's stated purpose is partly to cover edge cases. Adding examples for empty, ambiguous, or out-of-scope inputs is exactly how you make the set diverse in a way that improves real-world robustness, not just cosmetic variety.

Q3 · Do edge-case examples let you drop the general instruction?

No. You can't demonstrate every possible input, so keep a clear instruction for the general rule. The examples steer the specific hard cases; the instruction lets Claude generalize to edge cases you didn't show.