🌐 Live demo (LOOK · UNDERSTAND · BUILD): (no credit card).
CODE// cot.mjs
import { generateText } from "ai";
import { google } from "@ai-sdk/google";
const model = google("gemini-2.5-flash");
const problem = "Roger has 5 tennis balls. He buys 2 cans of 3 balls each. How many balls does he have now?";
const bad = await generateText({
model,
prompt: problem + "\n\nJust answer with the number, nothing else."
});
const good = await generateText({
model,
prompt: problem + "\n\nLet's think step by step."
});
console.log("=== Without CoT ===\n" + bad.text);
console.log("\n=== With CoT ===\n" + good.text);
CODEnode --env-file=.env cot.mjs
Two runs of the same model on the same problem, side by side. The difference is visible immediately.
Levels of CoT
1. Zero-shot CoT (above)
Just add "Let's think step by step." Works on most modern models.
2. Few-shot CoT
Prepend 2-3 worked examples before the question:
CODEQ: Sara had 4 apples and got 2 more. How many?
A: Sara had 4. She got 2 more. 4 + 2 = 6. Answer: 6.
Q: Roger has 5 tennis balls. He buys 2 cans of 3 each. How many balls?
A: [model continues in same format]
Better on harder problems — the model has explicit examples of the reasoning depth you want.
3. Structured CoT
Force a format:
CODE"Solve this. Number your steps 1, 2, 3. Final answer on a new line starting 'Answer:'."
Easier to parse programmatically.
4. Hidden CoT
Generate the chain, then strip it before showing the user:
CODEconst reply = result.text;
const clean = reply.replace(/<thinking>[\s\S]*?<\/thinking>/g, '').trim();
User sees just the answer; the model gets the accuracy benefit.
What about reasoning models?
GPT-5, Claude 4 Sonnet, o1, o3, Gemini 2.5 — modern flagship models train with reasoning baked in. They don't need "let's think step by step." They do it automatically.
But:
- They cost 10× more per token
- They're slower (visible "thinking..." UI)
- They're overkill for simple tasks
Cheap model + CoT prompt ≈ reasoning model output, at ~10% of the cost. CoT is still the highest-leverage technique you can use on small models.
What this unlocks
CoT is the foundation. Every fancier reasoning technique builds on top:
Self-consistency — sample N CoT runs, take majority vote
ReAct — CoT + tool calls interleaved (Day 1)
Tree of Thoughts — branch CoT into multiple paths, evaluate
Reflection — generate, criticize own output, regenerate
Master CoT first. Everything else is variations.
Try it now
Three tabs on one page:
↗ Original-Artikel auf dev.to lesenVollständiger Original-BerichtAusführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
7 Magic Words That Make Your LLM 10 Smarter at Math
- ▸ The setup
- ↳ Prompt A — "just answer"
- ↳ Prompt B — Chain of Thought
- ▸ Why it works
- ▸ When to use it
- ▸ Build it in 10 minutes
- ▸ Levels of CoT
- ↳ 1. Zero-shot CoT (above)
- ↳ 2. Few-shot CoT
- ↳ 3. Structured CoT
- ↳ 4. Hidden CoT
- ▸ What about reasoning models?
- ▸ What this unlocks
- ▸ Try it now
- ▸ What's next in PromptFromZero
SOCIAL SHARE CARD GENERATOR