DeepSeek API Usage Guide

Latest V3/R1 model complete tutorial · Price calculation · Code examples

What is DeepSeek?

DeepSeek is a leading AI company in China. Its latest DeepSeek-V3 and DeepSeek-R1 Model achieves GPT-4 level performance on multiple benchmarks while being 1/10th the price.

api.deepseek.com
OpenAI API format
128K tokens
¥1 / million tokens

Core Advantages

The price is only 1/10 of GPT-4 and 1/5 of Claude
Just modify base_url and existing code can be migrated
Native Chinese training, better performance in Chinese scenes
R1 model supports deep thinking and leads in mathematical reasoning

Quick Start

Register an account and obtain API Key

Visit platform.deepseek.com , create a key on the API Keys page after registering an account. New users will receive 5 million tokens upon registration.

Install OpenAI SDK

DeepSeek API is fully compatible with the OpenAI format and can directly use the OpenAI official SDK.

bash
# Python
pip install openai

# Node.js
npm install openai

Configure environment variables

Save the API Key as an environment variable to avoid hardcoding it in the code.

bash
# Linux/macOS
export DEEPSEEK_API_KEY="your-api-key"

# Windows PowerShell
$env:DEEPSEEK_API_KEY="your-api-key"

Send first request

Use the following code to test whether the API is working properly.

Python
from openai import OpenAI

client = OpenAI(
    api_key="your-api-key",
    base_url="https://api.deepseek.com"
)

response = client.chat.completions.create(
    model="deepseek-chat",  # V3 模型
    messages=[
        {"role": "user", "content": "你好,请介绍一下你自己"}
    ]
)

print(response.choices[0].message.content)
💡 Tips

The API Key is only displayed once when created, please be sure to save it. If lost, you need to delete and recreate it.

Model list

Model name API ID Features Context Enter price Output price
DeepSeek-V3 HOT deepseek-chat Universal dialogue, fast 128K ¥1/M ¥2/M
DeepSeek-R1 Reasoning deepseek-reasoner Deep thinking, strong in mathematics/code 128K ¥4/M ¥16/M
DeepSeek-Coder deepseek-coder Code generation and completion 128K ¥1/M ¥2/M
⚠️ NOTE

Cache hits are cheaper : When the input content is similar to the previous request, the token price of the cache hit is only ¥0.1/million (V3) or ¥1/million (R1).

How to choose a model?

deepseek-chat (V3)

Fast, low cost, suitable for most scenarios

deepseek-reasoner (R1)

Supports Chain-of-Thought, the first choice for complex problems

deepseek-coder

Specially optimized, higher code quality

deepseek-chat (V3)

128K context, supports long text

Price Calculator

¥0.00
Estimated cost

Price comparison

Model Enter price (per million) Output price (per million) compared to GPT-4
DeepSeek-V3 ¥1 ($0.14) ¥2 ($0.28) 97% cheaper
DeepSeek-R1 ¥4 ($0.55) ¥16 ($2.19) 85% cheaper
GPT-4-Turbo $10 $30 -
Claude 3.5 Sonnet $3 $15 70% cheaper

Python example

Basic Dialogue

Python
from openai import OpenAI

client = OpenAI(
    api_key="your-api-key",
    base_url="https://api.deepseek.com"
)

# 简单对话
response = client.chat.completions.create(
    model="deepseek-chat",
    messages=[
        {"role": "system", "content": "你是一个有帮助的助手"},
        {"role": "user", "content": "解释什么是机器学习"}
    ],
    temperature=0.7,
    max_tokens=2000
)

print(response.choices[0].message.content)

Streaming output

Python
# 流式响应
stream = client.chat.completions.create(
    model="deepseek-chat",
    messages=[{"role": "user", "content": "写一首关于春天的诗"}],
    stream=True
)

for chunk in stream:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="")

Using R1 inference model

Python
# DeepSeek-R1 推理模型(带思考过程)
response = client.chat.completions.create(
    model="deepseek-reasoner",
    messages=[{
        "role": "user", 
        "content": "一个农夫带着狼、羊和白菜过河,每次只能带一样..."
    }]
)

# R1 会返回思考过程
print("思考过程:", response.choices[0].message.reasoning_content)
print("最终答案:", response.choices[0].message.content)

JavaScript/Node.js Example

JavaScript
import OpenAI from 'openai';

const client = new OpenAI({
    apiKey: 'your-api-key',
    baseURL: 'https://api.deepseek.com'
});

async function chat() {
    const response = await client.chat.completions.create({
        model: 'deepseek-chat',
        messages: [
            { role: 'user', content: 'Hello, DeepSeek!' }
        ]
    });
    
    console.log(response.choices[0].message.content);
}

chat();

cURL example

bash
curl https://api.deepseek.com/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ${DEEPSEEK_API_KEY}" \
  -d '{
    "model": "deepseek-chat",
    "messages": [
      {"role": "user", "content": "Hello!"}
    ]
  }'