How to beginner · 3 min read

How DeepSeek-R1 reasoning works

Quick answer
DeepSeek-R1 is a specialized reasoning model accessed via the OpenAI compatible API with the deepseek-reasoner model name. It processes complex reasoning queries by leveraging reinforcement learning with value reranking (RLVR) to provide precise, logical answers.

PREREQUISITES

  • Python 3.8+
  • DeepSeek API key
  • pip install openai>=1.0

Setup

Install the openai Python package and set your DeepSeek API key as an environment variable.

  • Run pip install openai to install the SDK.
  • Export your API key in your shell: export DEEPSEEK_API_KEY='your_api_key_here'.
bash
pip install openai

Step by step

Use the OpenAI SDK with the deepseek-reasoner model to send reasoning queries. The model applies RLVR techniques to generate accurate, logical responses.

python
import os
from openai import OpenAI

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

response = client.chat.completions.create(
    model="deepseek-reasoner",
    messages=[{"role": "user", "content": "Explain the reasoning behind the Monty Hall problem."}]
)

print(response.choices[0].message.content)
output
The Monty Hall problem is a probability puzzle where switching your choice after a non-winning door is revealed increases your chances of winning from 1/3 to 2/3. This is because the host's action provides additional information that changes the probability distribution.

Common variations

You can adjust parameters like max_tokens and temperature to control response length and creativity. The API supports async calls and streaming responses for real-time applications.

python
import asyncio
import os
from openai import OpenAI

async def async_reasoning():
    client = OpenAI(api_key=os.environ["DEEPSEEK_API_KEY"])
    response = await client.chat.completions.acreate(
        model="deepseek-reasoner",
        messages=[{"role": "user", "content": "Why is the sky blue?"}],
        max_tokens=150,
        temperature=0.3
    )
    print(response.choices[0].message.content)

asyncio.run(async_reasoning())
output
The sky appears blue because molecules in the atmosphere scatter shorter wavelengths of light, like blue, more than longer wavelengths. This scattering causes the blue color to dominate when we look up during the day.

Troubleshooting

  • If you receive authentication errors, verify your DEEPSEEK_API_KEY environment variable is set correctly.
  • For rate limit errors, implement exponential backoff retries.
  • Unexpected or incomplete answers may require increasing max_tokens or lowering temperature.

Key Takeaways

  • Use the deepseek-reasoner model via the OpenAI-compatible SDK for advanced reasoning tasks.
  • Leverage RLVR-based reasoning for precise, logical answers to complex queries.
  • Adjust max_tokens and temperature to optimize response quality.
  • Use async API calls for scalable, real-time reasoning applications.
  • Always secure your API key in environment variables to avoid authentication issues.
Verified 2026-04 · deepseek-reasoner
Verify ↗