A DeepSeek API example, read field by field

By Christophe Paka · Facts checked on 23 October 2026

"I've never used any AI through an API before," one reader wrote; only through a subscription. You copy a sample, run it, and a greeting prints. But what did you just send, and what did it cost?

Skip that question and the first surprise comes with the bill, not with an error.

This DeepSeek API example puts the request beside the reply. The figure below shows which fields you set, where the answer text sits, and which two counts decide what you pay.

A DeepSeek request beside its response: a POST to /chat/completions with a bearer key, the Flash model name, messages, thinking enabled and effort high; the response holds the answer text and a usage block with cache hit and miss counts.
Figure 3.1: One call, every field. The reply's usage block is your bill, line by line.

How to read this figure

  1. Start at the request's first lines. The first line, POST /chat/completions, says where the request goes: DeepSeek's chat address. Every call starts from the base URL, https://api.deepseek.com, the web address your code sends to. The grey Authorization line carries your API key, the secret string that ties each call to your account. Anyone with it can spend your money, so keep it out of your code.

  2. Name the model and the messages. model picks the model by name, here deepseek-flash. messages holds a system message, which sets the role, and a user message, the question. The format copies OpenAI's, so OpenAI's ready made Python library works with two settings changed: base_url="https://api.deepseek.com" and model="deepseek-flash".

  3. Read the two blue lines. thinking.type and reasoning_effort set thinking mode: the model writes out its working before the answer. It is on by default, so the sample only spells it out. Thinking adds output too, so a default bill runs higher than one with thinking off.

  4. Find the answer on the right. The answer text sits at choices[0].message.content. Read it left to right: choices is the list of answers, [0] picks the first one (counting starts at zero), then its message, then that message's text. That is the one field most scripts print. Print the whole response once, so you see what else comes back.

  5. Read usage as your bill. usage counts the tokens you pay for. prompt_cache_hit_tokens counts input that repeats text DeepSeek has already seen, billed at a far lower price. prompt_cache_miss_tokens counts new input at the full price. The price grid shows what each side costs.

Key takeaways

  • Keep your API key in an environment variable, never inside code you share.
  • Log the usage block after every test call, so cost surprises show up early.
  • Work out your cache hit rate from the two input counts: hits divided by hits plus misses.

Try this: send this DeepSeek API example from your terminal

You need a terminal (the window where you type commands, the Terminal app on a Mac) and a few dollars. Every call goes to DeepSeek's servers in China, so test with made up text, never customer data.

  1. Sign in at platform.deepseek.com, open API keys, then Create new API key. Copy the key, which appears once, and pay in a few dollars on the Top up page.
  2. Put the key in an environment variable, a named value your terminal keeps for programs to read: export DEEPSEEK_API_KEY="your_key_here"
  3. In the same window, send the call with curl, a small program that sends web requests: 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 helpful assistant."}, {"role": "user", "content": "Hello!"}], "stream": false}'
  4. In the reply, find the greeting under choices and the token counts under usage, the fields from the figure.

Expected result: A reply with a short greeting and a usage block of token counts. Error 401 means your key is wrong or not set in this window; error 402 means the money you paid in ran out.

Questions readers ask

Which field of the DeepSeek reply holds the answer text?

choices[0].message.content: the first choice, its message, its content. Print that field alone and you see a short greeting. Print response.usage as well to see the token counts you pay for.

Can I use the OpenAI library with the DeepSeek API?

Yes. DeepSeek's main format copies OpenAI's request shape, so code written for OpenAI works after two changes: the base URL and the model name. Your words still go to DeepSeek, not to OpenAI. If you see an error from OpenAI, the code still sends to OpenAI, so fix the base URL.

What do errors 401 and 402 mean on the DeepSeek API?

Error 401, Authentication Fails, means your key is wrong or not set in the terminal window you run from. Error 402, Insufficient Balance, means the money you paid in ran out: the API is prepaid, and calls stop at zero. Top up, then run again.

Sources: DeepSeek API Docs, Your First API Call; DeepSeek API Docs, Error Codes; DeepSeek API Docs, Models and Pricing.

This figure is from DeepSeek Made Crystal Clear, chapter 3.

The chapter builds the idea step by step, with recipes you can follow, a Try it to check yourself, and every source.

Get the book

All diagrams