AkiliNode
AkiliNode
Guide · Python · DeepSeek

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.

Get an API key — free test credit More code examples

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:

ModelUse it forPrice (in / out per 1M)
deepseek-flashEveryday chat, bots, extractionUSD 0.33 / 1.32
deepseek-v4-proHard reasoning, code, agentsUSD 1.45 / 4.36
glm-4.7-flashPrototypes and student projectsFree
qwen3.7-maxTop-end Qwen flagshipUSD 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)

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.

Paying from Kenya? Top up with M-Pesa — no dollar card needed: DeepSeek API in Kenya — Pay with M-Pesa. Card declined elsewhere? Read what to do when your card is declined for AI APIs.
Start building — free test credit Full API docs
© 2026 AkiliNode · Frontier AI APIs, priced for Africa · AkiliNode is an independent service and is not affiliated with DeepSeek or OpenAI.