How to beginner · 3 min read

How to call DeepSeek with LiteLLM

Quick answer
Use the openai Python SDK configured with DeepSeek's API endpoint and your API key, then call client.chat.completions.create with the deepseek-chat model. LiteLLM can invoke this by setting the base_url to DeepSeek's API and using the standard OpenAI-compatible interface.

PREREQUISITES

  • Python 3.8+
  • DeepSeek API key
  • pip install openai>=1.0

Setup

Install the openai Python package (compatible with LiteLLM usage) and set your DeepSeek API key as an environment variable.

  • Run pip install openai to install the SDK.
  • Export your API key in your shell: export DEEPSEEK_API_KEY='your_api_key_here'.
bash
pip install openai

Step by step

Use the OpenAI-compatible SDK to call DeepSeek by specifying the base_url parameter pointing to DeepSeek's API endpoint. This example sends a chat completion request to the deepseek-chat model.

python
import os
from openai import OpenAI

# Initialize client with DeepSeek API key and base URL
client = OpenAI(
    api_key=os.environ["DEEPSEEK_API_KEY"],
    base_url="https://api.deepseek.com"
)

# Prepare messages for chat completion
messages = [
    {"role": "user", "content": "Hello, how can I use DeepSeek with LiteLLM?"}
]

# Call DeepSeek chat completion endpoint
response = client.chat.completions.create(
    model="deepseek-chat",
    messages=messages
)

# Extract and print the response text
print(response.choices[0].message.content)
output
Hello! You can integrate DeepSeek with LiteLLM by configuring the OpenAI-compatible client to use DeepSeek's API endpoint and model, then sending chat completion requests as usual.

Common variations

You can switch models to deepseek-reasoner for reasoning tasks or adjust parameters like max_tokens and temperature. For asynchronous calls, use Python's asyncio with the OpenAI SDK's async methods. LiteLLM can proxy requests by setting base_url accordingly.

python
import asyncio
from openai import OpenAI

async def async_call():
    client = OpenAI(
        api_key=os.environ["DEEPSEEK_API_KEY"],
        base_url="https://api.deepseek.com"
    )
    response = await client.chat.completions.acreate(
        model="deepseek-chat",
        messages=[{"role": "user", "content": "Async call example."}]
    )
    print(response.choices[0].message.content)

asyncio.run(async_call())
output
This is an example of an asynchronous call to DeepSeek's chat model using the OpenAI SDK.

Troubleshooting

  • If you get authentication errors, verify your DEEPSEEK_API_KEY environment variable is set correctly.
  • For connection issues, ensure your network allows HTTPS requests to https://api.deepseek.com.
  • If the model name is invalid, confirm you are using deepseek-chat or deepseek-reasoner.

Key Takeaways

  • Use the OpenAI SDK with the DeepSeek base_url to call DeepSeek models seamlessly.
  • Set your DeepSeek API key in the environment variable DEEPSEEK_API_KEY for authentication.
  • LiteLLM can integrate by configuring the OpenAI client with DeepSeek's endpoint and models.
  • Async calls and model parameter tuning are supported via the standard OpenAI SDK interface.
  • Check model names and network connectivity if you encounter errors.
Verified 2026-04 · deepseek-chat, deepseek-reasoner
Verify ↗