How to beginner · 3 min read

How to use DeepSeek-R1 API

Quick answer
Use the DeepSeek-R1 model by calling the OpenAI-compatible API endpoint with your API key and sending a chat completion request. The model excels at complex reasoning and can be accessed via the openai Python SDK by specifying model="deepseek-reasoner" in your request.

PREREQUISITES

  • Python 3.8+
  • DeepSeek API key (set as environment variable DEEPSEEK_API_KEY)
  • pip install openai>=1.0

Setup

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

bash
pip install openai>=1.0

Step by step

Use the OpenAI-compatible Python SDK to send a chat completion request to the deepseek-reasoner model. This example shows a simple prompt and prints the reasoning output.

python
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["DEEPSEEK_API_KEY"], base_url="https://api.deepseek.com")

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

print(response.choices[0].message.content)
output
The Pythagorean theorem states that in a right triangle, the square of the hypotenuse equals the sum of the squares of the other two sides. This can be reasoned by constructing squares on each side and comparing their areas...

Common variations

  • Use different prompts to tailor reasoning tasks.
  • Adjust max_tokens or temperature for output length and creativity.
  • Switch to async calls using asyncio and await with the OpenAI SDK.
python
import os
import asyncio
from openai import OpenAI

async def async_reasoning():
    client = OpenAI(api_key=os.environ["DEEPSEEK_API_KEY"], base_url="https://api.deepseek.com")
    response = await client.chat.completions.acreate(
        model="deepseek-reasoner",
        messages=[{"role": "user", "content": "Why does water boil at lower temperatures at higher altitudes?"}]
    )
    print(response.choices[0].message.content)

asyncio.run(async_reasoning())
output
At higher altitudes, atmospheric pressure is lower, so water molecules require less energy to escape into the gas phase, causing boiling at lower temperatures.

Troubleshooting

  • If you get authentication errors, verify your DEEPSEEK_API_KEY environment variable is set correctly.
  • For timeout or connection errors, check your network and the base_url parameter.
  • If responses are incomplete, increase max_tokens in your request.

Key Takeaways

  • Use the OpenAI-compatible SDK with model="deepseek-reasoner" to access DeepSeek-R1.
  • Set your API key in the environment variable DEEPSEEK_API_KEY and specify the DeepSeek base URL.
  • You can perform synchronous or asynchronous calls depending on your application needs.
  • Adjust parameters like max_tokens and temperature to control output detail and style.
  • Check environment variables and network settings if you encounter authentication or connection issues.
Verified 2026-04 · deepseek-reasoner
Verify ↗