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.
You need a Token Factory API key from your project; to create one, see 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 describes.
Keep the key out of your code
Section titled “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:
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
Section titled “Call a model from Python”-
Install the SDK:
pip install -U openai -
Save this in a file named hello.py. It creates a client that points at Token Factory and sends one request:
import osfrom openai import OpenAIclient = 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) -
Run it:
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.
Call a model from Node
Section titled “Call a model from Node”-
Install the SDK:
npm install openai -
Save this in a file named hello.mjs. The .mjs ending lets Node run the import and await lines as written.
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); -
Run it:
node hello.mjs
Stream the reply
Section titled “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:
stream = client.chat.completions.create( model="google/gemma-4-31b-it", messages=[{"role": "user", "content": "Count from 1 to 10."}], stream=True,)
finish = Nonefor 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:
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 lists each error and what to do about it.
Was this page helpful?