DeepSeek Python integration and API migration

Use the OpenAI Python SDK to call a currently listed DeepSeek model through TokModel. This guide covers the differences that matter when moving an existing direct-provider integration: credentials, endpoint, model IDs, streaming, errors and a controlled rollout.

Read the DeepSeek price comparison and monitoring definitions and current model prices before selecting a model. TokModel keys and balances are separate from those of your original provider.

1. Install the SDK and store credentials on the server

shell
python -m pip install --upgrade openai
# Set TOKMODEL_KEY in your server's environment or secret manager.
# Set TOKMODEL_MODEL to an exact available ID from /pricing or /v1/models.

Do not place API keys in browser JavaScript or commit them to a repository. A key issued by your original provider does not authenticate with TokModel.

2. Change the endpoint, key and model ID explicitly

The endpoint is https://api.tokmodel.com/v1. Keep the /v1 suffix once; the SDK adds /chat/completions. Copy an exact TokModel model ID instead of assuming that a provider alias maps to the same model here.

python
import os
from openai import OpenAI

client = OpenAI(
    base_url="https://api.tokmodel.com/v1",
    api_key=os.environ["TOKMODEL_KEY"],
    timeout=60.0,
    max_retries=0,
)
model = os.environ["TOKMODEL_MODEL"]
response = client.chat.completions.create(
    model=model,
    messages=[{"role": "user", "content": "Reply with a short greeting."}],
    max_tokens=64,
)
print(response.choices[0].message.content)
print(response.usage)

Chat Completions uses the OpenAI-compatible request structure. Compatibility does not guarantee that every model supports every option. Test tools, structured output, vision, context limits and provider-specific fields for your chosen model before moving production traffic.

3. Handle streaming chunks safely

python
stream = client.chat.completions.create(
    model=model,
    messages=[{"role": "user", "content": "Explain caching in one sentence."}],
    stream=True,
    max_tokens=128,
)
for chunk in stream:
    if not chunk.choices:
        continue
    content = chunk.choices[0].delta.content
    if content:
        print(content, end="", flush=True)
print()

A usage-only chunk can have no choices; guard before accessing index zero. A transport interruption after text has been generated does not establish that the request was free. Generated output is billed according to the documented error and billing rules.

4. Diagnose errors before retrying

401: check the TokModel key and Bearer authentication. 402: check your account balance. 403: check model permissions. 404: verify the exact model ID. 429: reduce concurrency and use bounded backoff. For 500, 502 or 503, record the HTTP status and error code, then retry only according to your application's policy. See the full error table.

The example disables automatic retries to make the first integration test observable. For production, configure bounded retries for transient errors, but do not blindly replay a stream after output has reached the user. Keep credentials and request bodies out of diagnostic logs.

5. Migrate a small sample and keep a deliberate fallback

Keep separate clients, endpoint configuration and keys for the two providers. First send a small representative sample, compare schema handling and task quality, and inspect usage in the console. Increase traffic only after those checks pass. If you implement fallback, select the alternate provider explicitly and avoid duplicate requests after a partial streamed response.

Pricing is per million tokens, with input, output and cached input shown separately on the pricing page. The DeepSeek comparison uses TokModel's pricing API and upstream probe snapshots; it is not an independent benchmark or an official provider availability report.

Last substantive update: 2026-11-11.