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.
Core Advantages
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.
# 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.
# 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.
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)
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 |
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?
Fast, low cost, suitable for most scenarios
Supports Chain-of-Thought, the first choice for complex problems
Specially optimized, higher code quality
128K context, supports long text
Price Calculator
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
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
# 流式响应
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
# 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
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
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!"}
]
}'