SDKs · Updated 2026-07-21

OpenAI SDK

Integrate Varsten with OpenAI-compatible chat completion traffic while keeping the provider key available for direct fallback.

Production path

The OpenAI wrapper is the recommended production path today. It keeps your call shape close to the official SDK while allowing Varsten to meter, optimize, and fall back.

Constructor

ts
import { VarstenOpenAI } from "@varsten/openai";

export const ai = new VarstenOpenAI({
  varstenApiKey: process.env.VARSTEN_API_KEY,
  openaiApiKey: process.env.OPENAI_API_KEY,
  timeouts: {
    varstenTotalMs: 15_000,
    providerTotalMs: 60_000,
  },
  onFallback: (event) => {
    logger.warn({ reason: event.reasonCode }, "Varsten fallback");
  },
});

Request labels

Add labels that help finance and engineering understand savings by workload. Do not put customer content, prompt text, or secrets in metadata.

ts
await ai.chat.completions.create(
  {
    model: "gpt-4o-mini",
    messages,
  },
  {
    varsten: {
      team: "support",
      feature: "ticket_draft",
      environment: "production",
    },
  },
);

Streaming

Streaming can fall back before output starts. Once tokens are flowing, a mid-stream provider or network error is surfaced to the caller instead of silently restarting the request and risking duplicate output.

Runtime and package format

The package requires Node.js 18 or newer and is distributed as ESM. The openai package is a peer dependency.

Supported surface

Version 0.1.0 wraps OpenAI chat completions. Validate request features such as tools and streaming in staging before moving a production route. Other OpenAI resources are not exposed by this wrapper yet.