LangChain
Keep your LangChain chat model, its bound tools and its structured output, and let it answer on the visitor's own AI account by passing the AiConnect client's options to ChatOpenAI.
Your LangChain code keeps ChatOpenAI, bindTools, stream, invoke and withStructuredOutput. The chat model 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.
This page covers ChatOpenAI, the chat model tested with the wallet. It speaks the OpenAI protocol to the wallet whichever account answers, so a Claude plan answers it as well as a ChatGPT plan.
Install
npm install @langchain/core @langchain/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 model = new ChatOpenAI({ model: 'gpt-6-luna', apiKey: process.env.OPENAI_API_KEY });
// After
const model = new ChatOpenAI({
model: 'gpt-6-luna',
apiKey: aiconnect.sdkOptions.apiKey,
configuration: aiconnect.sdkOptions,
});aiconnect is your AiConnect client. configuration goes to the OpenAI SDK client that ChatOpenAI builds, which then sends its requests through aiconnect.fetch. The model requires a key, so the placeholder aiwallet fills it, and the wallet never reads it.
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 status 401.
Streaming with bound 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 { HumanMessage, SystemMessage, ToolMessage } from '@langchain/core/messages';
import type { AIMessageChunk, BaseMessage } from '@langchain/core/messages';
import { ChatOpenAI } from '@langchain/openai';
const { sdkOptions } = aiconnect;
const model = new ChatOpenAI({ apiKey: sdkOptions.apiKey, model: 'gpt-6-luna', configuration: sdkOptions })
.bindTools(tools);
const messages: BaseMessage[] = [new SystemMessage(system), new HumanMessage(question)];
for (let step = 0; step < 4; step += 1) {
let answer: AIMessageChunk | undefined;
for await (const chunk of await model.stream(messages, { signal })) {
if (typeof chunk.content === 'string' && chunk.content) {
showText(chunk.content);
}
answer = answer ? answer.concat(chunk) : chunk;
}
if (!answer) {
break;
}
messages.push(answer);
if (!answer.tool_calls?.length) {
break;
}
for (const call of answer.tool_calls) {
const id = call.id ?? call.name;
messages.push(new ToolMessage({ tool_call_id: id, content: await runTool(call.name, call.args) }));
}
}bindTools takes your tools in the OpenAI format, { type: 'function', function: { name, description, parameters } }, or LangChain tools. When they live on your MCP server, the MCP guide shows how to read them in the page. model.invoke(messages) answers whole.
Structured output
withStructuredOutput sends a JSON schema in the request's response format, which the wallet passes to the account in its own provider's format:
const picker = new ChatOpenAI({ apiKey: sdkOptions.apiKey, model: 'gpt-6-luna', configuration: sdkOptions })
.withStructuredOutput(pickSchema, { name: 'pick' });
const pick = await picker.invoke(question);Keep a model name that does not start with gpt-3 or gpt-4: for those, LangChain falls back to a forced tool call, which some Claude models refuse.
The model that answers
The model you pass is ignored, since the visitor chooses the model in the wallet. answer.response_metadata.model_name names the model that answered. The model names section tells which event carries which name.
Errors and retries
A failure before any text throws the OpenAI SDK's error, with its status: 401 when the site is not connected, 403 when the visitor declined or left the wallet locked, 429 when the account's plan is spent. The errors section lists every status.
ChatOpenAI retries a 429 and the 5xx statuses six times by default. On Auto the wallet already moves to the visitor's next account, so you can answer sooner:
const model = new ChatOpenAI({
model: 'gpt-6-luna', apiKey: sdkOptions.apiKey, configuration: sdkOptions, maxRetries: 0,
});The signal of a call stops the answer in the wallet.
What a request can carry
Images as data: URLs reach every account. Files, audio and OpenAI's own server tools are refused with a 400. The AI SDK guide lists what the wallet passes on to each account.
Vercel AI SDK
Keep your streamText and generateText code, its tool loop and its structured output, and let it answer on the visitor's own AI account through an OpenAI, Anthropic or Google provider built with the AiConnect client's fetch.
LlamaIndex
Keep your LlamaIndex OpenAI LLM and its tool calls, and let it answer on the visitor's own AI account by passing the AiConnect client's options to the OpenAI SDK client it builds.