Few-shot Example Pairs
A text prompt with zero, one or three worked examples, and how the answer format gets steadier with more examples.
AI prompt
Clean diagram of a prompt template split into coloured blocks: an instruction block at the top reading Classify each review as positive, negative or mixed and return JSON, then three small example blocks each with an Input line and an Output line in JSON, then the new Input line and an empty Output slot, an arrow to a model output block showing neat JSON, light theme, flat modern style. Negative: gradients, clutter, text errors. Reduced motion: static diagram.About this pattern
Definition
Few-shot prompting means showing the model a few input and output pairs before the real input. Examples teach the exact format, tone and edge cases better than a description does. Zero examples works for simple tasks; one fixes the format; two or three also show how to handle variation.
When to use it
- Use two or three varied examples; include one tricky case.
- Keep format identical across examples: same labels, same field order.
- Put examples before the real input, separated clearly.
- Do not make the examples all the same, or the model will copy them.
What the controls do
- Choose zero, one or three examples and read the illustrative outputs.
Motion & timing
- The text updates instantly.
- Reduced motion: nothing animates.
Tips & variations
- Examples cost tokens in every request; keep them short.
- Check that examples do not leak private data.
Code of this live preview
<div class="bx"><div class="pre" id="pre" role="group" aria-label="Examples"><button data-p="0" aria-pressed="true">0 examples</button><button data-p="1" aria-pressed="false">1 example</button><button data-p="2" aria-pressed="false">3 examples</button></div><pre class="pm" id="pm"></pre><div class="rs"><b>Illustrative model output</b><pre id="rs"></pre></div></div>:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
button{font:inherit;color:inherit;background:none;border:0;padding:0;cursor:pointer}a{color:inherit;text-decoration:none}ul,ol,p,h4{margin:0;padding:0}.sr-only{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
.pre{display:flex;gap:5px;flex-wrap:wrap}.pre button{height:28px;padding:0 11px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:11px;cursor:pointer}
.pre button[aria-pressed=true]{background:var(--accent);color:var(--accent-ink);border-color:var(--accent)}
.bx{width:min(330px,100%);display:grid;gap:9px}.rw{display:grid;gap:3px;font-size:11.5px;color:var(--muted)}.rw b{color:var(--text);font-weight:600}input[type=range]{width:100%;accent-color:#171717}
.out{font-size:12px;color:var(--muted);line-height:1.4}.out b{color:var(--text)}
button:focus-visible,input:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.pm{margin:0;font:11.5px/1.5 ui-monospace,Menlo,monospace;background:var(--pv);border-radius:10px;padding:8px 10px;white-space:pre-wrap;max-height:130px;overflow:auto}.pm mark{background:#dfeaff;color:inherit;border-radius:3px;display:block}.pm .ex{background:#fff3cf;display:block;border-radius:3px}.rs{margin-top:6px;font-size:11.5px}.rs pre{margin:3px 0 0;font:11.5px/1.5 ui-monospace,Menlo,monospace;background:#fff;border:1px solid var(--line);border-radius:10px;padding:7px 10px;white-space:pre-wrap}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}const INS='Classify the review as positive, negative or mixed. Return JSON.',EX=[['Great battery, fast shipping.','{"label":"positive","reason":"battery, shipping"}'],['Broke after two days.','{"label":"negative","reason":"durability"}'],['Nice screen but the speaker is weak.','{"label":"mixed","reason":"screen vs speaker"}']],NEW='Looks good, though the strap feels cheap.',O=['I would say this review is mostly positive, with a small complaint about the strap quality.','{"label":"mixed"}','{"label":"mixed","reason":"look vs strap"}'],pm=document.getElementById('pm'),rs=document.getElementById('rs'),bs=[...document.querySelectorAll('#pre button')],N=[0,1,3];let p=0;
const esc=s=>s.replace(/&/g,'&').replace(/</g,'<');
function up(){let h='<mark>'+esc(INS)+'</mark>';for(let i=0;i<N[p];i++)h+='<span class="ex">Input: '+esc(EX[i][0])+'\nOutput: '+esc(EX[i][1])+'</span>';h+='Input: '+esc(NEW)+'\nOutput:';pm.innerHTML=h;rs.textContent=O[p];bs.forEach(b=>b.setAttribute('aria-pressed',String(+b.dataset.p===p)));}
bs.forEach(b=>b.addEventListener('click',()=>{p=+b.dataset.p;up();}));up();One complete HTML file. Save it and open it in a browser.
<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Few-shot Example Pairs — demo</title>
<style>
:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
html,body{height:100%}body{background:var(--bg);color:var(--text);font:15px/1.5 -apple-system,BlinkMacSystemFont,'Segoe UI',Inter,Roboto,sans-serif;-webkit-font-smoothing:antialiased}.stage{min-height:100%;display:grid;place-items:center;padding:24px}button,input,select,textarea{font-family:inherit}@media (prefers-reduced-motion:reduce){html{scroll-behavior:auto}}
button{font:inherit;color:inherit;background:none;border:0;padding:0;cursor:pointer}a{color:inherit;text-decoration:none}ul,ol,p,h4{margin:0;padding:0}.sr-only{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
.pre{display:flex;gap:5px;flex-wrap:wrap}.pre button{height:28px;padding:0 11px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:11px;cursor:pointer}
.pre button[aria-pressed=true]{background:var(--accent);color:var(--accent-ink);border-color:var(--accent)}
.bx{width:min(330px,100%);display:grid;gap:9px}.rw{display:grid;gap:3px;font-size:11.5px;color:var(--muted)}.rw b{color:var(--text);font-weight:600}input[type=range]{width:100%;accent-color:#171717}
.out{font-size:12px;color:var(--muted);line-height:1.4}.out b{color:var(--text)}
button:focus-visible,input:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.pm{margin:0;font:11.5px/1.5 ui-monospace,Menlo,monospace;background:var(--pv);border-radius:10px;padding:8px 10px;white-space:pre-wrap;max-height:130px;overflow:auto}.pm mark{background:#dfeaff;color:inherit;border-radius:3px;display:block}.pm .ex{background:#fff3cf;display:block;border-radius:3px}.rs{margin-top:6px;font-size:11.5px}.rs pre{margin:3px 0 0;font:11.5px/1.5 ui-monospace,Menlo,monospace;background:#fff;border:1px solid var(--line);border-radius:10px;padding:7px 10px;white-space:pre-wrap}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}
</style></head>
<body><div class="stage">
<div class="bx"><div class="pre" id="pre" role="group" aria-label="Examples"><button data-p="0" aria-pressed="true">0 examples</button><button data-p="1" aria-pressed="false">1 example</button><button data-p="2" aria-pressed="false">3 examples</button></div><pre class="pm" id="pm"></pre><div class="rs"><b>Illustrative model output</b><pre id="rs"></pre></div></div>
</div>
<script>
const INS='Classify the review as positive, negative or mixed. Return JSON.',EX=[['Great battery, fast shipping.','{"label":"positive","reason":"battery, shipping"}'],['Broke after two days.','{"label":"negative","reason":"durability"}'],['Nice screen but the speaker is weak.','{"label":"mixed","reason":"screen vs speaker"}']],NEW='Looks good, though the strap feels cheap.',O=['I would say this review is mostly positive, with a small complaint about the strap quality.','{"label":"mixed"}','{"label":"mixed","reason":"look vs strap"}'],pm=document.getElementById('pm'),rs=document.getElementById('rs'),bs=[...document.querySelectorAll('#pre button')],N=[0,1,3];let p=0;
const esc=s=>s.replace(/&/g,'&').replace(/</g,'<');
function up(){let h='<mark>'+esc(INS)+'</mark>';for(let i=0;i<N[p];i++)h+='<span class="ex">Input: '+esc(EX[i][0])+'\nOutput: '+esc(EX[i][1])+'</span>';h+='Input: '+esc(NEW)+'\nOutput:';pm.innerHTML=h;rs.textContent=O[p];bs.forEach(b=>b.setAttribute('aria-pressed',String(+b.dataset.p===p)));}
bs.forEach(b=>b.addEventListener('click',()=>{p=+b.dataset.p;up();}));up();
</script>
</body></html>