OverpayingForAIPricing desk

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

API quickstart

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 hidden reasoning on deepseek-reasoner.

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 are deepseek-chat and deepseek-reasoner. 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-chat",
    "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-chat",  # or "deepseek-reasoner"
    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 using catalogue prices at the time of writing: a call to deepseek-chat (V3.2 in our catalogue) with 3,000 prompt tokens and 500 completion tokens costs about 3,000 × $0.00000027 + 500 × $0.0000004 ≈ $0.001. A thousand such calls is about a dollar. The same call to the reasoner might generate 3,000 reasoning tokens plus the 500-token answer, so completion tokens become 3,500 at R1 output rates — several times the cost.

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, which is far below the standard rate. The exact discount is 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

The official API also publishes off-peak discount windows during which both models are billed at reduced rates. The hours and percentages are on the pricing page and have changed before, so do not hard-code 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-chat and deepseek-reasoner.

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?

The official API publishes discount windows on its pricing page. They have changed over time, so check https://api-docs.deepseek.com/quick_start/pricing rather than relying on a number you saw elsewhere.

Finished reading?

Mark it done to track your progress through the course.

Compare, calculate, decide — for DeepSeek

If our calculators helped you cut down on hidden AI wallet leaks, consider buying us a coffee. A tiny fraction of your savings 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.