AI Model Wiki

    Skip to main content
    10 min read
    Lesson 3 of 5

    Using the DeepSeek API in Your Projects

    From the course DeepSeek AI: Open-Source Reasoning Power

    DeepSeek's API is OpenAI-compatible, making it a drop-in replacement for GPT-4 in most applications — at a fraction of the cost.

    API Setup

    1. Sign up at platform.deepseek.com
    2. Generate an API key
    3. Use the OpenAI SDK with DeepSeek's base URL
    import OpenAI from 'openai';
    
    const client = new OpenAI({
      baseURL: 'https://api.deepseek.com',
      apiKey: 'your-deepseek-api-key',
    });
    
    // Chat completion (V3)
    const response = await client.chat.completions.create({
      model: 'deepseek-chat',
      messages: [
        { role: 'system', content: 'You are a helpful assistant.' },
        { role: 'user', content: 'Explain quantum computing simply.' }
      ],
    });
    
    // Reasoning (R1)
    const reasoning = await client.chat.completions.create({
      model: 'deepseek-reasoner',
      messages: [
        { role: 'user', content: 'Solve: If 2^x + 2^x = 2^10, find x.' }
      ],
    });
    

    Pricing Comparison (per million tokens)

    ProviderInputOutput
    DeepSeek V3$0.27$1.10
    DeepSeek R1$0.55$2.19
    GPT-4o$2.50$10.00
    Claude 3.5 Sonnet$3.00$15.00
    GPT-4 Turbo$10.00$30.00

    Migration from OpenAI

    Since DeepSeek is OpenAI-compatible, migrating is often just:

    1. Change the base URL
    2. Change the API key
    3. Update model names: gpt-4 → deepseek-chat or deepseek-reasoner

    Advanced Features

    • JSON mode — Set response_format: { type: "json_object" } for structured output
    • Function calling — Supports OpenAI-style function/tool calling
    • Streaming — Real-time token streaming for chat interfaces
    • Context window — 64K tokens for V3, 64K for R1
    • Temperature control — 0.0 for deterministic, up to 2.0 for creative outputs

    Rate Limits & Best Practices

    • Start with the free tier to evaluate quality for your use case
    • Implement retry logic with exponential backoff
    • Cache responses for repeated queries to reduce costs further
    • Use V3 for simple tasks and R1 only when reasoning is needed
    • Monitor token usage — R1's thinking tokens count toward costs

    When to Use DeepSeek API vs Alternatives

    ✅ Use DeepSeek when:

    • Cost is a priority (high-volume applications)
    • You need strong math/coding capabilities
    • OpenAI-compatible API matters for easy migration
    • You want competitive quality without premium pricing

    ⚠️ Consider alternatives when:

    • You need the absolute latest model capabilities (GPT-5, Claude 4)
    • Your use case requires specific safety guardrails
    • You need guaranteed uptime SLAs for production
    • Regulatory requirements mandate specific providers

    Key Takeaways

    • •DeepSeek API is OpenAI-compatible — swap base URL and API key to migrate
    • •Pricing is 10-50x cheaper than OpenAI for comparable quality
    • •Use deepseek-chat for general tasks and deepseek-reasoner for complex reasoning
    • •Supports JSON mode, function calling, streaming, and 64K context windows
    • •Use V3 for simple tasks and R1 only when reasoning is needed to optimize costs
    0 of 5 lessons marked complete

    Progress is kept in this browser only.