Provider wrappers

Wrappers instrument official OpenAI, Anthropic, and Google Gemini clients so successful and failed completions are tracked automatically without manual track() calls.

noryen.wrap(client)

noryen.wrap(client) detects the provider from the object shape and delegates to the right wrapper. Supported shapes include chat.completions.create (OpenAI-style), Anthropic messages.create and Gemini models.generateContent.

openai.ts
import OpenAI from "openai";
import { noryen } from "@noryen/sdk";
noryen.init({ apiKey: process.env.NORYEN_API_KEY });
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
const client = noryen.wrap(openai);
await client.chat.completions.create({
model: "gpt-4o-mini",
messages: [{ role: "user", content: "Hello Noryen!" }],
});

If the client type is not recognized, the SDK throws: use explicit wrapOpenAI, wrapAnthropic, or wrapGemini, or fall back to track().

Explicit wrapper functions

Use noryen.wrapOpenAI, noryen.wrapAnthropic, or noryen.wrapGemini when you want an explicit provider without auto-detection (same behavior as wrap() for that type):

wrap-explicit.ts
import { noryen } from "@noryen/sdk";
import OpenAI from "openai";
noryen.init({ apiKey: process.env.NORYEN_API_KEY });
const client = noryen.wrapOpenAI(
new OpenAI({ apiKey: process.env.OPENAI_API_KEY }),
);

WrapOptions

Optional second argument:

  • metadata — merged into every wrapped event (Metadata)
  • modelOverride — force the tracked model string
  • debug — wrapper-level warnings

OpenRouter and OpenAI-compatible APIs

Any client that uses the OpenAI SDK with a custom baseURL (such as OpenRouter) is still an OpenAI client for wrapping purposes:

openrouter.ts
import OpenAI from "openai";
import { noryen } from "@noryen/sdk";
noryen.init({ apiKey: process.env.NORYEN_API_KEY });
const openrouter = new OpenAI({
baseURL: "https://openrouter.ai/api/v1",
apiKey: process.env.OPENROUTER_API_KEY,
});
const client = noryen.wrap(openrouter);

More detail: OpenRouter guide.

When to use manual track() instead

Use noryen.track() for custom stacks, gRPC, internal model gateways, or providers without a supported client shape.

After wrapped calls in short-lived environments, call flush() before returning.