Quick start
OpenAI SDK
Point the OpenAI SDK at the gateway and stream a reply, with usage and cost on the last chunk.
You need a key with the inference scope. Export it as AIML_API_KEY.
Install
npm install openaiStream a completion
Only two things change compared to calling OpenAI directly: the base URL gains /v1 under the gateway host, and the model is named vendor/model.
import OpenAI from "openai";
const client = new OpenAI({
baseURL: (process.env.AIML_BASE_URL ?? "https://api.ai.ml") + "/v1",
apiKey: process.env.AIML_API_KEY,
});
const stream = await client.chat.completions.create({
model: process.env.AIML_MODEL ?? "openai/gpt-5-mini",
messages: [{ role: "user", content: "Write one sentence about gateways." }],
stream: true,
stream_options: { include_usage: true },
});
let requestId = "";
for await (const chunk of stream) {
requestId ||= chunk.id; // the aiml request id is the chunk id
const delta = chunk.choices[0]?.delta?.content;
if (delta) process.stdout.write(delta);
// the final chunk carries usage and the aiml extension (cost, resolved endpoint)
const aiml = (chunk as { aiml?: { cost?: { total_micro_usd: number }; resolved?: { provider: string } } }).aiml;
if (chunk.usage) {
console.log(`\ntokens=${chunk.usage.completion_tokens} cost_micro=${aiml?.cost?.total_micro_usd ?? "?"} provider=${aiml?.resolved?.provider ?? "?"}`);
}
}
console.log(`request_id=${requestId}`);
console.log("[done]");What to notice:
chunk.idis the aiml request id. Keep it: it looks the request up later withGET /v1/generation/{id}and it is what support asks for.- With
stream_options.include_usage, the last chunk carriesusageplus anaimlobject withcost.total_micro_usdand the resolved endpoint. The cost is the settled ledger amount in micro-USD, not an estimate. - The default model in the examples is
openai/gpt-5-mini; any id fromGET /v1/modelsworks.
Run it
AIML_API_KEY=aiml-live-… node --experimental-strip-types streaming.ts
AIML_API_KEY=aiml-live-… python streaming.pyAgainst a local cell (make deploy-local-up), also set AIML_BASE_URL=http://127.0.0.1:8080.
Next
- Tool calling and structured output use the same client.
- Errors lists what a failed call returns and what to retry.