DEV Community

Mattias chaw
Mattias chaw

Posted on

How to Migrate from OpenAI API to DeepSeek in 5 Minutes

The One-Line Migration

If you're paying OpenAI $20/month for GPT-4o access and burning through tokens faster than expected, here's some good news: DeepSeek V4 Pro is a drop-in replacement for the OpenAI API. One line of code is all it takes.

# Before (OpenAI)
client = OpenAI(api_key="sk-...")

# After (DeepSeek)
client = OpenAI(
    api_key="sk-your-deepseek-key",
    base_url="https://api.deepseek.com/v1"  # <- This is all you change
)
Enter fullscreen mode Exit fullscreen mode

That's it. No new SDK. No function signature changes. Your existing chat.completions.create() calls work exactly the same.

Why Developers Are Switching

The numbers don't lie:

Model Input $/1M tokens Output $/1M tokens Context
GPT-4o $2.50 $10.00 128K
GPT-4o-mini $0.15 $0.60 128K
DeepSeek V4 Pro $0.50 $2.19 128K
DeepSeek Chat $0.14 $0.28 128K

DeepSeek Chat is cheaper than GPT-4o-mini and performs closer to GPT-4o. DeepSeek V4 Pro costs 80% less than GPT-4o for comparable quality.

Step-by-Step Migration Guide

Step 1: Get Your API Key

Option A — Direct from DeepSeek (requires Chinese phone number):

  • Register at platform.deepseek.com
  • Verify with Chinese phone (+86)
  • Navigate to API Keys and Create

Option B — Through a unified API gateway (no Chinese phone needed):

  • AIWave provides instant access to DeepSeek + 50+ Chinese models
  • OpenAI-compatible endpoint, no phone verification required

Step 2: Change the Base URL

import openai

# OpenAI original
openai.api_base = "https://api.openai.com/v1"

# DeepSeek (direct)
openai.api_base = "https://api.deepseek.com/v1"

# Or via unified gateway (access multiple Chinese models)
openai.api_base = "https://api.aiwave.live/v1"
Enter fullscreen mode Exit fullscreen mode

Step 3: Map Your Models

OpenAI Model DeepSeek Equivalent GLM Equivalent
gpt-4o deepseek-chat (V4 Pro) glm-5.1
gpt-4o-mini deepseek-chat glm-4-flash
o1 / o3 deepseek-reasoner N/A
gpt-4-vision glm-4v N/A

Step 4: Test Your Integration

from openai import OpenAI

client = OpenAI(
    api_key="sk-your-key",
    base_url="https://api.aiwave.live/v1"
)

response = client.chat.completions.create(
    model="deepseek-chat",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Explain quantum computing in one paragraph."}
    ],
    temperature=0.7,
    max_tokens=500
)

print(response.choices[0].message.content)
print(f"Tokens used: {response.usage.total_tokens}")
Enter fullscreen mode Exit fullscreen mode

Real Performance Benchmarks

Code Generation (Python)

Task: Write a concurrent web scraper with rate limiting.

DeepSeek V4 Pro produced working code with proper asyncio.Semaphore handling, error retries, and type hints. GPT-4o used ThreadPoolExecutor. Both valid, different approaches.

Verdict: Tie

Reasoning (Math)

Task: Two trains, 300 km apart, approaching at 60 km/h and 80 km/h. Second train departs 30 minutes later. When do they meet?

Both solved correctly. DeepSeek showed more detailed step-by-step reasoning.

Verdict: DeepSeek slightly better

Chinese-to-English Translation

DeepSeek handled Chinese technical terms better. GPT-4o made 2 minor terminology errors.

Verdict: DeepSeek better for Chinese content

Creative Writing

GPT-4o produced more varied, engaging prose. DeepSeek more clinical.

Verdict: GPT-4o better for creative tasks

Handling Edge Cases

Streaming works identically:

stream = client.chat.completions.create(
    model="deepseek-chat",
    messages=[{"role": "user", "content": "Write a haiku about coding"}],
    stream=True
)
for chunk in stream:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="")
Enter fullscreen mode Exit fullscreen mode

Function Calling:

tools = [{
    "type": "function",
    "function": {
        "name": "get_weather",
        "description": "Get current weather for a city",
        "parameters": {
            "type": "object",
            "properties": {"city": {"type": "string"}},
            "required": ["city"]
        }
    }
}]

response = client.chat.completions.create(
    model="deepseek-chat",
    messages=[{"role": "user", "content": "Weather in Tokyo?"}],
    tools=tools
)
Enter fullscreen mode Exit fullscreen mode

JSON Mode:

response = client.chat.completions.create(
    model="deepseek-chat",
    messages=[{"role": "user", "content": "List 3 programming languages with release year as JSON"}],
    response_format={"type": "json_object"}
)
Enter fullscreen mode Exit fullscreen mode

Common Pitfalls

1. Token Counting Differences

DeepSeek uses a different tokenizer. Always monitor actual usage:

print(f"Prompt tokens: {response.usage.prompt_tokens}")
print(f"Completion tokens: {response.usage.completion_tokens}")
Enter fullscreen mode Exit fullscreen mode

2. System Prompt Behavior

DeepSeek is more literal with system prompts. If your prompt uses nuanced instructions, test thoroughly before deploying.

The Multi-Model Strategy

The smartest approach: use the right model for each task.

def route_task(task_type: str) -> str:
    routing = {
        "code": "deepseek-chat",
        "reasoning": "deepseek-reasoner",
        "translation_zh": "glm-5.1",
        "creative": "glm-5.1",
        "vision": "glm-4v",
        "fast": "glm-4-flash",
    }
    return routing.get(task_type, "deepseek-chat")

model = route_task("code")
response = client.chat.completions.create(model=model, messages=[...])
Enter fullscreen mode Exit fullscreen mode

With a unified API gateway, all these models are accessible through a single endpoint and API key.

Cost Savings: Real Numbers

For a mid-size SaaS app processing 10M tokens per month:

Scenario Monthly Cost
GPT-4o (all traffic) $125
GPT-4o-mini (all traffic) $7.50
DeepSeek V4 Pro $27
Smart routing (mix) $12

Smart routing saves 90% versus GPT-4o while maintaining quality.

Next Steps

  1. Test DeepSeek with your prompts — literally change base_url and test
  2. Monitor quality — track user satisfaction during transition
  3. Implement model routing — right model for each task
  4. Watch your costs drop — set up cost monitoring

Building with Chinese AI models? AIWave provides unified API access to 50+ models — DeepSeek, GLM, Kimi, ERNIE, and more. No Chinese phone number required. Get $5 free on signup.

Top comments (0)