How to Intermediate · 3 min read

How to use reasoning models for complex analysis

Quick answer
Use specialized reasoning models such as claude-sonnet-4-5 or deepseek-reasoner to perform complex analysis by crafting clear prompts that guide the model through multi-step logic. Call these models via their APIs with structured inputs to get detailed, stepwise reasoning outputs.

PREREQUISITES

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

Setup

Install the required Python packages and set your API keys as environment variables to access reasoning models from OpenAI and Anthropic.

bash
pip install openai anthropic

Step by step

This example shows how to use claude-sonnet-4-5 from Anthropic and deepseek-reasoner from DeepSeek for complex reasoning tasks. The prompt guides the model to analyze a multi-step problem and return a detailed explanation.

python
import os
import anthropic
from openai import OpenAI

# Initialize Anthropic client
anthropic_client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])

# Initialize OpenAI client for DeepSeek
openai_client = OpenAI(api_key=os.environ["OPENAI_API_KEY"], base_url="https://api.deepseek.com")

# Define a complex reasoning prompt
prompt = (
    "You are a reasoning assistant. Analyze the following problem step-by-step and provide a detailed explanation:\n"
    "\nProblem: If a train travels 60 miles in 1.5 hours and then 90 miles in 2 hours, what is the average speed?"
)

# Anthropic Claude-sonnet-4-5 call
anthropic_response = anthropic_client.messages.create(
    model="claude-sonnet-4-5",
    max_tokens=512,
    system="You are a helpful reasoning assistant.",
    messages=[{"role": "user", "content": prompt}]
)
print("Anthropic Claude-sonnet-4-5 response:\n", anthropic_response.content[0].text)

# DeepSeek Reasoner call
deepseek_response = openai_client.chat.completions.create(
    model="deepseek-reasoner",
    messages=[{"role": "user", "content": prompt}]
)
print("\nDeepSeek Reasoner response:\n", deepseek_response.choices[0].message.content)
output
Anthropic Claude-sonnet-4-5 response:
To find the average speed, first calculate the total distance and total time. Total distance = 60 + 90 = 150 miles. Total time = 1.5 + 2 = 3.5 hours. Average speed = total distance / total time = 150 / 3.5 ≈ 42.86 mph.

DeepSeek Reasoner response:
The train travels 60 miles in 1.5 hours and 90 miles in 2 hours. Total distance = 60 + 90 = 150 miles. Total time = 1.5 + 2 = 3.5 hours. Average speed = 150 miles / 3.5 hours = approximately 42.86 miles per hour.

Common variations

You can use asynchronous calls or switch models depending on your needs. For example, use claude-3-5-sonnet-20241022 for faster responses or gpt-4o for multimodal tasks. Streaming output is supported by some SDKs for real-time reasoning feedback.

python
import asyncio
import anthropic

async def async_anthropic_reasoning():
    client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
    response = await client.messages.acreate(
        model="claude-3-5-sonnet-20241022",
        max_tokens=512,
        system="You are a helpful reasoning assistant.",
        messages=[{"role": "user", "content": prompt}]
    )
    print("Async Anthropic response:\n", response.content[0].text)

# Run async example
asyncio.run(async_anthropic_reasoning())
output
Async Anthropic response:
To solve the problem, calculate total distance and total time, then divide distance by time to get average speed. Total distance = 150 miles, total time = 3.5 hours, average speed ≈ 42.86 mph.

Troubleshooting

  • If you get incomplete answers, increase max_tokens or simplify your prompt.
  • If the model returns irrelevant info, clarify instructions in the system prompt.
  • For API errors, verify your API keys and network connectivity.

Key Takeaways

  • Use specialized reasoning models like claude-sonnet-4-5 or deepseek-reasoner for multi-step logical analysis.
  • Craft clear, stepwise prompts to guide the model through complex problems effectively.
  • Adjust max_tokens and system instructions to improve answer completeness and relevance.
Verified 2026-04 · claude-sonnet-4-5, deepseek-reasoner, claude-3-5-sonnet-20241022, gpt-4o
Verify ↗