
September 11, 2026
11 min read
By Kokil Thapa | Last reviewed: September 2026
Your model returns confident nonsense on structured tasks. Few-Shot and Chain-of-Thought Prompting fixes that pattern by showing the model what good output looks like and asking it to reason step by step before it answers. I use both techniques daily when wiring LLM APIs into Laravel apps, support bots, and document workflows. This guide covers the mechanics, copy-paste templates, and production guardrails that actually stick after deploy.
What is Few-Shot and Chain-of-Thought Prompting?
Large language models predict the next token from context. Few-Shot and Chain-of-Thought Prompting shapes that context so the model mimics a pattern you define instead of guessing from pretraining alone.
Few-shot prompting places one or more solved examples inside the prompt. Each example shows an input and the exact output you want. The model continues the pattern on the new input.
Chain-of-thought (CoT) prompting asks the model to write intermediate reasoning steps before the final answer. On arithmetic, classification with exceptions, and policy checks, CoT often beats direct answers.
Zero-shot means instructions only—no examples. One-shot uses a single example. Few-shot uses two or more. CoT can sit on top of any of those levels.
The original chain-of-thought paper from Google Research showed large gains on math word problems when models wrote reasoning steps first. That pattern still holds on modern APIs from OpenAI and Anthropic. See the Chain-of-Thought Prompting paper (Wei et al.) for the baseline evidence.
Core terms in one line each
- Demonstration: a single input/output pair in the prompt.
- Exemplar: another name for a demonstration example.
- Scratchpad: the model’s written reasoning before the answer.
- Parse target: the structured field your app reads after the call.
How do you write effective few-shot examples for LLM prompts?
Few-shot quality beats quantity. Three sharp examples often beat ten noisy ones. Each example should teach one rule: format, edge case, or tone.
On a legal-tech portal I built, few-shot pairs taught the model how to classify user questions into booking, document, or general info buckets. The examples included one ambiguous query so the model learned to ask a clarifying question instead of guessing.
Rules for strong demonstrations
- Match production inputs in length, language, and messiness.
- Cover the hardest edge case you can afford in token budget.
- Keep labels and field names identical across all examples.
- Place the newest user input last, after all demonstrations.
- Separate examples with a clear delimiter the model can copy.
Classify the customer message. Reply with JSON only.
Example 1:
Message: "I need my marriage certificate attested by Tuesday."
Category: document_service
Confidence: high
Example 2:
Message: "How much for notary at your Lazimpat office?"
Category: pricing_inquiry
Confidence: high
Example 3:
Message: "asap help!!!"
Category: needs_clarification
Confidence: low
Now classify:
Message: "{{user_message}}"
Category:
Store exemplars in version-controlled JSON, not hard-coded strings scattered in controllers. A JSON formatter helps you validate structure before the prompt ships. Pair that with token and cost controls so few-shot growth does not blow the budget.
OpenAI’s prompt engineering guide recommends diverse, consistent examples. Anthropic’s docs stress clear delimiters and explicit output schemas. Both align with the template above.
How many shots should you use?
| Scenario | Typical shot count | Trade-off |
|---|---|---|
| Simple JSON extraction | 1–2 | Low token cost; may miss edge cases |
| Multi-label classification | 3–5 | Good accuracy; watch context window |
| Nepali + English mixed support | 4–6 | Needs dialect examples; higher cost |
| Strict schema (10+ fields) | 2–3 full + 1 partial | Full examples are expensive |
Start with two shots. Add one only when eval scores drop on a specific failure mode. That discipline keeps latency and spend predictable on API-backed features.
When should you use chain-of-thought prompting instead of zero-shot?
Reach for CoT when the task needs arithmetic, conditional logic, multi-hop lookup, or policy chains. Skip it when you need a single keyword, a boolean flag, or a short rewrite.
Zero-shot CoT is the famous “Let’s think step by step” suffix on an otherwise plain question. It helps on many models without custom examples. Few-shot CoT is stronger when your domain uses non-obvious steps.
Zero-shot CoT example
Question: A trek deposit is Rs 15,000. The client pays 40% now and the rest in two equal instalments. How much is each instalment?
Let's think step by step.
Few-shot CoT example
Compute VAT-inclusive total. Show steps, then final JSON.
Input: Subtotal Rs 10,000, VAT 13%
Steps:
1. VAT = 10000 × 0.13 = 1300
2. Total = 10000 + 1300 = 11300
Output: {"subtotal":10000,"vat":1300,"total":11300}
Input: Subtotal Rs 4,500, VAT 13%
Steps:
1. VAT = 4500 × 0.13 = 585
2. Total = 4500 + 585 = 5085
Output: {"subtotal":4500,"vat":585,"total":5085}
Input: Subtotal Rs {{amount}}, VAT 13%
Steps:
For Nepali business math, cross-check totals with your own calculator layer. CoT reduces errors but does not replace server-side validation. The EMI calculator tool on this site follows the same principle: show the steps, then verify programmatically.
Read the OpenAI prompt engineering guide for vendor-specific tips on instruction hierarchy and system messages.
How do you combine few-shot and chain-of-thought prompting in production apps?
Production means reproducible prompts, parsed outputs, fallbacks, and evals—not one-off ChatGPT sessions. I treat prompts like API contracts and store them beside the code that parses responses.
Laravel service pattern
On production Laravel 12 or 13 apps, isolate prompt assembly in a dedicated class. Pass exemplars from config. Log prompt version hashes, not raw user PII.
<?php
namespace App\Services\Ai;
class BookingIntentPromptBuilder
{
public function build(string $message, array $exemplars): string
{
$blocks = collect($exemplars)->map(function ($ex) {
return <<<TXT
Message: {$ex['message']}
Steps: {$ex['steps']}
Intent: {$ex['intent']}
TXT;
})->implode("\n\n");
return <<<PROMPT
You classify booking intents. Think step by step, then output intent on the last line.
{$blocks}
Message: {$message}
Steps:
PROMPT;
}
}
Wire this behind a queue job when latency allows. Return partial UI updates with Livewire or Alpine while the job runs. That pattern appears in booking flows like trek management platforms where misclassification sends bad supplier emails.
Parsing chain-of-thought safely
Never trust free-form reasoning for permissions or payments. Extract a fixed final field with a delimiter or JSON schema mode.
- Ask for
FINAL_ANSWER:on its own line after steps. - Use structured outputs or JSON schema when the vendor supports it.
- Reject responses missing required keys; retry with a shorter prompt.
- Cap reasoning tokens; long scratchpads cost money and leak noise.
Combine this with AI-assisted debugging when prompts drift after model updates. Log failure cases into a golden set and re-run weekly evals.
Evaluation loop
Build a CSV of inputs, expected intents, and expected numeric results. Run it in CI with automated test hooks. Track accuracy, parse failure rate, and p95 latency. Swap exemplars only when scores move the right way.
For client portals like document-sharing law platforms, wrong classification is a support ticket. Few-shot CoT plus server validation beats either technique alone.
What are common mistakes with few-shot and CoT prompting?
Most failures I see are process problems, not model problems. Teams copy a demo prompt, ship it, and skip evals.
Mistake 1: Examples that disagree with each other
If Example 1 outputs pretty JSON and Example 2 adds markdown fences, the model picks randomly. Normalize every demonstration to the same skeleton.
Mistake 2: CoT without a parse target
A wall of reasoning is hard to test. Always define the machine-readable field your app reads. Hide steps from end users if they clutter the UI.
Mistake 3: Too many shots in Nepali-English mixed chat
Bilingual support needs exemplars in both scripts. One English-only shot teaches the wrong tone for Nepali queries. Use the Nepali Unicode converter to normalise text before it hits the prompt.
Mistake 4: Ignoring token economics
Each extra exemplar repeats on every request. Cache static prefixes where vendors allow prompt caching. Read Claude API integration notes and model-specific caching docs before you scale traffic.
Align AI features with governance basics: log decisions, limit sensitive data in prompts, and document which prompt version approved a given action.
Comparison: prompting strategies at a glance
| Strategy | Best for | Token cost | Parse difficulty |
|---|---|---|---|
| Zero-shot | Simple rewrite, summaries | Low | Easy |
| Few-shot | Format-heavy extraction | Medium | Easy |
| Zero-shot CoT | Quick logic boost | Medium | Medium |
| Few-shot CoT | Domain rules + logic | High | Medium |
| RAG + few-shot CoT | Long docs, fresh facts | Highest | Harder |
When facts change often—product catalogs, fee schedules—pair retrieval with few-shot CoT. Static exemplars alone cannot know this week’s fuel price table or court fee updates. Fetch facts first, then ask the model to reason over grounded text.
For greenfield features, scope work under custom software development or extend existing apps via web development services. Prompt design belongs in the same discovery phase as schema design—not as a launch-week patch.
Related reading: prompting for image generation, AI-powered search in Laravel, content pipeline automation, and AI code review in CI. For quality gates, see testing and optimization services.
Key Takeaways
- Few-shot teaches format and domain tone; chain-of-thought improves multi-step accuracy when you need shown work.
- Use two or three tight exemplars first; add shots only when eval data proves a gap.
- Always separate reasoning steps from the parsed
FINAL_ANSWERyour code consumes. - Validate outputs server-side—CoT reduces errors but does not replace business rules.
- Version prompts, log hashes, and run golden-set evals in CI before you scale traffic.
- Combine retrieval with few-shot CoT when answers depend on data that changes weekly.
People Also Ask
What is the difference between few-shot and chain-of-thought prompting?
Few-shot prompting supplies solved examples so the model copies structure and labels. Chain-of-thought prompting adds explicit reasoning steps before the answer. You can use either alone or together; combined few-shot CoT is the strongest pattern for domain-specific logic.
How many examples do you need for few-shot prompting?
Start with two or three demonstrations that cover normal cases and one edge case. More examples help until you hit diminishing returns or token limits. Measure accuracy on a fixed eval set instead of guessing the count.
Does chain-of-thought prompting work with all LLMs?
Most modern instruction-tuned models benefit from CoT, especially on math and logic. Smaller or heavily quantised models may skip steps or hallucinate reasoning. Test your target model on real inputs before shipping.
Should users see the chain-of-thought reasoning?
Show steps when transparency builds trust—support triage or educational tools, for example. Hide them when the UI needs a short answer or when steps might leak sensitive context. Always store reasoning in logs for auditors if the decision matters.
Ship Few-Shot and Chain-of-Thought Prompting with confidence
Few-Shot and Chain-of-Thought Prompting is not a party trick. It is how you turn general models into reliable workers for classification, quoting, and document intake. Treat exemplars as config, reasoning as auditable scratch space, and final fields as API contracts validated on the server.
If you are adding LLM features to a Laravel app, WooCommerce store, or client portal, start with a small golden set and one production prompt class. Expand shots only when metrics demand it. Need help wiring prompts, evals, and fallbacks into a live system? Contact us or explore AI integration and automation services—and browse the portfolio for platforms already running structured AI workflows.
Frequently Asked Questions
0 Comments
Leave a comment
Your email is not published. Comments appear once they have been read. Sign in to have your details filled in.

