---
title: "Call a model with the OpenAI SDK"
description: "Call a TIR Token Factory model from Python or Node with the OpenAI SDK, using your API key, the Token Factory base URL, and the model ID."
content_type: "how-to"
product: "token-factory"
platform: "TIR"
url: "/tir/token-factory/how-to/call-a-model-with-the-openai-sdk/"
topic: ["call-a-model"]
---

# Call a model with the OpenAI SDK

<!-- cspell:ignore gemma -->

Token Factory is OpenAI-compatible, so code written for the OpenAI SDK works with three changes: the
base URL, the API key, and the model ID. Every model shares the base URL
`https://blaze.e2enetworks.net/v1`, and the model ID in each request decides which model answers.
For tools, JSON output, images, and reasoning with the same client, see [Use tools, JSON output,
vision, and reasoning](/tir/token-factory/how-to/use-tools-json-vision-and-reasoning/).

You need a Token Factory API key from your project; to create one, see [Create an API
key](/tir/token-factory/how-to/create-an-api-key/). The examples call Gemma 4 31B, whose model ID is
`google/gemma-4-31b-it`. To call another model, copy its ID from Token Factory's model list, as
[Choose a model](/tir/token-factory/how-to/choose-a-model/) describes.

## Keep the key out of your code

Store the key in an environment variable on the machine that runs your code, rather than in the code
itself, and never in a web page or app that runs on your users' devices:

```bash
export TOKEN_FACTORY_API_KEY="<your-api-key>"
```

`<your-api-key>` is the key you copied when you created it.

## Call a model from Python

1. Install the SDK:

   ```bash
   pip install -U openai
   ```

2. Save this in a file named hello.py. It creates a client that points at Token Factory and sends
   one request:

   ```python
   import os
   from openai import OpenAI

   client = OpenAI(
       api_key=os.environ["TOKEN_FACTORY_API_KEY"],
       base_url="https://blaze.e2enetworks.net/v1",
   )

   response = client.chat.completions.create(
       model="google/gemma-4-31b-it",
       messages=[{"role": "user", "content": "Implement hello world in Python"}],
   )
   print(response.choices[0].message.content)
   print(response.usage)
   ```

3. Run it:

   ```bash
   python hello.py
   ```

The script prints the reply, then the tokens the request used: prompt tokens, completion tokens, and
how many of the prompt tokens came from the prompt cache, a detail some models leave empty. These are
the tokens your project is billed for; see [Tokens and
billing](/tir/token-factory/concepts/tokens-and-billing/).

## Call a model from Node

1. Install the SDK:

   ```bash
   npm install openai
   ```

2. Save this in a file named hello.mjs. The .mjs ending lets Node run the import and await lines as
   written.

   ```javascript
   import OpenAI from "openai";

   const client = new OpenAI({
     apiKey: process.env.TOKEN_FACTORY_API_KEY,
     baseURL: "https://blaze.e2enetworks.net/v1",
   });

   const response = await client.chat.completions.create({
     model: "google/gemma-4-31b-it",
     messages: [{ role: "user", content: "Implement hello world in Python" }],
   });
   console.log(response.choices[0].message.content);
   ```

3. Run it:

   ```bash
   node hello.mjs
   ```

## Stream the reply

To show the reply as the model writes it, turn streaming on. Stream long replies: a request that is
not streamed fails if the whole reply takes longer than four minutes.

The stream ends with one extra chunk that carries only the token usage and has no choices, so skip
chunks without choices. A stream that stops before a chunk with a finish reason arrives has failed
partway, so keep the last finish reason you see, and send the request again if there is none:

```python
stream = client.chat.completions.create(
    model="google/gemma-4-31b-it",
    messages=[{"role": "user", "content": "Count from 1 to 10."}],
    stream=True,
)

finish = None
for chunk in stream:
    if not chunk.choices:
        print("\n", chunk.usage)
        continue
    print(chunk.choices[0].delta.content or "", end="", flush=True)
    finish = chunk.choices[0].finish_reason or finish

if finish is None:
    print("The stream stopped partway. Send the request again.")
```

In Node, the same loop reads the stream with `for await`:

```javascript
const stream = await client.chat.completions.create({
  model: "google/gemma-4-31b-it",
  messages: [{ role: "user", content: "Count from 1 to 10." }],
  stream: true,
});

let finish = null;
for await (const chunk of stream) {
  if (!chunk.choices?.length) continue;
  process.stdout.write(chunk.choices[0].delta?.content ?? "");
  finish = chunk.choices[0].finish_reason ?? finish;
}

if (!finish) console.log("The stream stopped partway. Send the request again.");
```

If a request fails, [Troubleshooting TIR Token
Factory](/tir/token-factory/troubleshooting/token-factory/) lists each error and what to do about
it.
