How to beginner · 3 min read

How to use reasoning models for coding

Quick answer
Use specialized reasoning models such as claude-sonnet-4-5 or deepseek-reasoner to improve code generation and debugging by leveraging their enhanced logical and stepwise reasoning capabilities. Call these models via their APIs with prompts that explicitly request reasoning steps or code explanations.

PREREQUISITES

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

Setup

Install the necessary 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

Use the claude-sonnet-4-5 model from Anthropic or deepseek-reasoner from DeepSeek to generate code with reasoning. Provide clear prompts that ask the model to explain its logic or solve problems stepwise.

python
import os
import anthropic
from openai import OpenAI

# Anthropic Claude reasoning model example
def anthro_reasoning_code():
    client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
    prompt = (
        "Write a Python function to check if a number is prime. "
        "Explain your reasoning step by step."
    )
    response = client.messages.create(
        model="claude-sonnet-4-5",
        max_tokens=512,
        system="You are a helpful coding assistant that explains reasoning.",
        messages=[{"role": "user", "content": prompt}]
    )
    print("Anthropic Claude response:\n", response.content[0].text)

# OpenAI DeepSeek reasoning model example
def deepseek_reasoning_code():
    client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
    messages = [
        {"role": "user", "content": "Write a Python function to check if a number is prime. Explain your reasoning step by step."}
    ]
    response = client.chat.completions.create(
        model="deepseek-reasoner",
        messages=messages
    )
    print("DeepSeek Reasoner response:\n", response.choices[0].message.content)

if __name__ == "__main__":
    anthro_reasoning_code()
    deepseek_reasoning_code()
output
Anthropic Claude response:
 def is_prime(n):
     if n <= 1:
         return False
     for i in range(2, int(n ** 0.5) + 1):
         if n % i == 0:
             return False
     return True

Explanation:
- Numbers less than or equal to 1 are not prime.
- Check divisibility from 2 up to the square root of n.
- If divisible by any number in this range, n is not prime.
- Otherwise, n is prime.

DeepSeek Reasoner response:
 def is_prime(num):
     if num <= 1:
         return False
     for i in range(2, int(num ** 0.5) + 1):
         if num % i == 0:
             return False
     return True

Reasoning:
- Numbers <= 1 are not prime.
- Only need to check divisors up to sqrt(num) because if num has a factor larger than sqrt(num), it must have a smaller corresponding factor.
- If any divisor divides num evenly, num is not prime.
- Otherwise, num is prime.

Common variations

You can use async calls with the Anthropic SDK or switch models like claude-3-5-sonnet-20241022 for different reasoning styles. Streaming output is supported by some SDKs for real-time code generation feedback.

python
import asyncio
import anthropic

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

if __name__ == "__main__":
    asyncio.run(async_anthropic_reasoning())
output
Async Anthropic response:
 To reverse a linked list in Python, you iterate through the list, changing the next pointer of each node to point to the previous node. This requires keeping track of three pointers: previous, current, and next. By updating these pointers stepwise, you reverse the list in-place efficiently.

Troubleshooting

  • If the model output is incomplete, increase max_tokens or use streaming if supported.
  • If reasoning is shallow, explicitly prompt for step-by-step explanations.
  • Check your API keys and environment variables if authentication errors occur.

Key Takeaways

  • Use reasoning models like claude-sonnet-4-5 or deepseek-reasoner for enhanced code logic and explanations.
  • Explicitly prompt for stepwise reasoning to get detailed coding insights.
  • Leverage async and streaming features for efficient and interactive coding workflows.
Verified 2026-04 · claude-sonnet-4-5, claude-3-5-sonnet-20241022, deepseek-reasoner
Verify ↗