How to beginner · 3 min read

How to prompt reasoning models effectively

Quick answer
To prompt reasoning models effectively, use clear, step-by-step instructions and break complex problems into smaller parts within your prompt. Employ chain-of-thought prompting by explicitly asking the model to explain its reasoning before giving a final answer.

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 for secure access.

bash
pip install openai>=1.0

Step by step

This example demonstrates prompting gpt-4o with chain-of-thought instructions to solve a reasoning problem stepwise.

python
import os
from openai import OpenAI

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

prompt = (
    "You are a reasoning assistant. Solve the problem step-by-step. "
    "Question: If a train travels 60 miles in 1.5 hours, what is its average speed? "
    "Explain your reasoning before giving the final answer."
)

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

print(response.choices[0].message.content)
output
The train travels 60 miles in 1.5 hours. To find the average speed, divide the distance by the time: 60 miles ÷ 1.5 hours = 40 miles per hour. So, the average speed is 40 mph.

Common variations

You can use other reasoning-focused models like claude-3-5-sonnet-20241022 with Anthropic's SDK or add explicit instructions for multi-step reasoning. Streaming responses or async calls are also supported by respective SDKs.

python
from anthropic import Anthropic
import os

client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])

system_prompt = "You are a helpful assistant specialized in step-by-step reasoning."
user_prompt = (
    "Calculate the average speed of a train that travels 60 miles in 1.5 hours. "
    "Explain your reasoning stepwise before the final answer."
)

response = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=200,
    system=system_prompt,
    messages=[{"role": "user", "content": user_prompt}]
)

print(response.content[0].text)
output
To find the average speed, divide the total distance by the total time. The train travels 60 miles in 1.5 hours, so 60 ÷ 1.5 = 40 miles per hour. Therefore, the average speed is 40 mph.

Troubleshooting

  • If the model gives incomplete reasoning, explicitly prompt for "step-by-step" or "chain-of-thought" explanations.
  • For vague answers, increase max_tokens to allow longer responses.
  • If the model ignores instructions, rephrase prompts to be more direct and clear.

Key Takeaways

  • Use explicit step-by-step or chain-of-thought instructions to guide reasoning models.
  • Break complex problems into smaller parts within your prompt for clarity.
  • Choose reasoning-optimized models like gpt-4o or claude-3-5-sonnet-20241022 for best results.
Verified 2026-04 · gpt-4o, claude-3-5-sonnet-20241022
Verify ↗