# OpenAI SDK

Use the OpenAI Python and TypeScript SDKs with Boundless: install, base URL, a first request, streaming and a tool call.


The OpenAI SDKs work against Boundless with two changes: the base URL and the key. Everything else is the code you already have.

## Install

```bash
pip install openai
npm install openai
```

## Base URL and key

Set the base URL to `https://api.inference.boundless.network/v1`. Send a Boundless key where the OpenAI key goes. [Create a key](https://inference.boundless.network/settings/api-keys), then export it:

```bash
export BOUNDLESS_API_KEY="paste-your-key-here"
```

Use a model ID exactly as the [models table](https://inference.boundless.network/docs/models) lists it.

## Make a request

```python
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["BOUNDLESS_API_KEY"],
    base_url="https://api.inference.boundless.network/v1",
)

response = client.chat.completions.create(
    model="deepseek/deepseek-v4.1-flash-1017",
    messages=[{"role": "user", "content": "Say hello"}],
)

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

```typescript
import OpenAI from "openai"

const client = new OpenAI({
  apiKey: process.env.BOUNDLESS_API_KEY,
  baseURL: "https://api.inference.boundless.network/v1",
})

const response = await client.chat.completions.create({
  model: "deepseek/deepseek-v4.1-flash-1017",
  messages: [{ role: "user", content: "Say hello" }],
})

console.log(response.choices[0]?.message.content)
console.log(response.usage)
```

## Stream the reply

Set `stream` to `true`. Set `stream_options.include_usage` to `true` to get exact token counts in the last chunk. That chunk has an empty `choices` array.

```python
stream = client.chat.completions.create(
    model="deepseek/deepseek-v4.1-flash-1017",
    messages=[{"role": "user", "content": "Say hello"}],
    stream=True,
    stream_options={"include_usage": True},
)

for chunk in stream:
    if chunk.choices and chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)
    if chunk.usage:
        print(f"\n{chunk.usage}")
```

```typescript
const stream = await client.chat.completions.create({
  model: "deepseek/deepseek-v4.1-flash-1017",
  messages: [{ role: "user", content: "Say hello" }],
  stream: true,
  stream_options: { include_usage: true },
})

for await (const chunk of stream) {
  const text = chunk.choices[0]?.delta?.content
  if (text) process.stdout.write(text)
  if (chunk.usage) console.log("\n", chunk.usage)
}
```

[Streaming](https://inference.boundless.network/docs/streaming) explains the chunk format.

## Call a tool

Pass your functions in `tools`. The model answers with the call it wants. This example forces the call and prints it. [Tool calling](https://inference.boundless.network/docs/tool-calling) shows the full loop, with streamed arguments and the result sent back.

```python
response = client.chat.completions.create(
    model="deepseek/deepseek-v4.1-flash",
    messages=[{"role": "user", "content": "What is 20 + 22? Use the add tool."}],
    max_tokens=1024,
    tools=[{
        "type": "function",
        "function": {
            "name": "add",
            "description": "Add two integers.",
            "parameters": {
                "type": "object",
                "properties": {"a": {"type": "integer"}, "b": {"type": "integer"}},
                "required": ["a", "b"],
            },
        },
    }],
    tool_choice={"type": "function", "function": {"name": "add"}},
)

call = response.choices[0].message.tool_calls[0]
print(call.function.name, call.function.arguments)
```

## Known differences

- Credits that run out return `402` with `budget_exceeded`. The SDK does not retry a `402`. A rate-limit `429` is retried by default, which is what you want. See [Errors](https://inference.boundless.network/docs/errors).
- Not every model supports tools. `GET /v1/model/info` lists the capability flags.
- A reasoning model spends `max_tokens` on thinking too. See [Reasoning](https://inference.boundless.network/docs/reasoning).
- The gateway does not serve `/v1/embeddings`, `/v1/completions`, `/v1/batches`, images or audio. See [API Compatibility](https://inference.boundless.network/docs/api-compatibility).
- `POST /v1/responses` serves only the subset Codex CLI uses. Use chat completions from the SDK.
- After a `502`, a timeout or a dropped stream, check `GET /v1/requests/{id}` before you resend. See [Retrying safely](https://inference.boundless.network/docs/errors#retrying-safely).
