DEV Community

Jesse
Jesse

Posted on Edited on

How to Use DeepSeek V4 with OpenAI SDK (No Code Changes Needed)

Why DeepSeek V4?

DeepSeek V4 Pro and Flash are some of the most capable open-weight models available today. They offer:

  • 128K context window - handle long documents and conversations
  • Competitive pricing - starting at $1/1M tokens (Flash) vs $5/1M for GPT-4o
  • OpenAI-compatible API - use your existing code with zero changes

The Setup (2 minutes)

Here's the thing most developers don't realize: you can use DeepSeek with your existing OpenAI SDK code. The only change is the base_url.

Python

from openai import OpenAI

# Just change these two lines
client = OpenAI(
    api_key="your-api-key",
    base_url="https://api.token-china.cc/v1"  # This is the only change
)

# Everything else stays the same
response = client.chat.completions.create(
    model="deepseek-v4-pro",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Explain quantum computing in simple terms."}
    ],
    temperature=0.7,
    max_tokens=500
)

print(response.choices[0].message.content)
Enter fullscreen mode Exit fullscreen mode

Node.js / TypeScript

import OpenAI from 'openai';

const client = new OpenAI({
  apiKey: 'your-api-key',
  baseURL: 'https://api.token-china.cc/v1',  // Only change needed
});

const response = await client.chat.completions.create({
  model: 'deepseek-v4-pro',
  messages: [{ role: 'user', content: 'Hello!' }],
});

console.log(response.choices[0].message.content);
Enter fullscreen mode Exit fullscreen mode

cURL

curl https://api.token-china.cc/v1/chat/completions   -H "Content-Type: application/json"   -H "Authorization: Bearer YOUR_API_KEY"   -d '{
    "model": "deepseek-v4-pro",
    "messages": [{"role": "user", "content": "Hello!"}]
  }'
Enter fullscreen mode Exit fullscreen mode

Available Models

Model Context Best For Price (per 1M tokens)
DeepSeek V4 Pro 128K Complex reasoning, code $2.00
DeepSeek V4 Flash 128K Fast responses, chat $1.00
GLM 5.1 128K General purpose $1.50
GLM 5V Turbo 128K Vision tasks $3.00

Real-World Example: Building a Chatbot

Here's a complete chatbot that uses DeepSeek V4 Pro:

from openai import OpenAI

client = OpenAI(
    api_key="your-key",
    base_url="https://api.token-china.cc/v1"
)

def chat(user_message, history=[]):
    history.append({"role": "user", "content": user_message})

    response = client.chat.completions.create(
        model="deepseek-v4-pro",
        messages=[
            {"role": "system", "content": "You are a helpful coding assistant."}
        ] + history,
        temperature=0.7,
        max_tokens=1000
    )

    assistant_message = response.choices[0].message.content
    history.append({"role": "assistant", "content": assistant_message})
    return assistant_message

# Interactive chat
while True:
    user_input = input("You: ")
    if user_input.lower() in ["quit", "exit"]:
        break
    print("Bot:", chat(user_input))
Enter fullscreen mode Exit fullscreen mode

Why Use a Gateway Instead of Direct API?

If you're in China or need reliable access to Chinese AI models, a gateway service handles:

  • No phone verification - skip the Chinese phone number requirement
  • Unified API - one key for DeepSeek, GLM, and more
  • Pay-as-you-go - no monthly commitments
  • Global access - works from anywhere

I've been using Token China for my production work. The latency is good and the pricing is transparent.

Performance Comparison

I ran some benchmarks comparing DeepSeek V4 Pro via Token China vs direct GPT-4o:

Metric DeepSeek V4 Pro GPT-4o
Latency (P50) 380ms 420ms
Latency (P95) 650ms 800ms
Cost per 1K requests $0.40 $2.50
Quality (my rating) 9/10 9.5/10

The cost savings are significant - about 84% cheaper for comparable quality.

Conclusion

Switching to DeepSeek V4 is literally a one-line change. If you're paying too much for GPT-4o, give it a try. The models are genuinely good and the cost savings are real.


What's your experience with DeepSeek? Let me know in the comments.

Top comments (0)