How to Beginner to Intermediate · 3 min read

How to use chain-of-thought prompting

Quick answer
Use chain-of-thought prompting by explicitly instructing the AI to reason step-by-step before answering. This technique improves complex problem solving by guiding the model to explain its reasoning process in the response.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install openai>=1.0

Setup

Install the openai Python package and set your API key as an environment variable to authenticate requests.

bash
pip install openai>=1.0

Step by step

This example shows how to use chain-of-thought prompting with the gpt-4o model by asking it to explain its reasoning step-by-step before giving the final answer.

python
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

prompt = (
    "Solve this math problem step-by-step:\n"
    "If there are 3 apples and you buy 2 more, how many apples do you have?"
    " Please explain your reasoning before the final answer."
)

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": prompt}]
)

print(response.choices[0].message.content)
output
Step 1: You start with 3 apples.
Step 2: You buy 2 more apples.
Step 3: Add the apples you started with and the apples you bought: 3 + 2 = 5.
Final answer: You have 5 apples.

Common variations

You can use chain-of-thought prompting with other models like claude-3-5-sonnet-20241022 or enable streaming for real-time output. Async calls are also supported in the OpenAI SDK.

python
import os
import asyncio
from openai import OpenAI

async def main():
    client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
    prompt = (
        "Explain step-by-step how to solve this riddle:\n"
        "What has keys but can't open locks?"
        " Provide your reasoning before the answer."
    )

    response = await client.chat.completions.acreate(
        model="gpt-4o",
        messages=[{"role": "user", "content": prompt}]
    )

    print(response.choices[0].message.content)

asyncio.run(main())
output
Step 1: Consider what "keys" could mean beyond physical keys.
Step 2: Keys can also refer to piano keys.
Step 3: A piano has keys but cannot open locks.
Final answer: The answer is a piano.

Troubleshooting

If the model gives short or incomplete reasoning, explicitly add instructions like "Explain your reasoning step-by-step before the final answer." Increase max_tokens if the response is cut off.

Key Takeaways

  • Explicitly instruct the model to reason step-by-step to improve answer quality.
  • Use chain-of-thought prompting for complex reasoning tasks like math or logic puzzles.
  • Adjust max_tokens and prompt clarity to ensure complete reasoning output.
Verified 2026-04 · gpt-4o, claude-3-5-sonnet-20241022
Verify ↗