Few-Shot Prompting for Production Apps That Scale
Few-shot prompting means showing a model several input/output examples before asking it to handle a new case, instead of relying on instructions alone. For production apps, the question isn't whether few-shot prompting works — it almost always improves accuracy on structured tasks — but how to implement it so it stays reliable, cheap, and maintainable as your app grows past a prototype.
This article covers the practical side: how many examples to use, where to store them, how to version them, and how to avoid the token-cost and latency traps that catch teams off guard when few-shot prompts move from a notebook into a real API-backed product.
Why Few-Shot Beats Zero-Shot for Structured Tasks
Zero-shot prompting ("extract the fields from this invoice") relies entirely on the model inferring your intent from instructions. Few-shot prompting shows the model exactly what "correct" looks like:
Example input: "Invoice #4521, due 2024-03-15, total $1,240.00"
Example output: {"invoice_id": "4521", "due_date": "2024-03-15", "total": 1240.00}
Example input: "INV-9981 — Net 30, Amount: €899,50"
Example output: {"invoice_id": "9981", "due_date": null, "total": 899.50}
Now process: "Receipt #332, payable by 04/01/2024, $56.00"
This works because it removes ambiguity around format, edge cases, and output schema. For classification, extraction, tone-matching, and formatting tasks, few-shot examples consistently outperform instruction-only prompts — especially on edge cases like missing fields, unusual currencies, or non-English input.
How Many Examples Do You Actually Need?
More isn't always better. In practice:
- 2–3 examples are enough for simple classification (sentiment, intent routing).
- 4–6 examples work well for extraction tasks with multiple fields or nested structure.
- Beyond 8 examples, returns diminish fast and you're mostly paying for tokens without meaningful accuracy gains.
The bigger lever isn't quantity — it's example diversity. One example per edge case (missing field, malformed input, ambiguous category) beats five near-identical "happy path" examples. If your classifier keeps misfiring on a specific input pattern, add one example that covers it rather than doubling your total example count.
Structuring Few-Shot Prompts for Production
A few-shot prompt that works in a script often breaks in production because of three things: inconsistent formatting, example drift, and token bloat. Here's a structure that holds up:
SYSTEM: You are a support ticket classifier. Output only valid JSON
matching the schema: {"category": string, "urgency": "low"|"medium"|"high"}
USER: Classify: "My payment failed twice and I'm locked out of my account"
ASSISTANT: {"category": "billing", "urgency": "high"}
USER: Classify: "Can you add dark mode?"
ASSISTANT: {"category": "feature_request", "urgency": "low"}
USER: Classify: "{{actual_ticket_text}}"
Keep examples in the actual conversation format (alternating user/assistant turns) rather than jamming them into a single system string. This matches how the model was trained to use few-shot context and tends to produce more consistent outputs than a wall of text in the system prompt.
Versioning Your Examples
Treat your few-shot examples like code, not like prompt copy you tweak in a dashboard:
- Store them in a file (JSON or YAML) checked into your repo, not hardcoded inline in multiple places.
- Tag each example set with a version number and log which version produced which output, so you can trace regressions.
- Write a small regression test suite: a fixed set of inputs with expected categories/fields that you run every time you change an example or swap models.
This matters more than it sounds. Teams that skip versioning end up with five slightly different copies of "the extraction prompt" scattered across services, and nobody can explain why outputs changed last Tuesday.
Managing Token Cost and Latency
Every few-shot example you add is tokens you pay for on every single request — not once, but every time. A 6-example extraction prompt at 150 tokens per example adds 900 tokens of overhead before the actual input even starts. At scale, that's real cost and real latency.
Three practical mitigations:
- Trim examples to the minimum that covers your edge cases. Measure accuracy with 3 examples vs. 6 before assuming more is better.
- Cache the static part of your prompt if your provider supports prompt caching — the examples don't change per request, so you shouldn't pay full price to re-send them every time.
- Monitor token usage per request type, not just in aggregate. A classifier endpoint with a bloated few-shot prompt can quietly become your most expensive route even if it's called less often than others.
If you're running this through an API layer like SubToAPI, usage metadata is returned with every response, so you can see exactly how many input tokens your few-shot examples are costing per call and catch bloat before it shows up as a surprise bill. That's useful when you have several different few-shot prompts (classification, extraction, summarization) running across different parts of the app and want one place to see which ones are token-heavy.
When to Move Past Few-Shot
Few-shot prompting is the right tool when:
- You have a stable, well-defined task (classification, extraction, format conversion).
- You can enumerate 3–8 representative examples that cover your real edge cases.
- The task doesn't require multi-step reasoning or tool use.
It's the wrong tool when:
- Your examples keep multiplying because the task is actually several different tasks disguised as one.
- You need the model to call external tools, fetch data, or chain multiple steps — that's a job for structured tool use instead of more examples. See /docs/tools for how that works.
- Accuracy plateaus below what you need even after tuning examples — at that point, consider fine-tuning or a different model.
Shipping It
Once your few-shot prompt is tested and versioned, wiring it into a production endpoint is straightforward:
const response = await fetch("https://api.subtoapi.app/v1/messages", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.SUBTOAPI_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
model: "claude-sonnet-4",
messages: [
...fewShotExamples,
{ role: "user", content: `Classify: "${ticketText}"` }
]
})
});
Keep fewShotExamples as a separate, versioned array you import — not inline string concatenation — so updating the prompt doesn't mean hunting through request-building code. Full request and response details are in /docs/messages, and /docs/quickstart walks through setup end to end if you're wiring this up for the first time.
Questions
Does few-shot prompting slow down response time? Yes, moderately — more input tokens mean slightly longer time-to-first-token, especially for streaming responses. The impact is usually small (tens to low hundreds of milliseconds) unless your example set is unusually large, but it's worth measuring if latency is critical.
Should few-shot examples be hardcoded or dynamically selected? For most apps, a fixed, versioned set of 3–6 examples is simpler and more predictable than dynamic retrieval. Dynamic example selection (picking the most relevant examples per input) helps on highly varied tasks but adds complexity and a new failure mode: picking bad examples.
Can few-shot prompting replace fine-tuning? For most classification and extraction tasks, yes — few-shot prompting gets you most of the accuracy without training infrastructure. Fine-tuning becomes worthwhile when you need lower latency, smaller token overhead per request, or accuracy beyond what examples alone can deliver.