AI Agents & LLMsMedium
What is chain-of-thought prompting?
Learning path, stage 2 of 5: Prompting and model features · question 3 of 10Chain-of-thought (CoT) gets the model to write out intermediate reasoning steps before the final answer. Each step conditions on the previous ones, so multi-step problems (maths, logic, planning) become much more accurate than when the model jumps straight to an answer.
Variants
- Few-shot CoT: examples that include worked reasoning (Wei et al.).
- Zero-shot CoT: simply add "Let's think step by step" (Kojima et al.).
- Structured CoT: ask for reasoning in
<thinking>tags and the answer in<answer>tags, so code can pull out just the answer. - Built-in reasoning: modern "thinking" models do this internally, and you control depth with a setting instead of prompt text.
Trade-offs
- More output tokens, so more cost and latency. Don't use it for trivial lookups.
- The written reasoning is not always faithful to how the model reached its answer. Don't treat it as an audit log.
- It helps most on tasks that really have several steps.
Sources
Code example
tsx
1// Structured CoT: reason in tags, then extract only the answer.2import Anthropic from "@anthropic-ai/sdk";34const client = new Anthropic();56export async function solve(problem: string) {7 const res = await client.messages.create({8 model: "claude-opus-5-5",9 max_tokens: 4096,10 messages: [11 {12 role: "user",13 content:14 `${problem}\n\nWork through it step by step inside <thinking> tags, ` +15 "then give only the final result inside <answer> tags.",16 },17 ],18 });19 const text = res.content.flatMap((b) => (b.type === "text" ? [b.text] : [])).join("");20 return text.match(/<answer>([\s\S]*?)<\/answer>/)?.[1].trim() ?? text;21}2223console.log(await solve("A train leaves at 9:40 and the trip takes 2h 35m. When does it arrive?"));
To run it: follow the one-time setup guide, add npm i @anthropic-ai/sdk zod, save the code as a .ts file and run npx tsx file.ts. Anything marked declare is a part you supply.
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the AI Agents & LLMs set.