aiml docs
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 openai

Stream 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.

cookbook/ts/streaming.ts#L3-L29
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.id is the aiml request id. Keep it: it looks the request up later with GET /v1/generation/{id} and it is what support asks for.
  • With stream_options.include_usage, the last chunk carries usage plus an aiml object with cost.total_micro_usd and 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 from GET /v1/models works.

Run it

AIML_API_KEY=aiml-live-… node --experimental-strip-types streaming.ts
AIML_API_KEY=aiml-live-… python streaming.py

Against a local cell (make deploy-local-up), also set AIML_BASE_URL=http://127.0.0.1:8080.

Next

On this page