How to Access and Use the DeepSeek API
Ilias Ism
Apr 11, 2025
8 min read

In January 2025, DeepSeek released an open-source AI model that matched GPT-4's performance on several benchmarks while costing significantly less.
The model scored competitively on advanced math and coding tests, but the price caught developers' attention - several times cheaper than OpenAI or Anthropic for similar quality
The DeepSeek API makes this accessible through two modes: deepseek-chat for general tasks, and deepseek-reasoner for complex problem-solving that requires step-by-step thinking. Both use the same underlying model but process requests differently.
Here's what makes the DeepSeek API practical:
It works with the OpenAI SDK. If you've already built something with OpenAI's API, switching takes minutes. Just change the URL, add your DeepSeek API key, and you're done. No need to rewrite your code.
Plus, it's open-source. That means transparent pricing, no vendor lock-in, and you can even self-host if you want.
This guide walks you through everything you need to use the DeepSeek API - from getting your API key and understanding pricing, to making your first call, handling errors, and cutting token costs. Whether you're building a chatbot, a code tool, or testing AI features, you'll know how to integrate the DeepSeek API and decide if it's right for your project.
Let's start with the basics: understanding which DeepSeek model you actually need.
Understanding DeepSeek API Models: Chat vs Reasoner
![[object Object]](/_next/image?url=https%3A%2F%2Fcdn.sanity.io%2Fimages%2Fi6kpkyc7%2Fprod-dataset%2F9e568c5d81ba8cbda726c360fc0ca1d5c7093394-1376x246.webp&w=3840&q=75)
The DeepSeek API gives you access to two models, and picking the right one matters.
They're built for different jobs, and using the wrong one either wastes money or gives you weaker results.
Both models run on DeepSeek-V3.2-Exp, the same underlying AI, but they process requests differently.
Think of them as two modes: one for speed, one for accuracy.
deepseek-chat (Non-Thinking Mode)
deepseek-chat is the general-purpose model. Think of it as your everyday workhorse for most tasks.
This model handles a wide range of requests. From conversations and articles to code and technical tasks. It's designed to respond quickly without deep reasoning steps.
When to use deepseek-chat:
- Building chatbots or conversational AI
- Writing, editing, or summarizing content
- Answering general questions
- Simple coding tasks (generating scripts, explaining code)
- Data extraction and transformation
- Customer support automation
- Any task where speed matters more than showing detailed reasoning
If you're making something that needs to understand and respond naturally without step-by-step thinking, deepseek-chat is your go-to. It's fast, affordable, and handles most everyday AI tasks well.
deepseek-reasoner (Thinking Mode)
deepseek-reasoner is different. It's built specifically for tasks that need step-by-step thinking and accuracy.
This model doesn't just predict responses. It generates Chain-of-Thought (CoT) reasoning before giving you the final answer. You can see how it "thinks through" the problem, which makes it better for technical work where precision matters more than speed.
When to use deepseek-reasoner:
- Advanced math problems
- Complex coding tasks (debugging, algorithm design, optimization)
- Logic puzzles and multi-step problem solving
- Technical analysis that requires breaking down problems
- Tasks where you need the model to "show its work"
- Situations where accuracy is more critical than response time
Important note: deepseek-reasoner doesn't support Function Calling. If your project needs that feature, use deepseek-chat instead.
The trade-off? deepseek-reasoner generates more tokens because it explains its reasoning process. That means longer responses and slightly higher costs per request. But if accuracy matters and you need reliable results on technical tasks, it's worth it.
Technical Comparison
Here's what both models offer:
![[object Object]](/_next/image?url=https%3A%2F%2Fcdn.sanity.io%2Fimages%2Fi6kpkyc7%2Fprod-dataset%2Fada66a115f1f294ff39ef036866e0a24508e0e28-1424x1188.webp&w=3840&q=75)
Key differences:
- Context length: Both support 128K tokens -enough for very long conversations or large documents.
- Max output: deepseek-chat outputs up to 8K tokens (default 4K), while deepseek-reasoner can generate up to 64K tokens (default 32K) including its reasoning steps.
- Features: deepseek-chat supports Function Calling; deepseek-reasoner doesn't.
- Pricing: Both models cost exactly the same per token.
- Speed: deepseek-chat is faster; deepseek-reasoner takes longer due to reasoning steps.
Most projects start with deepseek-chat because it handles 80% of use cases. You only switch to deepseek-reasoner when you hit tasks that need real reasoning power.
Now that you know which model fits your needs, let's talk about what it actually costs to use them.
DeepSeek API Pricing & Cost Analysis
One of the biggest reasons developers choose the DeepSeek API is the pricing. It's significantly cheaper than major alternatives while delivering comparable performance.
The DeepSeek API uses token-based pricing, you pay for what you use.
Both deepseek-chat and deepseek-reasoner (which both run DeepSeek-V3.2-Exp) cost the same:
DeepSeek API Pricing:
- Input tokens (cache hit): $0.028 per 1M tokens
- Input tokens (cache miss): $0.28 per 1M tokens
- Output tokens: $0.42 per 1M tokens
Cache hits happen when you reuse prompts. The API recognizes them and charges 10x less. Cache misses are new, unique prompts at the standard rate.
Here's how DeepSeek compares to other providers:
![[object Object]](/_next/image?url=https%3A%2F%2Fcdn.sanity.io%2Fimages%2Fi6kpkyc7%2Fprod-dataset%2F80f743bd912847a5973c2037ce9a11d0be6e8f22-1510x498.webp&w=3840&q=75)
(Note: DeepSeek pricing shown is for cache miss; cache hits are $0.028)
What this means in practice:
DeepSeek V3.2-Exp is roughly 9x cheaper than GPT-4o and competitive with budget models like Gemini Flash while delivering higher quality.
Real example: Processing 1 million input tokens and generating 500,000 output tokens costs:
- DeepSeek: $0.28 + $0.21 = $0.49
- GPT-4o: $2.50 + $5.00 = $7.50
- Gemini Flash: $0.10 + $0.20 = $0.30
For high-volume applications, DeepSeek offers the best balance of cost and quality. With cache hits, costs drop even further, making it ideal for chatbots and repeated tasks.
With pricing covered, let's get you set up with the DeepSeek API.
How to Access the DeepSeek API
Getting started with the DeepSeek API is straightforward. You'll need an API key, install the OpenAI SDK, and make your first call. The whole process takes about 5 minutes.
Step 1: Get Your DeepSeek API Key
Go to the DeepSeek API Platform and sign up for an account.
Once you're logged in, navigate to the API Keys section in your dashboard. Click "Create New Key" to generate your key.
Important: Your API key is only shown once at creation. Copy it immediately and store it somewhere secure. You won't be able to see it again.
Never share your API key publicly or commit it to version control. Treat it like a password.
Step 2: Install the OpenAI SDK
The DeepSeek API is fully compatible with the OpenAI SDK, which means you don't need to learn a new toolkit or deal with raw HTTP requests.
The OpenAI SDK is a pre-built library that handles all the API communication for you. like authentication, request formatting, error handling, and response parsing. It makes integration simple and clean.
Install it using pip:
1pip install openai
Step 3: Make Your First DeepSeek API Call
Here's a basic example to test your connection:
main.py
1from openai import OpenAI23client = OpenAI(4 api_key="<Your_DeepSeek_API_Key>",5 base_url="https://api.deepseek.com"6)78response = client.chat.completions.create(9 model="deepseek-chat",10 messages=[11 {"role": "system", "content": "You are a helpful assistant"},12 {"role": "user", "content": "Hello"},13 ],14 stream=False15)1617print(response.choices[0].message.content)
What's happening here:
- from openai import OpenAI - Imports the OpenAI SDK
- api_key - Your DeepSeek API key (replace the placeholder)
- base_url - Points to DeepSeek's API instead of OpenAI's
- model - Use "deepseek-chat" or "deepseek-reasoner"
- messages - Your conversation: system prompt + user input
- stream=False - Get the complete response at once (set to True for streaming)
Run this code and you should get a response back from the model.
To use deepseek-reasoner instead:
Just change the model parameter:
python
model="deepseek-reasoner"
Everything else stays the same.
Now that you're connected, let's make sure you're handling your API key securely.
Security & Best Practices
Keeping your DeepSeek API key secure is critical. A leaked key means anyone can use your account and rack up charges.
Store Keys in Environment Variables
Don't put your API key directly in your code like this:
`api_key="sk-abc123..." # ❌ Bad practice
Instead, use environment variables. This keeps your key out of your codebase and version control. Create a `.env` file in your project directory:
DEEPSEEK_API_KEY=your_api_key_here`
Then load it in your code:
python
1import os2from dotenv import load_dotenv3from openai import OpenAI45load_dotenv()67client = OpenAI(8 api_key=os.environ.get('DEEPSEEK_API_KEY'),9 base_url="https://api.deepseek.com"10)
Important: Add .env to your .gitignore file so it never gets committed to Git.
Quick Security Checklist
- Store API keys in environment variables
- Add .env to .gitignore
- Rotate keys regularly if exposed
- Use separate keys for development and production
- Monitor your API usage for unexpected spikes
With security covered, let's look at how to handle errors when things go wrong.
Error Handling & Troubleshooting
When working with the DeepSeek API, things can go wrong. Proper error handling keeps your application stable.
Common Errors and Fixes
Authentication Error (401): Invalid API key
Fix: Check your key is loaded correctly from environment variables
Rate Limit Error (429): Too many requests
Fix: Add retry logic with delays between attempts
Invalid Request (400): Wrong format or model name
Fix: Verify you're using "deepseek-chat" or "deepseek-reasoner"
Timeout Error: Request took too long
Fix: Increase timeout or split large requests into smaller chunks
Basic Error Handling
Here's a simple error handling example:
1from openai import OpenAI2import os3from dotenv import load_dotenv45load_dotenv()67client = OpenAI(8 api_key=os.environ.get('DEEPSEEK_API_KEY'),9 base_url="https://api.deepseek.com"10)1112try:13 response = client.chat.completions.create(14 model="deepseek-chat",15 messages=[16 {"role": "system", "content": "You are a helpful assistant"},17 {"role": "user", "content": "Hello"}18 ]19 )20 print(response.choices[0].message.content)2122except Exception as e:23 print(f"Error: {str(e)}")
Quick Troubleshooting
- API not responding? Check your internet connection
- Weird outputs? Verify your prompt and model choice
- High costs? Check token usage in the response
- Model not found? Use exact names: "deepseek-chat" or "deepseek-reasoner"
Now let’s have a look at optimizing your prompts to reduce costs.
Optimizing DeepSeek API Costs
Token usage directly impacts your bill.
Here are practical strategies to reduce costs without sacrificing quality.
Adjust Temperature Based on Task
Temperature controls how creative or predictable the model's responses are. Lower values save tokens by keeping responses focused.
For code generation or math: Use temperature=0.0 for consistent, direct answers.
For general tasks: Stick with default temperature=1.0 for balanced responses.
For creative writing: Use temperature=1.3-1.5 for more variety, but expect longer outputs.
1response = client.chat.completions.create(2 model="deepseek-chat",3 messages=[{"role": "user", "content": "Write a Python function to sort a list"}],4 temperature=0.0 # Focused, minimal output5)
Note: Temperature doesn't work with deepseek-reasoner, it uses its own reasoning process.
Use Structured Output Formats
Request specific formats like JSON to keep responses short and scannable:
1response = client.chat.completions.create(2 model="deepseek-chat",3 messages=[4 {"role": "system", "content": "Return only JSON format"},5 {"role": "user", "content": "Extract name and email from: John Doe, john@example.com"}6 ]7)
This cuts unnecessary explanation text and makes parsing easier.
Optimize Chain-of-Thought Usage
Chain-of-Thought (CoT) with deepseek-reasoner is powerful but generates more tokens. Use it wisely:
❌ Don't ask for detailed explanations:
"Explain step-by-step how to calculate the GCD of 48 and 18, showing all work."
✅ Ask for concise reasoning:
"Find GCD of 48 and 18 using Euclidean algorithm. Show steps only."
Example comparison:
Verbose prompt: "How do I find the greatest common divisor of 48 and 18?"
Output: ~900 tokens with full explanation
Cost: ~$0.002
Optimized prompt: "GCD of 48 and 18. Show calculation steps only."
Output: ~150 tokens
Cost: ~$0.0003
Savings: 83% cost reduction with same answer.
Leverage Cache Hits
Reuse system prompts and common context to trigger cache hits:
python
system_prompt = "You are a helpful coding assistant" *# Reuse this# First call - cache miss ($0.28/1M tokens)# Subsequent calls - cache hit ($0.028/1M tokens)*
For applications with repeated patterns, cache hits can cut input costs by 90%.
Keep Prompts Tight
Every word in your prompt costs tokens. Be direct:
❌ Verbose: "I would like you to please help me write a function that can sort a list of numbers in ascending order."
✅ Concise: "Write a function to sort a list ascending."
Same result, fewer tokens.
Wrapping Up
The DeepSeek API offers a powerful combination of performance and affordability that's hard to ignore. With OpenAI SDK compatibility, straightforward pricing, and two models designed for different use cases, it provides a solid foundation for AI-powered applications.
You now know how to:
- Choose between deepseek-chat and deepseek-reasoner based on your needs
- Set up your API key and make your first call
- Handle errors and secure your implementation
- Optimize prompts to keep costs low
Whether you're building a chatbot, a code assistant, or processing large volumes of text, the DeepSeek API gives you the tools to do it efficiently.
Want to skip the API setup entirely? If you're building a chatbot or conversational AI and want to get started even faster, platforms like Chatbase let you deploy DeepSeek-powered agents without writing code, just upload your data and you're ready to go.
Ready to start building? Grab your API key from the DeepSeek Platform and put what you've learned into action.
Share this article:







