Vercel AI SDK
The AI SDK works with Cloakray through its standard providers. Pick the one whose API format you want; they all reach the same models.
OpenAI-compatible provider
The most direct option: Chat Completions for every model.
npm install ai @ai-sdk/openai-compatible
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
import { generateText, streamText } from "ai";
const cloakray = createOpenAICompatible({
name: "cloakray",
baseURL: "https://api.cloakray.com/v1",
apiKey: process.env.CLOAKRAY_API_KEY,
});
const { text } = await generateText({
model: cloakray("claude-sonnet-4-6"),
prompt: "Write a haiku about caching.",
});
const result = streamText({
model: cloakray("gpt-5.5"),
prompt: "Explain backpressure in two sentences.",
});
for await (const delta of result.textStream) process.stdout.write(delta);
Tools
import { generateText, stepCountIs, tool } from "ai";
import { z } from "zod";
const { text } = await generateText({
model: cloakray("gpt-5.5"),
tools: {
weather: tool({
description: "Current weather for a city",
inputSchema: z.object({ city: z.string() }),
execute: async ({ city }) => ({ city, temperatureC: 18 }),
}),
},
stopWhen: stepCountIs(5),
prompt: "Do I need a jacket in Berlin?",
});
generateObject and structured output work the same way, on models that support structured outputs.
Images and embeddings
import { embedMany, generateImage } from "ai";
const { image } = await generateImage({
model: cloakray.imageModel("gpt-image-2"),
prompt: "A lighthouse in a storm, oil painting",
});
const { embeddings } = await embedMany({
model: cloakray.textEmbeddingModel("text-embedding-3-small"),
values: ["first document", "second document"],
});
Older AI SDK versions export generateImage as experimental_generateImage.
OpenAI and Anthropic providers
@ai-sdk/openai uses the Responses API, and @ai-sdk/anthropic the Messages API. Both take a base URL:
import { createOpenAI } from "@ai-sdk/openai";
import { createAnthropic } from "@ai-sdk/anthropic";
const openai = createOpenAI({ baseURL: "https://api.cloakray.com/v1", apiKey: process.env.CLOAKRAY_API_KEY });
const anthropic = createAnthropic({ baseURL: "https://api.cloakray.com/v1", apiKey: process.env.CLOAKRAY_API_KEY });
await generateText({ model: anthropic("claude-opus-4-8"), prompt: "Hello" });
Use these when you want provider-specific options, such as Anthropic's thinking or cacheControl provider options.
Bare model strings (gateway protocol)
When you pass a plain string — model: "claude-sonnet-4-6" — the AI SDK resolves it through its default gateway provider, which uses its own wire protocol. Cloakray speaks that protocol too, so an app written that way only needs the provider repointed:
import { createGateway } from "@ai-sdk/gateway";
globalThis.AI_SDK_DEFAULT_PROVIDER = createGateway({
baseURL: "https://api.cloakray.com/v3/ai",
apiKey: process.env.CLOAKRAY_API_KEY,
});
| AI SDK | Base URL |
|---|---|
| 5 | https://api.cloakray.com/v1/ai |
| 6 | https://api.cloakray.com/v3/ai |
| 7 | https://api.cloakray.com/v4/ai |
Use Cloakray model IDs as they appear in the catalogue, without a provider prefix. gateway.getAvailableModels() lists the text models your key can use, and gateway.getCredits() returns your balance. Text generation, streaming, function tools and image inputs are supported on this protocol; provider-native tools are not.