# Python SDK

> Use ScaleMax from Python with OpenAI-compatible clients, environment variables, streaming, and the correct base URL.

Use the official OpenAI Python SDK and set `base_url` to ScaleMax. Model IDs come from the [models and pricing](https://docs.scalemax.pro/docs/models) page, and every request field is listed in [Chat Completions](https://docs.scalemax.pro/docs/chat-completions).

Set `SCALEMAX_BASE_URL` to `https://api.scalemax.pro/v1` for API credit or `https://api.scalemax.pro/token/v1` for an API-token key. This is the OpenAI-compatible route; native Claude apps use their origin-only setup instead.

**macOS / Linux**

```bash
# API-credit key:
export SCALEMAX_BASE_URL="https://api.scalemax.pro/v1"
# API-token key:
# export SCALEMAX_BASE_URL="https://api.scalemax.pro/token/v1"
```

**Windows PowerShell**

```powershell
# API-credit key:
$env:SCALEMAX_BASE_URL = "https://api.scalemax.pro/v1"
# API-token key:
# $env:SCALEMAX_BASE_URL = "https://api.scalemax.pro/token/v1"
```

## Install

```bash
pip install openai
```

## Basic chat

client.py:

```python
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["SCALEMAX_API_KEY"],
    base_url=os.environ["SCALEMAX_BASE_URL"],
)

response = client.chat.completions.create(
    model="YOUR_AUTHENTICATED_MODEL_ID",
    messages=[{"role": "user", "content": "Write a haiku about fast inference."}],
)

print(response.choices[0].message.content)
```

## Streaming

stream.py:

```python
stream = client.chat.completions.create(
    model="YOUR_AUTHENTICATED_MODEL_ID",
    messages=[{"role": "user", "content": "Stream a short greeting."}],
    stream=True,
)

for chunk in stream:
    delta = chunk.choices[0].delta.content
    if delta:
        print(delta, end="", flush=True)
```

Server-sent-event framing and safe disconnect handling are covered in [Streaming](https://docs.scalemax.pro/docs/streaming). The same flow in other languages: [JavaScript](https://docs.scalemax.pro/docs/examples/javascript), [cURL](https://docs.scalemax.pro/docs/examples/curl).
