DeepSeek API in Python: a 5-minute quickstart
DeepSeek's models speak the OpenAI API format, so from Python you need exactly one library and one changed line: the base URL. This guide goes from zero to a working chat call, then covers streaming, switching models and what it costs.
Step 1 — Install the SDK
pip install openai
Yes, the OpenAI library. AkiliNode is an OpenAI-compatible gateway for DeepSeek, GLM and Qwen, so the standard SDK works unchanged — the trick is only where you point it.
Step 2 — Make your first call
import os
from openai import OpenAI
client = OpenAI(
base_url="https://api.akilinode.com/v1", # ← the only change
api_key=os.environ["AKILINODE_API_KEY"], # your AkiliNode token
)
resp = client.chat.completions.create(
model="deepseek-flash",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Write a haiku about Nairobi rain."},
],
)
print(resp.choices[0].message.content)
Get a token from the console after signing up — new accounts get USD 0.10 of test credit, enough for hundreds of test calls on deepseek-flash. Keep the key in an environment variable, never in your code.
Step 3 — Stream the answer
For chat UIs, stream tokens as they arrive:
stream = client.chat.completions.create(
model="deepseek-flash",
messages=[{"role": "user", "content": "Explain recursion in two sentences."}],
stream=True,
)
for chunk in stream:
print(chunk.choices[0].delta.content or "", end="")
Step 4 — Switch models by name
The same key and endpoint serve 25 models. Change only the model field:
| Model | Use it for | Price (in / out per 1M) |
|---|---|---|
deepseek-flash | Everyday chat, bots, extraction | USD 0.33 / 1.32 |
deepseek-v4-pro | Hard reasoning, code, agents | USD 1.45 / 4.36 |
glm-4.7-flash | Prototypes and student projects | Free |
qwen3.7-max | Top-end Qwen flagship | USD 2.30 / 6.90 |
Full catalog: DeepSeek models & pricing and the complete price list.
Step 5 — Handle errors like production code
from openai import OpenAI, APIError, RateLimitError
try:
resp = client.chat.completions.create(...)
except RateLimitError:
# back off and retry — don't hammer the API
...
except APIError as e:
print("API error:", e.status_code, e.message)
- 401 — your token is wrong or missing the
sk-prefix when you copy it. - 402 / insufficient balance — top up from USD 5; in Kenya you can pay with M-Pesa (see below).
- 429 — slow down; add exponential backoff.
What it costs in practice
A typical chatbot reply on deepseek-flash (≈400 tokens in, ≈150 out) costs about USD 0.00033 — so a USD 5 top-up covers roughly 15,000 replies. Run your own numbers in the API cost calculator.