Prompt Engineering & Reasoning Frameworks
Learn reasoning techniques (CoT, Self-Consistency, Tree of Thoughts), structured JSON prompting, and Verbalized Sampling to prevent mode collapse.
Formulate task objective
Task: Fetch active users and generate database summary report.
▶📚 Prerequisites(1)
🎓 Learning objectives
- •Compare Chain-of-Thought (CoT) and Self-Consistency prompting
- •Explain Tree of Thoughts branching and evaluation
- •Design robust JSON prompts with validation schemas
- •Apply Verbalized Sampling to bypass typicality bias and restore generation diversity
What is it?
Prompt reasoning encompasses the techniques that improve LLM reasoning quality: Chain-of-Thought (CoT) prompting that elicits step-by-step reasoning, few-shot examples that demonstrate problem-solving patterns, and structured output formats that guide the model's response shape. Reasoning models like o1 and o3 internalize CoT as a training objective, producing longer but more accurate outputs.
Why it exists
Without guided reasoning, LLMs answer complex questions in one shot — they pattern-match to the most statistically likely answer rather than reasoning through the problem. On multi-step math, logic puzzles, and code debugging, this leads to subtle errors. CoT forces the model to externalize its reasoning steps, making errors visible and reducing them significantly.
Problem it solves
LLMs trained on next-token prediction develop a bias toward confident-sounding quick answers, even when the correct answer requires careful multi-step analysis. Prompting techniques counteract this: CoT adds "think step by step" to elicit reasoning traces; few-shot examples teach the format of correct reasoning; structured output prevents the model from jumping to conclusions.
Intuition
When you ask a student to "solve this math problem", they may just write an answer (often wrong). When you say "show your work step by step", they are forced to reason through each step — and in doing so, catch their own errors. CoT does the same thing for LLMs: by requiring intermediate reasoning steps, the model cannot jump to a wrong conclusion without passing through reasoning steps that reveal the error.
If you come from Java/Spring Boot: think of CoT as adding logging to your service methods. Without logging, you just see input → output (and cannot debug wrong outputs). With logging (CoT), you see every intermediate state, making errors obvious. The reasoning trace is the application log.
If you come from React/Frontend: CoT is like using React DevTools. Without it, you see the final rendered output and have to guess what happened. With it, you can trace the component tree, see intermediate state, and pinpoint where the logic went wrong. Forcing the model to reason step-by-step gives you that DevTools view.
Analogy
Chain-of-Thought is like asking a doctor to think out loud when diagnosing. "Patient has fever, cough, shortness of breath... could be flu, but the O2 saturation is 92%, which rules out simple flu... more likely pneumonia. Let me check for... yes, crackles on auscultation. Diagnosis: pneumonia." Each step of reasoning builds on the previous, making the final diagnosis far more reliable than a gut-check guess.
Technical explanation
Chain-of-Thought techniques:
-
Zero-shot CoT: append "Let's think step by step" to any question. This single phrase improves accuracy on multi-step problems by 30–40% on benchmarks like GSM8K (math word problems).
-
Few-shot CoT: include 3–8 examples showing [question, reasoning trace, answer]. The model learns the reasoning format from examples. More powerful than zero-shot but consumes context budget.
-
Self-consistency: generate 5–10 independent CoT reasoning paths, take majority vote on final answer. Reduces variance, improves accuracy on ambiguous problems. 5× more expensive but more reliable.
-
Tree-of-Thought (ToT): explore multiple reasoning branches simultaneously, evaluate each branch, prune low-quality paths. Best for problems with discrete solution space (puzzles, planning).
-
ReAct for reasoning: interleave reasoning ("I need to find the population of France") with tool calls (search_web("France population 2024")) for factual questions that require retrieval.
Structured outputs: instruct the model to respond in a specific format. Use XML tags for sections (......) — models parse and follow tag-based structures more reliably than plain instructions. JSON mode forces valid JSON output.
Reasoning models (o1, o3, claude-opus-4-5): apply CoT as a training objective — the "thinking" is internal, before the visible response. They spend compute on reasoning tokens not shown to the user. Significantly better on competition math, code, and logical reasoning.
Architecture
Prompt anatomy for complex reasoning tasks:
System prompt:
- Role definition ("You are an expert software architect")
- Reasoning instructions ("Think step by step. Check your work.")
- Output format ("Respond in this format: ......")
Few-shot examples (2–5 examples): User: [example problem] Assistant: Step 1: ... Step 2: ... Step 3: ... Therefore, the answer is X because...
Actual question: User: [real problem to solve]
Response shape: Assistant: reasoning trace final answer with justification
Workflow
- Identify reasoning failures: run the task without CoT, sample 20 outputs, categorize errors (wrong reasoning vs wrong facts vs wrong format)
- Choose CoT technique: zero-shot CoT for most cases, few-shot CoT for domain-specific reasoning patterns
- Design reasoning format: XML tags, numbered steps, or JSON — choose the format that matches how a domain expert would reason
- Collect few-shot examples: write 3–5 examples of ideal reasoning traces for your specific problem type
- Test self-consistency: if accuracy is still insufficient, generate N reasoning paths and majority-vote the answer
- Consider reasoning model: if accuracy is paramount and 5–10s latency is acceptable, use o1/o3 for the hard reasoning step
- Cache reasoning traces: for repeated similar problems, retrieve and adapt past reasoning examples via RAG instead of generating from scratch
Example
Zero-shot CoT (simplest, often enough)
response = client.messages.create( model="claude-opus-4-5", messages=[{"role": "user", "content": "A train leaves NYC at 9am going 60mph. Another leaves Boston (215 miles away) at 10am going 80mph. When do they meet? Think step by step." }] )
Few-shot CoT with structured output
SYSTEM = """Analyze code bugs using this format: Step 1: Identify what the code is trying to do Step 2: Trace the actual execution flow Step 3: Find where actual diverges from intended Step 4: Identify root cause Specific code change to fix the bug"""
Self-consistency (majority vote over 5 paths)
answers = [] for _ in range(5): response = client.messages.create(model="claude-opus-4-5", messages=[{"role": "user", "content": question + " Think step by step."}]) answers.append(extract_final_answer(response.content[0].text)) final = Counter(answers).most_common(1)[0][0] # majority vote
Real-world usage
OpenAI o3: achieves 99.5th percentile on AIME (competitive math), 87.7% on SWE-bench (real-world GitHub issues). Uses learned CoT with extensive compute-at-inference-time reasoning before generating responses.
Google AlphaCode 2: Combines CoT-style reasoning with competitive programming problem analysis. Uses 5-step reasoning: problem understanding → algorithm design → complexity analysis → implementation → testing.
Anthropic extended thinking: Claude claude-opus-4-5 with extended thinking enabled produces visible reasoning traces before final answers. Used in production by legal AI companies for contract analysis where reasoning transparency is required.
Trade-offs
Output tokens vs accuracy: CoT reasoning adds 200–2000 tokens before the final answer. At $15/1M output tokens, a 1000-token reasoning trace costs $0.015 extra per request. Worth it for high-stakes decisions; overkill for simple Q&A.
Zero-shot vs few-shot CoT: zero-shot is cheaper (no example tokens) but less reliable. Few-shot provides format examples that significantly improve consistency. The 3-example sweet spot uses ~600–900 additional tokens but meaningfully improves complex reasoning.
Reasoning models vs prompted CoT: o1/o3 and claude-opus-4-5 (extended thinking) produce better reasoning but cost 5–10× more than standard models and have 5–30s latency. Use them for problems where a wrong answer has high cost (legal, medical, financial decisions).
Visual explanation
Reasoning Paradigms: Chain-of-Thought: [Prompt] ──> [Thought 1] ──> [Thought 2] ──> [Final Answer]
Self-Consistency: [Prompt] ──┬─> [Thought Path A] ──> Answer A ├─> [Thought Path B] ──> Answer B ──> [Majority Vote] ──> Best Answer └─> [Thought Path C] ──> Answer A
Tree of Thoughts: [Start] ──┬─> [Thought Step 1] (Good) ──> [Thought Step 2] (Best) └─> [Thought Step 1b] (Dead-end) ──> [Backtrack]
Advantages
- —
No weight training required
- —
Rapid deployment and modification
Disadvantages
- —
Adds latency (CoT generates more tokens)
- —
Sensitive to minor wording alterations
Common mistakes
- —
Using CoT for simple tasks. "What is 2+2? Think step by step." wastes tokens without benefit — use CoT only for tasks that require multiple reasoning steps. Simple lookups, classifications, and summaries do not need CoT.
- —
Not testing whether CoT actually helps your specific task. CoT improves math and logical reasoning but can hurt performance on straightforward retrieval or creative tasks by making the model overthink. Always A/B test with and without CoT.
- —
Asking the model to reason after the answer ("The answer is X because..."). The reasoning must come BEFORE the answer to actually guide it. Correct format: "Let me think... [reasoning]... Therefore the answer is X." Putting answer first is post-hoc rationalization, not actual reasoning.
- —
Writing few-shot examples that are too similar to each other. If all 5 examples demonstrate the same type of problem, the model overfits to that pattern and fails on variations. Include examples covering edge cases, different problem types, and different reasoning paths.
- —
Expecting CoT to fix factual knowledge gaps. CoT improves reasoning quality but cannot create knowledge the model does not have. If the model does not know a fact, no amount of "think step by step" will produce the correct answer — use RAG or tool calls to provide the missing information.
🎤 Interview questions
How does constrained grammar decoding enforce JSON formatting at the token logits level? Why is it better than raw prompting?
📂 Subtopics
What Is Prompt Engineering? (Fundamentals)
The foundational framing of prompt engineering as the 'steering wheel' for an LLM — changing instructions, not weights, to shift model behavior.
~10 min
Attentive Reasoning Queries (ARQ)
A structured, JSON-schema-based reasoning technique that replaces CoT's free-form 'thinking aloud' with explicit domain-specific queries the model must answer at each step, improving instruction adherence in long multi-turn conversations.
~20 min