# LangChain (/docs/guides/sdks/langchain)

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](/docs/guides/existing-sdks) 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 [#install]

```bash
npm install @langchain/core @langchain/openai @tensorpass/aiconnect
```

Until `@tensorpass/aiconnect` is published on npm, the [JavaScript quickstart](/docs/quickstart/javascript#install-the-package) shows how to point your project at a checkout of the repository.

## The change [#the-change]

```ts
// 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](/docs/quickstart/react) does: a request from a site the visitor has not connected fails with status 401.

## Streaming with bound tools [#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.

```ts
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](/docs/guides/mcp-tools#with-an-ai-sdk) shows how to read them in the page. `model.invoke(messages)` answers whole.

## Structured output [#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:

```ts
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-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](/docs/guides/existing-sdks#model-names) tells which event carries which name.

## Errors and retries [#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](/docs/guides/existing-sdks#errors) 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:

```ts
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 [#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](/docs/guides/existing-sdks#what-the-wallet-takes-from-a-request) lists what the wallet passes on to each account.
