Back to Prompting Recipes

Chain of Thought

Reasoning

Reasoning

Encourage step-by-step reasoning for complex problems.

Summary

Chain of Thought (CoT) is a prompting technique that encourages the LLM to break down complex reasoning tasks into a series of intermediate steps. By explicitly showing the model how to "think step by step," it dramatically improves performance on tasks requiring multi-step reasoning like math problems, logical puzzles, and complex decision-making.

Implementation

  1. Use explicit prompting with phrases like "Let's think step by step" or "Let's solve this problem by breaking it down."
  2. Encourage detailed reasoning by asking the model to explain its thought process for each step.
  3. Structure matters - use numbered steps or clear paragraph breaks to help the model organize its thoughts.
  4. For complex problems, consider combining with few-shot examples that demonstrate the desired reasoning pattern.
  5. Request verification by asking the model to check its work after reaching a conclusion.

Common pitfalls

  • Shallow reasoning: Prompt for deeper analysis
  • Skipping steps: Request explicit intermediate steps
  • Wrong format: Provide example structure
  • Premature conclusions: Ask model to verify answer

Best for

  • Mathematical problems requiring step-by-step calculation
  • Logical reasoning tasks with multiple premises
  • Multi-step decision making scenarios
  • Any task where the path to the answer matters as much as the answer itself
Code View

Chain of Thought Implementation

// Chain of Thought recipe using OpenAI
// Install: bun add openai

import OpenAI from "openai";

async function main() {
  const input = "Add your prompt here.";
  const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
  const system = "You are a senior AI engineer and technical writer. Use the prompting technique to answer the request clearly and precisely. Recipe: Chain of Thought. Description: Encourage step-by-step reasoning for complex problems. Focus: Reasoning Provide actionable, implementation-ready guidance.";
  const user = `Request: ${input}`;

  const openaiResponse = await openai.chat.completions.create({
    model: "gpt-4o-mini",
    messages: [
      { role: "system", content: system },
      { role: "user", content: user },
    ],
  });

  const openaiText = openaiResponse.choices[0]?.message?.content?.trim() ?? "";

  console.log(openaiText);
}

main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});