How to Intermediate · 3 min read

How to use reasoning models for multi-step tasks

Quick answer
Use specialized reasoning models such as deepseek-reasoner or claude-3-5-sonnet-20241022 to handle multi-step tasks by breaking down problems into logical steps and prompting the model to reason through each step sequentially. Implement this by chaining prompts or using the model's reasoning capabilities directly via the API.

PREREQUISITES

  • Python 3.8+
  • API key for DeepSeek or Anthropic
  • pip install openai>=1.0 or pip install anthropic>=0.20

Setup

Install the required SDK and set your API key as an environment variable. For DeepSeek, use the OpenAI-compatible SDK; for Anthropic, use their official SDK.

bash
pip install openai anthropic

Step by step

This example shows how to use the deepseek-reasoner model to solve a multi-step math problem by prompting it to think stepwise.

python
import os
from openai import OpenAI

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

prompt = (
    "You are a reasoning assistant. Solve the following problem step-by-step:\n"
    "If a train travels 60 miles in 1.5 hours, what is its average speed?"
)

response = client.chat.completions.create(
    model="deepseek-reasoner",
    messages=[{"role": "user", "content": prompt}],
    max_tokens=150
)

print(response.choices[0].message.content)
output
The train travels 60 miles in 1.5 hours.
Step 1: Calculate the average speed using the formula speed = distance / time.
Step 2: speed = 60 miles / 1.5 hours = 40 miles per hour.
Answer: The average speed of the train is 40 miles per hour.

Common variations

You can use claude-3-5-sonnet-20241022 for similar reasoning tasks with Anthropic's SDK. Async calls and streaming outputs are also supported for interactive applications.

python
import os
from anthropic import Anthropic

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

system_prompt = "You are a helpful reasoning assistant."
user_prompt = (
    "Solve this step-by-step: A farmer has 10 apples and gives 3 to a friend. How many apples remain?"
)

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

print(response.content[0].text)
output
Step 1: The farmer starts with 10 apples.
Step 2: The farmer gives away 3 apples.
Step 3: Calculate remaining apples: 10 - 3 = 7.
Answer: The farmer has 7 apples remaining.

Troubleshooting

  • If the model output is incomplete, increase max_tokens.
  • If reasoning is shallow, explicitly prompt for step-by-step thinking.
  • For long multi-step tasks, break the problem into smaller chunks and chain calls.

Key Takeaways

  • Use specialized reasoning models like deepseek-reasoner or claude-3-5-sonnet-20241022 for multi-step tasks.
  • Prompt the model explicitly to think step-by-step to improve reasoning quality.
  • Chain multiple prompts for complex tasks that exceed single-call context limits.
Verified 2026-04 · deepseek-reasoner, claude-3-5-sonnet-20241022
Verify ↗