How to beginner to intermediate · 3 min read

How to use reasoning models for math problems

Quick answer
Use specialized reasoning models such as DeepSeek-R1 or claude-sonnet-4-5 to solve math problems by prompting them with clear step-by-step instructions. These models excel at logical reasoning and multi-step calculations, making them ideal for math tasks.

PREREQUISITES

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

Setup

Install the openai Python SDK and set your API key as an environment variable to access reasoning models like DeepSeek-R1.

bash
pip install openai>=1.0

Step by step

Use the DeepSeek-R1 model with a prompt that instructs the model to solve a math problem step-by-step. This example shows how to query the model and print the answer.

python
import os
from openai import OpenAI

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

math_problem = "Calculate the sum of the first 10 positive integers."
prompt = f"Solve this math problem step-by-step:\n{math_problem}\nStep 1:"

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

print("Model response:\n", response.choices[0].message.content)
output
Model response:
Step 1: List the first 10 positive integers: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.
Step 2: Calculate their sum: 1+2+3+4+5+6+7+8+9+10 = 55.
Answer: 55.

Common variations

You can use other reasoning models like claude-sonnet-4-5 for math problems by changing the model parameter. Async calls or streaming responses are also possible depending on the SDK.

python
import os
import asyncio
from openai import OpenAI

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

async def solve_math_async():
    response = await client.chat.completions.acreate(
        model="claude-sonnet-4-5",
        messages=[{"role": "user", "content": "What is 15 multiplied by 12? Show steps."}],
        max_tokens=128
    )
    print("Async model response:\n", response.choices[0].message.content)

asyncio.run(solve_math_async())
output
Async model response:
Step 1: Multiply 15 by 10 = 150.
Step 2: Multiply 15 by 2 = 30.
Step 3: Add 150 + 30 = 180.
Answer: 180.

Troubleshooting

  • If the model returns incomplete answers, increase max_tokens to allow longer reasoning chains.
  • If the output is off-topic, clarify the prompt with explicit instructions like "Show your work step-by-step."
  • For rate limits, ensure your API key has sufficient quota or retry after some time.

Key Takeaways

  • Use reasoning-specialized models like DeepSeek-R1 for complex math problems.
  • Prompt models with explicit step-by-step instructions to improve answer quality.
  • Adjust max_tokens and model choice based on problem complexity and response length.
  • Async and streaming calls are supported for responsive applications.
  • Clear prompts reduce hallucinations and improve math reasoning accuracy.
Verified 2026-04 · deepseek-reasoner, claude-sonnet-4-5
Verify ↗