Chain of thought

Chain of Thought (CoT) prompting asks the LLM to "think step by step" rather than jumping straight to an answer. This dramatically improves accuracy on reasoning tasks like math, logic, and multi-step analysis.

Reasoning patterns flow

The progression from basic chain-of-thought to self-correction and problem decomposition

Direct answer (Error-prone)

Chain of thought (accurate)

CoT shines on multi-step reasoning: math, logic puzzles, and complex analysis. For simple factual lookups or creative writing, it adds overhead without much benefit. Use it when the problem requires connecting multiple pieces of information.

patterns/17a_chain_of_thought.py
python
def chain_of_thought_reasoning(problem, llm):
    """Zero-shot CoT: just ask the LLM to think step by step."""
    prompt = f"""Solve this step by step:
    Problem: {problem}

    Think through:
    1. What is the problem asking?
    2. What information do I need?
    3. What steps should I take?
    4. What is the solution?
    5. How can I verify my answer?

    Show your reasoning process clearly."""
    return llm.generate(prompt).content

# Few-shot CoT: provide examples of step-by-step reasoning
def few_shot_cot(problem, examples, llm):
    examples_text = "\n".join(
        [f"Q: {ex['q']}\nReasoning: {ex['reasoning']}\nA: {ex['a']}"
         for ex in examples]
    )
    prompt = f"""Here are examples of step-by-step reasoning:
    {examples_text}

    Now solve: {problem}
    Show your reasoning step by step."""
    return llm.generate(prompt).content

Zero-shot CoT (just add "think step by step") and structured CoT with explicit reasoning stages.

AI prompt: Try it with AI

Loading practice…

Quiz: Quiz

Loading practice…

Ordering exercise: Order the cot reasoning steps

Loading practice…

Flashcards: Flashcards

Loading practice…

Chain-of-thought adds latency and cost because the AI generates more tokens. For simple factual lookups it is overkill. Use it when the problem requires multi-step reasoning, math, or logic. A good rule: if a human would need to think step-by-step, the AI benefits from chain-of-thought too.

Chain-of-thought is one of the most powerful reasoning techniques you can apply. Next, we will explore self-correction so your agents can catch and fix their own errors.