Step 1: Get a key
Create an API key in the DeepSeek platform console and load a small prepaid balance — the API is prepaid, and a few dollars goes a very long way at $0.14/M input. Store the key in an environment variable, never in source.
Step 2: Make the first call
Python
from openai import OpenAI
client = OpenAI(
api_key="YOUR_DEEPSEEK_KEY",
base_url="https://api.deepseek.com",
)
response = client.chat.completions.create(
model="deepseek-v4-flash",
messages=[
{"role": "system", "content": "You are a concise assistant."},
{"role": "user", "content": "Summarize KV caching in 3 bullets."},
],
stream=False,
)
print(response.choices[0].message.content)TypeScript
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.DEEPSEEK_API_KEY,
baseURL: "https://api.deepseek.com",
});
const response = await client.chat.completions.create({
model: "deepseek-v4-flash",
messages: [
{ role: "system", content: "You are a concise assistant." },
{ role: "user", content: "Summarize KV caching in 3 bullets." },
],
});
console.log(response.choices[0].message.content);curl
curl https://api.deepseek.com/chat/completions \
-H "Authorization: Bearer $DEEPSEEK_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "deepseek-v4-flash",
"messages": [
{"role": "system", "content": "You are a concise assistant."},
{"role": "user", "content": "Summarize KV caching in 3 bullets."}
]
}'Step 3: Engineer for cache hits (the 5× discount)
This is the billing detail that separates cheap deployments from accidentally expensive ones. Input tokens that match a recently seen prompt prefix bill at $0.028/M instead of $0.14/M. In practice: put your stable content (system prompt, tool schemas, few-shot examples) first and byte-for-byte identical on every request, and put the variable content (user message, retrieved documents) last. Agent frameworks that regenerate their system prompt with a timestamp on every call silently pay 5× more for input than they need to.
The error codes you'll actually see
| Code | Meaning | Fix |
|---|
| 401 | Invalid or missing API key | Check the Authorization: Bearer header; regenerate the key in the DeepSeek console if needed |
| 402 | Insufficient balance | Top up in the console — DeepSeek is prepaid; there is no postpaid overdraft |
| 400 (invalid model) | Wrong or retired model ID | Use deepseek-v4-flash or deepseek-v4-pro; legacy aliases retire 2026-07-24 |
| 429 | Rate limited | Back off exponentially; batch small requests instead of parallel-spamming |
| 500 / 503 | Server busy or down | Retry with jitter; add a fallback route if uptime is critical |
Where to go next
If you're coming from deepseek-chat, the migration checklist covers the July 24 deadline. To decide when a request should escalate from Flash to Pro, the Flash vs Pro routing rules give concrete triggers. And the cost calculator models your monthly bill from your actual traffic mix.
Frequently asked questions
Do I need a special SDK for DeepSeek?
No. The API is OpenAI-compatible: point any OpenAI SDK at https://api.deepseek.com with your DeepSeek key and the deepseek-v4-flash model ID. LangChain, LlamaIndex, and the Vercel AI SDK all work by swapping the base URL.
How does cache-hit billing work?
If the prefix of your prompt matches a recent request (typically your system prompt and tool definitions), that portion bills at $0.028/M instead of $0.14/M — an automatic 5× discount on repeated context. You don't opt in; you earn it by keeping prompt prefixes stable.
How do I enable thinking mode on Flash?
V4 Flash defaults to non-thinking mode. Reasoning modes (Think / Think Max) are enabled per-request via the API's reasoning parameters — check the official docs for the current parameter name, as preview-release APIs change.
What's the real cost of a typical chat request?
A request with 2K input tokens and 500 output tokens costs about $0.00042 — roughly 2,400 requests per dollar. With a warm cache on the input side it drops further. Our pricing calculator lets you model your own traffic mix.
Last verified: 2026-07-02 against the official DeepSeek API documentation. Preview-release APIs change; re-check parameter names in the official docs before shipping.