Guides

Examples

Common patterns, ready to copy.

Streaming with fallback

ts
const stream = await ai.chat({
  model: "anthropic/claude-opus-4",
  fallback: ["openai/gpt-5"],
  stream: true,
  messages: [{ role: "user", content: "Draft a launch post." }],
});

for await (const chunk of stream) {
  process.stdout.write(chunk.delta ?? "");
}

Structured output

ts
const res = await ai.chat({
  model: "openai/gpt-5",
  response_format: { type: "json_schema", schema: OrderSchema },
  messages: [{ role: "user", content: "Parse: 2x large pepperoni, delivery." }],
});

const order = OrderSchema.parse(JSON.parse(res.output_text));

Semantic cache

ts
await ai.chat({
  model: "openai/gpt-5-mini",
  cache: { mode: "semantic", ttl: 3600 },
  messages: [{ role: "user", content: q }],
});

Route by cost policy

ts
await ai.chat({
  route: { strategy: "cheapest", quality_floor: 0.85 },
  messages: [{ role: "user", content: q }],
});