# LangChain (https://inference.net/docs/integrations/langchain)

Connect LangChain to Inference.net using `ChatOpenAI` in Python (`langchain-openai`) or TypeScript (`@langchain/openai`) by pointing the base URL at `https://api.inference.net/v1` and authenticating with your `INFERENCE_API_KEY`. No downstream provider key is required for models hosted on Inference.net.

## Quick start

Create an account at [/register](/register/), generate a project key in [API keys](/dashboard/api-keys/), and export it:

```bash
export INFERENCE_API_KEY="inf_sk_..."
```

Install the LangChain OpenAI package and run your first completion:

<CodeTabs>

```typescript
// npm install @langchain/openai @langchain/core
import { ChatOpenAI } from "@langchain/openai";

const model = new ChatOpenAI({
  model: "deepseek-v4.1-flash-1017",
  apiKey: process.env.INFERENCE_API_KEY,
  configuration: {
    baseURL: "https://api.inference.net/v1",
  },
});

const response = await model.invoke(
  "Explain KV cache reuse in two sentences.",
);

console.log(response.content);
```

```python
# pip install langchain-openai
import os
from langchain_openai import ChatOpenAI

model = ChatOpenAI(
    model="deepseek-v4.1-flash-1017",
    base_url="https://api.inference.net/v1",
    api_key=os.environ["INFERENCE_API_KEY"],
)

response = model.invoke("Explain KV cache reuse in two sentences.")
print(response.content)
```

</CodeTabs>

## Stream responses

Both Python and TypeScript `ChatOpenAI` clients stream tokens directly from `https://api.inference.net/v1`:

<CodeTabs>

```typescript
import { ChatOpenAI } from "@langchain/openai";

const model = new ChatOpenAI({
  model: "glm-5.3",
  apiKey: process.env.INFERENCE_API_KEY,
  configuration: {
    baseURL: "https://api.inference.net/v1",
  },
});

const stream = await model.stream("List three steps to optimize LLM latency.");
for await (const chunk of stream) {
  process.stdout.write(String(chunk.content));
}
```

```python
import os
from langchain_openai import ChatOpenAI

model = ChatOpenAI(
    model="glm-5.3",
    base_url="https://api.inference.net/v1",
    api_key=os.environ["INFERENCE_API_KEY"],
)

for chunk in model.stream("List three steps to optimize LLM latency."):
    print(chunk.content, end="", flush=True)
```

</CodeTabs>

## Tool calling and structured output

Use any model with `supports_tools: true` in `GET https://api.inference.net/v1/models` (such as `deepseek-v4.1-flash-1017`, `glm-5.3`, or `moonshotai/kimi-k3.1-pro`) with `bindTools` / `bind_tools` or `withStructuredOutput` / `with_structured_output`.

<CodeTabs>

```typescript
import { ChatOpenAI } from "@langchain/openai";
import { z } from "zod";

const model = new ChatOpenAI({
  model: "deepseek-v4.1-flash-1017",
  apiKey: process.env.INFERENCE_API_KEY,
  configuration: {
    baseURL: "https://api.inference.net/v1",
  },
});

const TicketSummary = z.object({
  category: z.enum(["billing", "technical", "account"]),
  summary: z.string(),
});

const structured = model.withStructuredOutput(TicketSummary);
const result = await structured.invoke(
  "Customer reports 401 errors after rotating their API key.",
);

console.log(result);
```

```python
import os
from langchain_openai import ChatOpenAI
from pydantic import BaseModel, Field

class TicketSummary(BaseModel):
    category: str = Field(description="One of billing, technical, or account")
    summary: str

model = ChatOpenAI(
    model="deepseek-v4.1-flash-1017",
    base_url="https://api.inference.net/v1",
    api_key=os.environ["INFERENCE_API_KEY"],
)

structured = model.with_structured_output(TicketSummary)
result = structured.invoke(
    "Customer reports 401 errors after rotating their API key."
)
print(result)
```

</CodeTabs>

See [Function calling](/docs/api/function-calling/) and [Structured outputs](/docs/api/structured-outputs/) for full parameter support.

## Tag requests with task and environment headers

Attach `x-inference-task-id` and `x-inference-environment` at client creation time or per call to filter metrics in [Usage](/dashboard/usage/). Note that LangChain's per-call header option differs between TypeScript and Python:

<CodeTabs>

```typescript
import { ChatOpenAI } from "@langchain/openai";

const model = new ChatOpenAI({
  model: "deepseek-v4.1-flash-1017",
  apiKey: process.env.INFERENCE_API_KEY,
  configuration: {
    baseURL: "https://api.inference.net/v1",
    defaultHeaders: {
      "x-inference-environment": "production",
    },
  },
});

const response = await model.invoke("Summarize this release note.", {
  options: {
    headers: { "x-inference-task-id": "release-summary" },
  },
});
```

```python
import os
from langchain_openai import ChatOpenAI

model = ChatOpenAI(
    model="deepseek-v4.1-flash-1017",
    base_url="https://api.inference.net/v1",
    api_key=os.environ["INFERENCE_API_KEY"],
    default_headers={
        "x-inference-environment": "production",
    },
)

response = model.invoke(
    "Summarize this release note.",
    extra_headers={"x-inference-task-id": "release-summary"},
)
```

</CodeTabs>

## Verified model IDs

| Model ID | Context window | Tool calling | Model page |
| --- | --- | --- | --- |
| `deepseek-v4.1-flash-1017` | 1,040,000 | Yes | [DeepSeek V4.1 Flash](/models/deepseek-v4-1-flash-1017/) |
| `glm-5.3` | 1,048,576 | Yes | [GLM 5.3](/models/glm-5-3/) |
| `glm-5.3-flash` | 1,048,576 | Yes | [GLM 5.3 Flash](/models/glm-5-3-flash/) |
| `moonshotai/kimi-k3.1-pro` | 1,048,576 | Yes | [Kimi K3.1 Pro](/models/kimi-k3-1-pro/) |
| `inference-net/schematron-v2-turbo` | 128,000 | No | [Schematron V2 Turbo](/models/schematron-v2-turbo/) |

## Optional: US-only routing and BYOK passthrough

- **US-only endpoint:** Replace `https://api.inference.net/v1` with `https://us.api.inference.net/v1` for models displaying the `US` badge in the [model catalog](/models/).
- **Bring your own provider key:** To proxy requests to OpenAI or another provider through the Inference.net Gateway with a credit-billing API key, pass `x-inference-provider` and `x-inference-provider-api-key` in `default_headers` (Python) or `configuration.defaultHeaders` (TypeScript). See [API quickstart](/docs/api/quickstart/#2-proxy-through-the-inferencenet-gateway).

## Troubleshooting

- **TypeScript `baseURL` ignored:** In `@langchain/openai`, pass `baseURL` inside `configuration: { baseURL: "https://api.inference.net/v1" }` rather than as a top-level option.
- **`401 Unauthorized`:** Check that `apiKey` / `api_key` uses `INFERENCE_API_KEY`, not `OPENAI_API_KEY`.
- **`404` or HTML response:** Verify the base URL is `https://api.inference.net/v1` (with `/v1`), not `https://inference.net/v1`.
- **Model not found:** Copy the exact `id` from `GET https://api.inference.net/v1/models`, including owner prefixes such as `moonshotai/kimi-k3.1-pro`.

Explore all supported editors, gateways, and SDKs in the [Integrations hub](/docs/integrations/).