OpenAI SDK
Keep your OpenAI SDK code, chat completions or the Responses API, and let it answer on the visitor's own AI account by passing the AiConnect client's options to the constructor.
Your openai client keeps its methods, its streaming events and its error classes. It takes the options of your AiConnect client, and every call then answers through the visitor's wallet, on the account the visitor picked for your site. That account can be a ChatGPT plan, a Claude plan, a Gemini key or any other account the wallet holds. The AI SDK guide explains how this works for every SDK.
Install
npm install openai @tensorpass/aiconnectUntil @tensorpass/aiconnect is published on npm, the JavaScript quickstart shows how to point your project at a checkout of the repository.
The change
// Before
const client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
// After
const client = new OpenAI(aiconnect.sdkOptions);aiconnect is your AiConnect client, and sdkOptions is { apiKey: 'aiwallet', fetch: aiconnect.fetch, dangerouslyAllowBrowser: true }. The SDK requires a key, so the placeholder aiwallet fills it, and the wallet never reads it. The OpenAI SDK runs in a browser only with dangerouslyAllowBrowser, a guard meant for real keys, and no key reaches the page here.
The call runs in the page, where the wallet answers. Show your connect entry point first, as the React quickstart does: a request from a site the visitor has not connected fails with OpenAI.AuthenticationError.
Chat completions with tools
The bookshop demo in apps/demo of the AI Connect repository runs this loop in its SDK mode. search_books stands for your own tools, runTool runs one and returns its result as text, and showText writes the answer as it streams.
import OpenAI from 'openai';
const client = new OpenAI(aiconnect.sdkOptions);
const messages: OpenAI.ChatCompletionMessageParam[] = [
{ role: 'system', content: system },
{ role: 'user', content: question },
];
for (let step = 0; step < 4; step += 1) {
const stream = client.chat.completions.stream({ model: 'gpt-6-luna', messages, tools }, { signal });
stream.on('content', (delta) => showText(delta));
const { message } = (await stream.finalChatCompletion()).choices[0];
messages.push(message);
const calls = (message.tool_calls ?? []).filter((call) => call.type === 'function');
if (!calls.length) {
break;
}
for (const call of calls) {
const content = await runTool(call.function.name, JSON.parse(call.function.arguments || '{}'));
messages.push({ role: 'tool', tool_call_id: call.id, content });
}
}tools holds your tools in the OpenAI format, { type: 'function', function: { name, description, parameters } }. When they live on your MCP server, the MCP guide shows how to read them in the page. tool_choice, temperature, max_tokens and stream_options: { include_usage: true } work as they do with OpenAI.
The Responses API with structured output
client.responses answers too, streamed or whole, and a JSON schema in text.format reaches the account in its own provider's format.
const stream = client.responses.stream({
model: 'gpt-6-luna',
instructions: 'Answer with the book that fits best.',
input: question,
text: { format: { type: 'json_schema', name: 'pick', schema: pickSchema, strict: true } },
});
stream.on('response.output_text.delta', (event) => showText(event.delta));
const pick = JSON.parse((await stream.finalResponse()).output_text);The wallet keeps no conversation between requests, so previous_response_id is refused with a 400: send the whole input each time.
The model that answers
The model you pass is ignored, since the visitor chooses the model in the wallet. The first chunk of a chat completions stream, and response.created on the Responses API, carry the model of the account that answers. The model names section tells which event carries which name.
Errors and retries
A failure before any text throws the SDK's own error class for the wallet's status: OpenAI.AuthenticationError when the site is not connected, OpenAI.PermissionDeniedError when the visitor declined or left the wallet locked, OpenAI.RateLimitError when the account's plan is spent, and OpenAI.NotFoundError for embeddings, images, audio and model lists, which the wallet does not answer. The errors section lists every status.
The SDK retries a 429 and the 5xx statuses twice. On Auto the wallet already moves to the visitor's next account, so you can answer sooner:
const client = new OpenAI({ ...aiconnect.sdkOptions, maxRetries: 0 });Aborting the signal, or breaking out of the stream, stops the answer in the wallet.
What a request can carry
Images as data: URLs reach every account. Files, audio, n above 1 and OpenAI's own server tools, such as its web search, are refused. The AI SDK guide lists what the wallet passes on to each account.
Use the AI SDK you already have
Keep your OpenAI, Anthropic, Google Gen AI, Vercel AI SDK, LangChain or LlamaIndex code and let it answer on the visitor's own AI account by passing one option to the SDK, and serve a Mastra agent's tools to the page over MCP.
Anthropic SDK
Keep your Anthropic SDK code, Messages streamed or whole, and let it answer on the visitor's own AI account by passing the AiConnect client's options to the constructor.