TUTORIAL / UPDATED NOVEMBER 24, 2026

Connect an OpenAI-compatible Python client, then diagnose the first response.

Start with a single text prompt and a small output budget. This walkthrough uses the implemented non-streaming BestLLM endpoint; it does not claim full OpenAI API parity or a measured live provider result.

1. Check the prerequisites

Follow the DeepSeek Chat setup guide first: verified email, US-scoped workspace key, approved model access, available wallet funds, and an enabled gateway. Confirm the effective workspace price before sending a billable request.

This tutorial publishes no price quote or benchmark. Its source is the current gateway request contract, reviewed November 24, 2026.

2. Prepare a server-side client

In your own Python environment, install the official client with python -m pip install openai. Set BESTLLM_API_KEY securely in the environment. Save the following as first_chat.py and run python first_chat.py.

The base URL ends in /v1; the client adds /chat/completions. Do not append that path twice. For a configured local gateway, use http://127.0.0.1:4000/v1.

Why disable retries?

max_retries=0 lets you inspect one attempt at a time. A connection timeout does not prove the provider did no work. This guide does not assume that resending a request is idempotent.

max_tokens=128 limits requested output, not total dollar spend. Input tokens also count. Check the approved route’s output ceiling and effective price before increasing it.

3. Run a non-streaming request

import os
from openai import OpenAI, APIStatusError, APIConnectionError

client = OpenAI(
    api_key=os.environ["BESTLLM_API_KEY"],
    base_url="https://api.bestllm.dev/v1",
    max_retries=0,
    timeout=70.0,
)

try:
    response = client.chat.completions.create(
        model="deepseek-chat",
        messages=[{"role": "user", "content": "Explain an API gateway in one sentence."}],
        max_tokens=128,
        stream=False,
    )
    print(response.choices[0].message.content)
    print(response.usage)
except APIStatusError as exc:
    # Gateway errors use {"error": "code"}; inspect the HTTP body directly.
    print("HTTP", exc.status_code)
    print(exc.response.text)
except APIConnectionError:
    print("Connection or timeout error. Check usage before retrying.")

On HTTP success, read the message and usage, then verify the corresponding request in Console Usage and the charge in Billing. The example deliberately uses no tools, image inputs, structured-output mode or streaming.

The SDK can raise an HTTP exception for the gateway’s flat error body. Inspect exc.response.text rather than assuming an OpenAI-style nested error object. Redact account data before sharing logs.

4. Match the error to the next action

Errors emitted by the current gateway
HTTPError codeNext action
401missing_api_key / invalid_api_keyCheck the Bearer header and workspace key. Replace a revoked key; do not log it.
400invalid_requestCheck model, non-empty messages, positive integer token limit, and boolean stream. The response may contain validation details.
403email_verification_requiredComplete account email verification before retrying. Ask your administrator if that flow is unavailable.
403region_not_servedThe current gateway accepts US-scoped keys only. Do not relabel a workload to bypass residency requirements.
403model_route_not_approvedAsk the workspace administrator to check entitlement, route approval, retention policy and kill switch. A catalog entry is not authorization.
402wallet_authorization_requiredIn the current implementation, dispatch is disabled. Contact the operator; adding funds alone does not enable dispatch.
429key_rate_limited / too_many_requestsReduce concurrency and token limits. Honor Retry-After when present; the site-level limiter may omit it. Use bounded backoff.
501streaming_not_implementedUse stream=False. This gateway does not currently return streaming chunks.
502provider_request_failedThe upstream request failed. Check service status and usage before deciding whether to retry.
503settlement_pending / provider_reconciliation_requiredThe provider may already have processed the request. Check Usage and Billing and contact the operator before resending; do not automatically retry.
503key_service_unconfigured / dispatch_service_unconfiguredOperator configuration is missing. A client-side model or SDK change will not fix this.

A generic 500 can also indicate a server-side failure, including an unhandled wallet reservation error. Do not interpret every failure as insufficient credit; retain the status and timestamp for the operator.

5. Verify before expanding

Open Console to confirm usage and billing. Keep one request at a time until the approved route works. Then increase workload gradually within your key’s limits.

This recipe covers the Python client only. It does not establish T3 Code, MCP, Responses API, streaming, or arbitrary OpenAI client compatibility.

API documentation →