DeepSeek API Python Quickstart: A Working Chat in Ten Minutes
This tutorial gets a DeepSeek API call running in about ten minutes: key setup, SDK install, a chat message, then streaming. The API is OpenAI-format compatible, with an Anthropic-format endpoint as well, so migrating from other models is cheap.
Setup
- Register on the DeepSeek open platform and top up (new users usually get granted credits — check the console);
- Create an API key and keep it in an environment variable, never committed to the repo.
export DEEPSEEK_API_KEY="sk-..."
Install the SDK and send a message
pip install openai
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["DEEPSEEK_API_KEY"],
base_url="https://api.deepseek.com",
)
resp = client.chat.completions.create(
model="deepseek-flash", # DeepSeek-V4.1-Flash
messages=[
{"role": "system", "content": "You are a concise assistant."},
{"role": "user", "content": "Explain off-peak pricing in one sentence."},
],
)
print(resp.choices[0].message.content)
Switch to deepseek-v4-pro for harder reasoning; for image input see the vision API tutorial.
Streaming
stream = client.chat.completions.create(
model="deepseek-flash",
messages=[...],
stream=True,
)
for chunk in stream:
delta = chunk.choices[0].delta.content
if delta:
print(delta, end="", flush=True)
Anthropic-format endpoint
Teams used to the Anthropic SDK can point base_url at https://api.deepseek.com/anthropic — same model names, a two-line migration.
Three common pitfalls
- Wrong model name: current names are
deepseek-flashanddeepseek-v4-pro; legacy names still resolve to the new model; - Bill surprises: peak hours (09:00–12:00 and 14:00–18:00 Beijing time on workdays) cost double — schedule batches at night or on weekends, see the cost-saving guide;
- Thinking is default-on: the model thinks by default; switch it off per the official docs for faster responses on simple tasks.
For the full pricing table and time windows see the peak/off-peak explainer.
DeepSeek 网摘