DeepSeek has become one of the most cost-effective ways to add a capable large language model to a project. The API is OpenAI-compatible, the pricing is among the lowest of any major provider, and you can be making real calls in Python within a few minutes. This tutorial shows you exactly how, with current 2026 model names and working code.
By the end you will know how to authenticate, send a basic chat request, use the reasoning model for harder problems, stream responses token by token, and wrap it all in a reusable function.
What Is the DeepSeek API?
DeepSeek is an AI company whose models handle chat, coding, and step-by-step reasoning. Its API speaks the same format as the OpenAI API, which means you can use the official openai Python library and only change two things: the base URL and your API key. If you have used the OpenAI SDK before, you already know most of this.
The API also offers an Anthropic-compatible endpoint at https://api.deepseek.com/anthropic, so tools built for either ecosystem can point at DeepSeek with minimal changes.
Pricing in 2026
DeepSeek is cheap. Input tokens start at roughly $0.14 per million on the standard tier, with output a little higher, and matching prompt prefixes are served from cache at a fraction of that price. For most side projects and prototypes, your monthly bill stays in the range of a coffee. Always confirm the live numbers on the official pricing page before you scale, since rates and promos change.
Step 1: Get an API Key and Install the SDK
Create an account on the DeepSeek platform, open the API keys section, and generate a key. Keep it secret — never commit it to a public repo. Then install the OpenAI SDK:
pip install openai
Step 2: Make Your First Call
Create a file called deepseek_demo.py. Point the OpenAI client at DeepSeek's base URL and call the chat model:
from openai import OpenAI
client = OpenAI(
api_key="YOUR_DEEPSEEK_KEY",
base_url="https://api.deepseek.com",
)
response = client.chat.completions.create(
model="deepseek-chat",
messages=[
{"role": "system", "content": "You are a concise, helpful assistant."},
{"role": "user", "content": "Explain what an API is in one sentence."},
],
)
print(response.choices[0].message.content)
Run it and you will get a one-sentence answer. The system message sets the behavior; the user message is the question. That is the whole pattern for chat.
Step 3: Use the Reasoning Model
For math, logic, and multi-step problems, switch to the reasoning model. It returns its thinking separately from the final answer:
response = client.chat.completions.create(
model="deepseek-reasoner",
messages=[
{"role": "user", "content": "A train travels 60 km in 45 minutes. What is its speed in km/h?"},
],
)
message = response.choices[0].message
print("Reasoning:", message.reasoning_content)
print("Answer:", message.content)
The reasoning_content field holds the model's step-by-step working, while content holds the clean final answer. Show users the answer, and keep the reasoning for debugging or display when it helps.
Step 4: Stream the Response
For chat interfaces, streaming makes the reply appear as it is generated instead of after a pause. Add stream=True and loop over the chunks:
stream = client.chat.completions.create(
model="deepseek-chat",
messages=[{"role": "user", "content": "Write a two-line poem about clean code."}],
stream=True,
)
for chunk in stream:
delta = chunk.choices[0].delta.content
if delta:
print(delta, end="", flush=True)
Step 5: Build a Reusable Function
Wrap the call so the rest of your app stays clean and errors do not crash it:
from openai import OpenAI, OpenAIError
client = OpenAI(api_key="YOUR_DEEPSEEK_KEY", base_url="https://api.deepseek.com")
def ask(prompt, model="deepseek-chat"):
try:
response = client.chat.completions.create(
model=model,
messages=[{"role": "user", "content": prompt}],
timeout=30,
)
return response.choices[0].message.content
except OpenAIError as e:
return f"API error: {e}"
print(ask("Give me three ideas for a weekend coding project."))
Keep Your API Key Out of Your Code
Hard-coding a key is the most common beginner mistake, and it is how keys leak into public repos. Load it from an environment variable instead. Set it once in your shell:
export DEEPSEEK_API_KEY="your-key-here"
Then read it at runtime, so the secret never lives in the file you commit:
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["DEEPSEEK_API_KEY"],
base_url="https://api.deepseek.com",
)
For a real app, a .env file with a loader or your platform's secrets manager works the same way. The rule stays simple: the key is configuration, not code.
A Note on Model Names in 2026
In 2026 DeepSeek introduced its V4 model line, such as deepseek-v4-flash and deepseek-v4-pro. The familiar names deepseek-chat and deepseek-reasoner still work, but they are scheduled to be deprecated on 24 July 2026, mapping to the non-thinking and thinking modes of the current flash model. For new projects, check the live model list in the docs and pin the explicit V4 name once you are ready, so an upstream change never surprises you.
Common Errors and Fixes
- 401 Unauthorized: the API key is missing, wrong, or has extra spaces. Regenerate it and load it from an environment variable, not a hard-coded string.
- Model not found: you used an old or misspelled model name. Confirm the exact name in the current docs.
- Timeouts under load: pass a
timeoutand add a simple retry, since reasoning calls can take longer than plain chat. - Surprise costs: long prompts add up. Trim context and rely on prefix caching for repeated system prompts.
Real-World Use Cases
- Cheap chat backend: power a support widget or a personal assistant where the low token price keeps a chatty app affordable at scale.
- Summarize long documents: the large context window lets you paste in reports, transcripts, or code and get a tight summary in one call.
- Code helper scripts: ask the model to explain a stack trace, draft a unit test, or convert a snippet between languages from your terminal.
- Reasoning over data: use the reasoner model to walk through pricing logic, eligibility rules, or math-heavy questions where a direct answer is risky.
- Batch content tasks: classify, tag, or rewrite thousands of short records overnight, where every fraction of a cent per call matters.
The pattern is always the same call you already wrote — only the prompt and model change. That makes it easy to start small with one script and grow into a real feature once you see what works.
Frequently Asked Questions
Is the DeepSeek API free?
It is paid but very low cost, and new accounts often start with trial credit. For light use the spend is tiny, which makes it a friendly first paid API.
Can I use the OpenAI Python library?
Yes. Set base_url to https://api.deepseek.com and pass your DeepSeek key. The rest of the OpenAI SDK works unchanged.
What is the difference between the chat and reasoner models?
The chat model answers directly and quickly. The reasoner model works through a problem step by step and exposes that reasoning separately, which helps on math and logic tasks.
Does it support large inputs?
Yes, the current models support a very large context window, so you can pass long documents. Just remember that more tokens means more cost and slightly more latency.
How do I keep my key safe?
Store it in an environment variable or a secrets manager, load it at runtime, and never commit it. Rotate the key immediately if it ever leaks.
Wrapping Up
You now have everything to use DeepSeek in Python: a basic chat call, the reasoning model, streaming, a safe reusable wrapper, and the 2026 model-name details that keep your code from breaking. It is one of the lowest-friction ways to add real AI to a project on a small budget.
Want to compare DeepSeek with other open and low-cost models before you commit? Browse free and open AI models at Free API Hub and find the right fit for your app.



