OpenAI Python SDK
Use the official OpenAI Python client with AnonRouter for chat completions and streaming through compatibility mode.
Point the official OpenAI Python client at AnonRouter to use the familiar Chat Completions interface. You need an inference key with OpenAI-compatible access enabled and a model ID from the catalog.
For AnonRouter's own Python package, including gateway verification and encrypted inference, use the Python SDK guide instead.
Install
python -m pip install openaiSet ANONROUTER_API_KEY through your server environment or secret manager.
Set ANONROUTER_MODEL to the exact model ID you selected. Never commit the key,
include it in browser code, or print it in logs.
Send a chat completion
import os
from openai import OpenAI
with OpenAI(
api_key=os.environ["ANONROUTER_API_KEY"],
base_url="https://api.anonrouter.ai/v1",
max_retries=0,
) as client:
response = client.chat.completions.create(
model=os.environ["ANONROUTER_MODEL"],
messages=[{"role": "user", "content": "Say hello in one sentence."}],
max_completion_tokens=256,
)
print(response.choices[0].message.content or "")The custom base_url directs the call to AnonRouter. Use chat.completions.create
for this integration. AnonRouter implements a defined
compatibility surface, not every endpoint exposed by the
Python client. See the official Python client reference
for client configuration.
Stream the answer
import os
from openai import OpenAI
with OpenAI(
api_key=os.environ["ANONROUTER_API_KEY"],
base_url="https://api.anonrouter.ai/v1",
max_retries=0,
) as client:
with client.chat.completions.create(
model=os.environ["ANONROUTER_MODEL"],
messages=[{"role": "user", "content": "Describe the ocean in two sentences."}],
max_completion_tokens=256,
stream=True,
) as stream:
for chunk in stream:
if chunk.choices:
text = chunk.choices[0].delta.content
if text:
print(text, end="", flush=True)
print()Streaming chunks carry delta updates instead of a complete message. The
empty-choices guard also accommodates usage-only chunks. See the
Chat Completions reference.
These examples print responses for a local demonstration. In a service, return content to the user without adding it to application logs. Running either example sends an inference request using your account's available funding.
Choose the privacy boundary
Compatibility mode sends your stable key and prompt to the same confidential broker, which performs the ticket exchange internally. It is convenient for existing clients but does not keep credential identity separate from content.
Use the private ticket quickstart when your application can handle the two-step exchange. Use AnonRouter's Python SDK for independent verification and supported E2EE routes. Configuring the official OpenAI client alone does not verify hardware attestation or encrypt content to a provider key.
Troubleshooting
- 401 or 403: check that the inference key has OpenAI-compatible access enabled.
- 404: the base URL must be
https://api.anonrouter.ai/v1; the client adds the endpoint path. - Model not found: select an exact current ID from the catalog.
- Context or output limit error: reduce the input or requested output, or select a route with a larger limit.
- Interrupted stream: handle the exception and any partial answer explicitly. An interruption can still consume usage.