DeepSeek 网摘deepseek.wang

Tutorials · 2026-10-16

DeepSeek API Python Quickstart: A Working Chat in Ten Minutes

This tutorial gets a DeepSeek API call running in about ten minutes: key setup, SDK install, a chat message, then streaming. The API is OpenAI-format compatible, with an Anthropic-format endpoint as well, so migrating from other models is cheap.

Setup

export DEEPSEEK_API_KEY="sk-..."

Install the SDK and send a message

pip install openai

import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["DEEPSEEK_API_KEY"],
    base_url="https://api.deepseek.com",
)

resp = client.chat.completions.create(
    model="deepseek-flash",   # DeepSeek-V4.1-Flash
    messages=[
        {"role": "system", "content": "You are a concise assistant."},
        {"role": "user", "content": "Explain off-peak pricing in one sentence."},
    ],
)
print(resp.choices[0].message.content)

Switch to deepseek-v4-pro for harder reasoning; for image input see the vision API tutorial.

Streaming

stream = client.chat.completions.create(
    model="deepseek-flash",
    messages=[...],
    stream=True,
)
for chunk in stream:
    delta = chunk.choices[0].delta.content
    if delta:
        print(delta, end="", flush=True)

Anthropic-format endpoint

Teams used to the Anthropic SDK can point base_url at https://api.deepseek.com/anthropic — same model names, a two-line migration.

Three common pitfalls

For the full pricing table and time windows see the peak/off-peak explainer.