AIDiscount优汇智
Home / Blog

Using LangChain and the Vercel AI SDK with a Custom API Provider

How to point LangChain (Python and JS) and the Vercel AI SDK at an OpenAI-compatible endpoint by changing the base URL and key, with working examples.

2026-11-25 · 2 min read · AIDiscount team

LangChainVercel AI SDKAPINode.jsPython

LangChain and the Vercel AI SDK both talk to models through adapters that already support OpenAI-compatible APIs. Switching providers inside either framework means configuring the base URL and key on the OpenAI-compatible client, not changing how you call the framework itself.

LangChain (Python)

ChatOpenAI accepts a custom base URL through base_url (or the OPENAI_BASE_URL environment variable):

import os
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    model="gpt-6-luna",
    base_url="https://aidiscount.ai/v1",
    api_key=os.environ["AIDISCOUNT_API_KEY"],
)
response = llm.invoke("Summarize the plot of Hamlet in two sentences.")
print(response.content)

Streaming, tool binding with llm.bind_tools(...) and LangChain Expression Language chains all work unchanged, since they go through the same underlying client.

LangChain (JS/TS)

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

const llm = new ChatOpenAI({
  model: "gpt-6-luna",
  configuration: { baseURL: "https://aidiscount.ai/v1" },
  apiKey: process.env.AIDISCOUNT_API_KEY,
});
const response = await llm.invoke("Summarize the plot of Hamlet in two sentences.");
console.log(response.content);

LangChain with the Anthropic Messages format

Use ChatAnthropic with its own base URL option instead of ChatOpenAI:

from langchain_anthropic import ChatAnthropic

llm = ChatAnthropic(
    model="claude-opus-4-8",
    anthropic_api_url="https://aidiscount.ai",
    api_key=os.environ["AIDISCOUNT_API_KEY"],
)

Vercel AI SDK

The @ai-sdk/openai provider accepts a baseURL when you create it with createOpenAI, instead of using the default openai export:

import { createOpenAI } from "@ai-sdk/openai";
import { generateText } from "ai";

const aidiscount = createOpenAI({
  baseURL: "https://aidiscount.ai/v1",
  apiKey: process.env.AIDISCOUNT_API_KEY,
});

const { text } = await generateText({
  model: aidiscount("gpt-6-luna"),
  prompt: "Summarize the plot of Hamlet in two sentences.",
});
console.log(text);

Streaming works the same way through streamText:

import { streamText } from "ai";

const { textStream } = streamText({
  model: aidiscount("gpt-6-luna"),
  prompt: "Write a short poem about rivers.",
});
for await (const chunk of textStream) process.stdout.write(chunk);

For the Anthropic Messages format, use createAnthropic from @ai-sdk/anthropic with baseURL: "https://aidiscount.ai" the same way.

Using tools through these frameworks

Both frameworks convert their own tool definitions into the same JSON Schema shape the underlying API expects, so tool calling works without extra configuration once the base URL is set. LangChain uses llm.bind_tools([...]) or an agent executor; the Vercel AI SDK passes a tools object to generateText or streamText. See the function calling guide for the request and response shape underneath.

Common mistakes

  • Setting the base URL on the wrong object. In LangChain JS, the base URL is nested under configuration, not a top-level field.
  • Mixing a Chat Completions base URL with an Anthropic-format model name or vice versa. Each adapter targets one API shape; use ChatOpenAI/createOpenAI for OpenAI format and ChatAnthropic/createAnthropic for Anthropic format, matching the base URL to the right one.
  • Leaving the default openai export from the Vercel AI SDK. That export always points at OpenAI's own API; you need createOpenAI to set a different base URL.

Picking a model

Any model ID from the marketplace works as the model argument once the base URL and key are set; nothing else in your LangChain or Vercel AI SDK code needs to change when you switch models.

Try it with AIDiscount. One API key works with OpenAI- and Anthropic-compatible tools. Pay as you go, no monthly fee.
Create a free accountRead the API docs

Related guides

Languages

English · 简体中文 · 繁體中文 · 日本語 · 한국어 · Español · Français · Deutsch · Português · Русский