Reasoning model prompt best practices
Quick answer
Use clear, step-by-step instructions and explicit context in reasoning model prompts to guide logical thinking. Break complex problems into smaller parts and request explanations to improve accuracy and transparency.
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.
pip install openai>=1.0 Step by step
Use explicit, stepwise prompts that break down reasoning tasks and ask the model to explain its logic. This improves accuracy and interpretability.
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 and explain your reasoning.\n"
"Problem: If all cats are animals and some animals are playful, can we conclude some cats are playful? Explain."
)
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": prompt}]
)
print(response.choices[0].message.content) output
Step 1: All cats are animals, so every cat belongs to the animal group. Step 2: Some animals are playful, but this does not necessarily include cats. Step 3: Since 'some animals' is a subset that may or may not overlap with cats, we cannot conclude that some cats are playful. Conclusion: No, we cannot conclude some cats are playful based on the given information.
Common variations
You can use different reasoning-focused models like claude-3-5-sonnet-20241022 or enable streaming for real-time output. Async calls improve throughput in batch scenarios.
from anthropic import Anthropic
import os
client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
system_prompt = "You are a reasoning assistant. Provide stepwise logical explanations."
user_prompt = (
"Problem: If all cats are animals and some animals are playful, can we conclude some cats are playful? Explain."
)
message = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=512,
system=system_prompt,
messages=[{"role": "user", "content": user_prompt}]
)
print(message.content) output
Step 1: All cats are animals, so cats are included in the animal category. Step 2: Some animals are playful, but this does not specify which animals. Step 3: Since we don't know if cats are part of the playful animals, we cannot conclude that some cats are playful. Therefore, the conclusion is no.
Troubleshooting
- If the model gives vague or incorrect reasoning, add explicit instructions to "think step-by-step" or "explain your logic."
- Use few-shot examples in the prompt to demonstrate the reasoning style you want.
- Check token limits and reduce prompt length if responses are cut off.
Key Takeaways
- Explicitly instruct reasoning models to think step-by-step for clearer logic.
- Break complex problems into smaller parts within the prompt to improve accuracy.
- Request explanations to enhance transparency and trust in model outputs.