The API is the right tool when DeepSeek needs to run without a person in the loop, when you want to plug it into an editor or app, or when you need to process more text than you could ever paste by hand. It is the wrong tool for interactive chat, which the free app already does for nothing. If you are unsure, you do not need it yet.
Get a key
Sign in at platform.deepseek.com, add a small top-up, and create an API key. Store it in an environment variable, never in code. The official quick start covers the console; the pricing page is the source of truth for rates, cache-hit pricing and off-peak windows.
The endpoint is OpenAI-compatible
DeepSeek's API speaks the same chat-completions format as OpenAI. The base URL is https://api.deepseek.com, and the two model names on the price page are deepseek-flash (DeepSeek V4.1 Flash) and deepseek-v4-pro (DeepSeek V4 Pro). V4.1 Flash replaced V4 Flash, which DeepSeek retired; the old deepseek-v4-flash name still works and is billed at the Flash price. Any OpenAI SDK works by changing the base URL and key, which also means switching an existing app to DeepSeek is usually a two-line change.
curl https://api.deepseek.com/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $DEEPSEEK_API_KEY" \
-d '{
"model": "deepseek-flash",
"messages": [
{"role": "system", "content": "You are a concise assistant."},
{"role": "user", "content": "Summarise the difference between input and output tokens in two sentences."}
],
"max_tokens": 200
}'import os
from openai import OpenAI
client = OpenAI(
base_url="https://api.deepseek.com",
api_key=os.environ["DEEPSEEK_API_KEY"],
)
resp = client.chat.completions.create(
model="deepseek-flash", # or "deepseek-v4-pro"
messages=[
{"role": "system", "content": "You are a concise assistant."},
{"role": "user", "content": "Summarise the difference between input and output tokens in two sentences."},
],
max_tokens=200,
)
print(resp.choices[0].message.content)
print(resp.usage) # prompt_tokens, completion_tokens, cache hit/miss detailsReading the bill
Every response includes a usage object with prompt and completion token counts. DeepSeek's version also breaks the prompt tokens into cache-hit and cache-miss counts. Multiply each by the matching rate from the pricing page and you have the cost of that call. Log these three numbers from day one; they are the difference between knowing your bill and guessing it.
Worked example at DeepSeek's peak prices: a call to deepseek-flash with 3,000 prompt tokens and 500 completion tokens costs 3,000 × $0.0000003 + 500 × $0.0000012 = $0.0015. A thousand such calls is $1.50, or $0.75 off-peak. If the model spends 3,000 tokens thinking before the 500-token answer, completion tokens become 3,500 and the call costs 3,000 × $0.0000003 + 3,500 × $0.0000012 = $0.0051, more than three times as much.
Context caching
DeepSeek's context caching is automatic on the official API — there is nothing to enable. If the beginning of a new request is identical to the beginning of a recent one, those tokens are billed at the cache-hit input rate: $0.006 per 1M on V4.1 Flash at peak, 50 times below the $0.30 cache-miss rate. V4 Pro's cache hit is $0.044 against $1.32. Rates are on the pricing page. To benefit, keep the system prompt, instructions and any reference documents at the very start of the messages, and keep them byte-identical between calls.
Off-peak pricing
Both official models cost half outside peak hours. Peak is 01:00 to 04:00 and 06:00 to 10:00 UTC, Monday to Friday, excluding Chinese public holidays; every other hour is off-peak. The hours have changed before, so check the pricing page rather than hard-coding them. If you have batch work that is not time-sensitive — nightly summaries, backfills, evaluation runs — schedule it in the window. It is one of the few AI discounts that requires no engineering.
Knowledge check
You send the same 6,000-token system prompt plus reference document on every call, followed by a different user question each time. What is the cheapest change?