OverpayingForAIPricing desk

Lesson 7 of 8 · 12 min read · Beginner → Intermediate

API quickstart — DeepSeek

When the API beats the free app, how to call the OpenAI-compatible endpoint from curl and Python, and how context caching and off-peak pricing cut the bill.

In this lesson you will

  • Decide whether you need the API at all
  • Make a first call to api.deepseek.com with curl and the OpenAI Python SDK
  • Read a usage object and work out what a request cost
  • Use context caching and off-peak windows to pay less for the same calls

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.

Tokens and the context windowA token is roughly ¾ of an English word. The context window is the maximum tokens the model can hold at once — everything you send plus everything it writes.System prompt + instructionsConversation history + documentsNew answerfreeWhy it costs moneyEvery turn re-sends the whole history. A 40-message chat with a pasted PDF bills that PDF 40 times unless the provider caches it.“1M context” is a ceiling, not a target. Filling it on every request is the fastest way to overpay.rule of thumb: 1,000 tokens ≈ 750 words ≈ 1.5 pages
Figure 1.Input tokens are everything you send, including the system prompt and prior turns. Output tokens are everything the model generates — including any hidden reasoning the model does.

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.

bashFirst request with curl. Replace $DEEPSEEK_API_KEY with your key.
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
  }'
pythonSame call with the official OpenAI Python SDK pointed at DeepSeek.
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 details

Reading 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

Prompt caching: stop paying full price for the same prefixIf the start of your request is identical each time (system prompt, docs, tool list), the provider can serve it from cache at a fraction of the input price.Request 1Stable prefix — written to cache (full price)new questionRequest 2Stable prefix — cache hit (discounted)new questionRequest 3Stable prefix — cache hit (discounted)new questionRule: put the unchanging parts first, the changing parts last. A timestamp at the top of a system prompt silently kills the cache on every call.
Figure 2.When the start of a request matches a recent request, DeepSeek serves the matching prefix from cache and bills those tokens at the lower cache-hit rate. Put stable content first so the prefix matches.

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?

Lesson FAQ

Does the DeepSeek API work with the OpenAI SDK?

Yes. Set base_url to https://api.deepseek.com and use your DeepSeek key. The model names are deepseek-flash (V4.1 Flash) and deepseek-v4-pro.

How do I enable context caching on DeepSeek?

You do not need to. On the official API it is automatic; matching prefixes are billed at the cache-hit rate and the usage object reports hit and miss counts. Just keep your stable content at the start of the request.

What are DeepSeek off-peak hours?

On 2026-10-27 DeepSeek's pricing page listed peak hours as 01:00–04:00 and 06:00–10:00 UTC on weekdays. Every other hour, weekends and Chinese public holidays are off-peak at half price. The hours have changed before, so check https://api-docs.deepseek.com/quick_start/pricing.

Finished reading?

Mark it done to track your progress through the course.

Save progress across devices

Get a private link that restores your lessons on any device. Email is optional and only used to send you the link.

Compare, calculate, decide — for DeepSeek

If our calculators helped you cut down on hidden AI wallet leaks, thanks for using them. A tiny fraction of your savings is what keeps our pricing indexes updated daily.

Not sure which AI is cheapest for your use case? Find out in 30 seconds — no signup required.

AI cost intelligence

Stop overpaying for AI tools

Join the OverpayingForAI list for pricing updates, cheaper alternatives, and practical buying guidance.

Now tracking 50+ AI tools, models, platforms, subscriptions, coding tools, and automation products.

We use your email only for OverpayingForAI updates. Unsubscribe anytime.