Prompt Engineering & Chain-of-Thought Reasoning — AI Engineering
Prompt engineering is the highest-leverage LLM skill — the right technique can improve accuracy by 30–40% with zero additional compute during training.
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.
How it works
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).
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.
Code 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." }] ) # Self-consistency (majority vote over 5 paths) from collections import Counter answers = [] for _ in range(5): r = client.messages.create(model="claude-opus-4-5", messages=[{"role": "user", "content": question + " Think step by step."}]) answers.append(extract_final_answer(r.content[0].text)) final = Counter(answers).most_common(1)[0][0]
Key concepts
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.
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.
Asking the model to reason AFTER the answer. The reasoning must come BEFORE the answer to actually guide it. Putting the answer first is post-hoc rationalization, not actual reasoning.
Writing few-shot examples that are too similar to each other. 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 — use RAG or tool calls to provide missing information.
Interview questions on this topic
How does constrained grammar decoding enforce JSON formatting at the token logits level? Why is it better than raw prompting?
Practice answering these with structured learning → Start on CrackLab
Continue learning — explore the full AI Engineering curriculum
30+ structured topics from Python fundamentals to deploying production LLM systems. Free to start.
Related free guides
LLMs are neural networks that predict the next token using Transformer attention — understanding them unlocks every AI engineering discipline.
RAG grounds LLM answers in your own knowledge base by retrieving relevant document chunks at query time — making it the go-to architecture for enterprise AI.
AI agents extend LLMs with a feedback loop — perceive → reason → act → observe → repeat — enabling them to solve multi-step tasks that a single prompt cannot.