Skip to main content

Quick Start

The DeepSeek API uses an API format compatible with OpenAI. By modifying the configuration, you can use the OpenAI SDK or softwares compatible with the OpenAI API to access the DeepSeek API.

PARAMVALUE
base_url *       https://api.deepseek.com
api_keyapply for an API key

* To be compatible with OpenAI, you can also use https://api.deepseek.com/v1 as the base_url. But note that the v1 here has NO relationship with the model's version.

Invoke The Chat API

Once you have obtained an API key, you can access the DeepSeek API using the following example scripts. This is a non-stream example, you can set the stream parameter to true to get stream response.

# bash
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 helpful assistant."},
{"role": "user", "content": "Hello!"}
],
"stream": false
}'
# python3
# please install OpenAI SDK first: `pip3 install openai`
from openai import OpenAI

client = OpenAI(api_key="<deepseek api key>", base_url="https://api.deepseek.com")

response = client.chat.completions.create(
model="deepseek-chat",
messages=[
{"role": "system", "content": "You are a helpful assistant"},
{"role": "user", "content": "Hello"},
],
stream=False
)

print(response.choices[0].message.content)

Models

MODELDESCRIPTIONCONTEXT LENGTHMAX OUTPUT LENGTH
deepseek-chat (1)good at general tasks32K (2)4K
deepseek-coder (1)good at coding and math32K (2)4K
  • (1) The backend model of deepseek-chat and deepseek-coder has been updated to DeepSeek-V2 and DeepSeek-Coder-V2, you can access them without modification to the model name.
  • (2) The open-source DeepSeek-V2 and DeepSeek-Coder-V2 models support 128K context window, and the API/Web supports 32K context window.

The temperature Parameter

The default value of temperature is 1.0.

  • For deepseek-coder,we recommend users to use the default temperature (1.0).
  • For deepseek-chat,we recommend users to set the temperature according to their use case listed in below.
USE CASETEMPERATURE
Coding / Math   0.0
Data Cleaning / Data Analysis0.7
General Conversation1.0
Translation1.1
Creative Writing / Poetry1.25

Rate Limiting

The rate limit of each account is adjusted dynamically according to our real-time traffic pressure. When our servers are under high traffic pressure, you may receive 429 (Rate Limit Reached) or 503 (Server Overloaded). When this happens, please wait for a while and retry, and we also advise users to temporarily switch to the APIs of alternative LLM service providers, like OpenAI.

Token & Token Usage

Tokens are the basic units used by models to represent natural language text, and also the units we use for billing. They can be intuitively understood as 'characters' or 'words'. Typically, a Chinese word, an English word, a number, or a symbol is counted as a token.

Generally, the conversion ratio between tokens in the model and the number of characters is approximately as following:

  • 1 English character ≈ 0.3 token.
  • 1 Chinese character ≈ 0.6 token.

However, due to the different tokenization methods used by different models, the conversion ratios can vary. The actual number of tokens processed each time is based on the model's return, which you can view from the usage results.

Error Codes

When calling DeepSeek API, you may encounter errors. Here list the causes and solutions.

CODEDESCRIPTION
400 - Invalid FormatCause: Invalid request body format.
Solution: Please modify your request body according to the hints in the error message. For more API format details, please refer to DeepSeek API Docs.
401 - Authentication FailsCause: Authentication fails due to the wrong API key.
Solution: Please check your API key. If you don't have one, please create an API key first.
402 - Insufficient BalanceCause: You have run out of balance.
Solution: Please check your account's balance, and go to the Top up page to add funds.
422 - Invalid ParametersCause: Your request contains invalid parameters.
Solution: Please modify your request parameters according to the hints in the error message. For more API format details, please refer to DeepSeek API Docs.
429 - Rate Limit ReachedCause: You are sending requests too quickly.
Solution: Please pace your requests reasonably. We also advise users to temporarily switch to the APIs of alternative LLM service providers, like OpenAI.
500 - Server ErrorCause: Our server encounters an issue.
Solution: Please retry your request after a brief wait and contact us if the issue persists.
503 - Server OverloadedCause: The server is overloaded due to high traffic.
Solution: Please retry your request after a brief wait.