EN ▾

openaiapiproxy.comLangChain OpenAI Base URL: Use a Custom LLM API

LangChain OpenAI Base URL: Use a Custom LLM API

The langchain openai base url is a constructor argument on ChatOpenAI. Set it to https://api.openaiapiproxy.com/v1, pass your key and use the model name uncensored, and every chain and agent that takes a chat model will run on it.

Updated

A chain of glowing links ending in one bright orb, representing a LangChain pipeline connected to a custom model

Key points

  • In Python use ChatOpenAI(base_url=..., api_key=..., model="uncensored"). In JavaScript use configuration.baseURL.
  • Prompts, chains, streaming, tool binding and JSON mode keep working through the standard interfaces.
  • Embeddings are not offered, so OpenAIEmbeddings must point at a different service.
  • Use max_concurrency 8 on a full key and let retries handle 429 responses.

The two-line change

When you search for the langchain openai base url, the answer is short. LangChain's OpenAI chat class wraps the OpenAI SDK, and that SDK accepts a custom base URL. Point it at this service and nothing else in your chain needs to change. Install the packages with pip install langchain-openai langchain-core first.

from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    model="uncensored",
    base_url="https://api.openaiapiproxy.com/v1",
    api_key="YOUR_KEY",
    temperature=0.8,
)

print(llm.invoke("Describe a rainy harbor town in two sentences.").content)

That is the entire integration. The service is OpenAI-compatible, but it is a separate product: one open-weight model, run by an independent provider, with no content refusals for lawful adult use. It is not the official API of OpenAI or any other vendor. To get a key, use the API key page; new accounts can claim a $0.50 trial with one click, with no card.

Why this works: ChatOpenAI builds standard chat completion requests and sends them to whatever address it was given. The service answers with the standard response shape, so LangChain's message objects, callbacks and parsers behave as they do against any other provider. You keep the same abstractions, and only the destination changes. Because the destination is a different model, you should re-test your prompts. A prompt written to coax a restrictive model into cooperating may be unnecessary here, and shorter instructions often work better.

Environment variables or explicit arguments

You can configure the same thing through the environment, and this is handy for deployments. The OpenAI SDK reads OPENAI_BASE_URL and OPENAI_API_KEY. Older LangChain documentation used OPENAI_API_BASE for the same purpose. Because names have shifted between versions, the safest habit is to pass base_url and api_key explicitly and read your own variable.

import os
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    model="uncensored",
    base_url="https://api.openaiapiproxy.com/v1",
    api_key=os.environ["UNCENSORED_API_KEY"],
    max_retries=3,
    timeout=120,
)

A real benefit of explicit arguments: if your project also uses another provider for other tasks, a stray global variable cannot redirect that traffic to the wrong place. Two ChatOpenAI objects with different base URLs live happily in the same process.

The same setting in LangChain.js

In the JavaScript package the base URL goes inside a configuration object that is handed to the underlying SDK.

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

const llm = new ChatOpenAI({
  model: "uncensored",
  apiKey: process.env.UNCENSORED_API_KEY,
  configuration: {
    baseURL: "https://api.openaiapiproxy.com/v1",
  },
});

const res = await llm.invoke("Give me three names for a haunted ship.");
console.log(res.content);

The capitalisation is the usual trap: Python uses base_url, the JavaScript SDK uses baseURL.

A prompt, a model and a parser

Once the chat model is built, the LangChain expression language works as usual. This chain turns a topic into a short scene and returns a plain string.

from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(model="uncensored", base_url="https://api.openaiapiproxy.com/v1", api_key="YOUR_KEY")

prompt = ChatPromptTemplate.from_messages([
    ("system", "You write short scenes for an adult fiction platform."),
    ("human", "Write a scene about: {topic}"),
])

chain = prompt | llm | StrOutputParser()
print(chain.invoke({"topic": "two smugglers meeting at a closed customs office"}))

for piece in chain.stream({"topic": "a stormy night at a mountain inn"}):
    print(piece, end="", flush=True)

Streaming relies on the service's server-sent events, which arrive as the model generates them. A streamed call holds one concurrency slot until it finishes.

The | operator composes runnables, so the same chain can later gain a retriever, a second model call or an output parser without touching the earlier steps. If you move a prompt template between providers, keep the template in a file so you can version it separately from the endpoint settings.

Tool calling and JSON output

LangChain turns Python functions into tool schemas with the @tool decorator and sends them through the standard tools parameter. The service supports tools, tool_choice and streamed tool_calls, so bind_tools works.

from langchain_core.tools import tool
from langchain_openai import ChatOpenAI

@tool
def lookup_character(name: str) -> str:
    """Return the notes for a character in the story bible."""
    notes = {"Mara": "A ferry pilot with a fear of deep water."}
    return notes.get(name, "No notes.")

llm = ChatOpenAI(model="uncensored", base_url="https://api.openaiapiproxy.com/v1", api_key="YOUR_KEY")
llm_tools = llm.bind_tools([lookup_character])

reply = llm_tools.invoke("What do we know about Mara?")
for call in reply.tool_calls:
    print(call["name"], call["args"])
    print(lookup_character.invoke(call["args"]))

For JSON mode, bind the response format and mention JSON in your instructions:

import json
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(model="uncensored", base_url="https://api.openaiapiproxy.com/v1", api_key="YOUR_KEY")
json_llm = llm.bind(response_format={"type": "json_object"})

msg = json_llm.invoke("Return a JSON object with keys title and summary for a pirate novel.")
print(json.loads(msg.content))

Even with JSON mode, wrap parsing in error handling. Whatever the provider, malformed output happens occasionally, and a single retry with the parse error attached usually fixes it.

Chat history and long documents

LangChain chat models take a list of messages, and the history is yours to manage. The message classes make the roles explicit, which is clearer than tuples once a prompt has several parts.

from langchain_core.messages import AIMessage, HumanMessage, SystemMessage
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(model="uncensored", base_url="https://api.openaiapiproxy.com/v1", api_key="YOUR_KEY")

messages = [
    SystemMessage(content="You are the narrator of a slow-burn mystery."),
    HumanMessage(content="Open with the detective arriving in town."),
]
first = llm.invoke(messages)
messages.append(AIMessage(content=first.content))
messages.append(HumanMessage(content="Now introduce the suspicious innkeeper."))
second = llm.invoke(messages)
print(second.content)
print(second.usage_metadata)

The context window is 100,000 tokens, prompt and completion together. That is room for long chat histories, a character sheet and several pages of reference text in one call. It does not mean you should always fill it. Input tokens are billed at $0.15 per 1M, so a 60,000-token prompt costs 60,000 / 1,000,000 x $0.15 = $0.009 per call before the model writes a word. A retrieval step that sends only the relevant three pages is cheaper and often gives better answers.

When a prompt is built from a lot of retrieved text, remember the 8 MB limit on the request body. It is far above what the token window allows for normal text, so you will usually hit the token limit first, but pasted data with long unbroken strings can behave differently.

Rate limits, batches and agents

LangChain makes parallel work easy, which is exactly how you hit limits by accident. A full key allows 8 concurrent requests and 300 per minute. A trial key allows 2 and 60. Use the config argument to cap parallelism in batches.

from langchain_openai import ChatOpenAI

llm = ChatOpenAI(model="uncensored", base_url="https://api.openaiapiproxy.com/v1", api_key="YOUR_KEY", max_retries=4)

titles = llm.batch(
    [f"Suggest a title for episode {i} of a space western." for i in range(1, 25)],
    config={"max_concurrency": 8},
)
for t in titles:
    print(t.content)

If you see HTTP 429, the error code says why: rate_limited for requests per minute, concurrency for too many at once. On temporary overload the response includes Retry-After: 5. The retry logic in the underlying SDK handles most of this, but agents that fan out into many parallel tool calls can still saturate a trial key, so lower max_concurrency to 2 there.

Agents loop: each step sends the growing conversation back as input, so input tokens add up faster than output tokens. At $0.15 per 1M input and $0.50 per 1M output, an agent run that sends 60,000 input tokens in total and produces 6,000 output tokens costs 60,000 / 1,000,000 x $0.15 + 6,000 / 1,000,000 x $0.50 = $0.009 + $0.003 = $0.012. The pricing page shows top-up sizes.

What does not carry over

  • Embeddings. There is no embeddings endpoint. If your retrieval chain builds on OpenAIEmbeddings, use another embedding source for that part and keep only the chat model here.
  • Images and audio. The service is text only.
  • Model switching. There is one model, uncensored. A dropdown of vendor model names will not match.
  • Token counting by name. Libraries that guess token counts from the model name may fall back to an approximation. For real numbers, read usage_metadata on the response message.

The context window is 100,000 tokens for prompt and completion together, which is generous for long chat histories and document stuffing. Check the docs for the full list of parameters, and remember that the model does not refuse lawful adult content, so add your own filters to any public-facing chain. The hard rule is no sexual content involving minors.

Debugging a chain that misbehaves

When a chain gives odd output, find out what actually went over the wire before you change the prompt. Three tools help, in order of effort.

  1. Print the response metadata. The message returned by invoke carries response_metadata and usage_metadata. You can check the finish reason and the token counts without any extra setup.
  2. Turn on debug logging. set_debug(True) from langchain_core.globals prints every prompt and response in a chain. Use it in development only, because it prints the full prompts.
  3. Replay the call with curl. Copy the rendered messages from the debug output into a request to https://api.openaiapiproxy.com/v1/chat/completions. If curl behaves the same, the issue is in your prompt. If it differs, the issue is in the chain.

Typical symptoms and where to look: a not-found error usually means the base URL has a doubled /v1 or is missing it. A message about an unknown model means the name is not uncensored. Empty tool call lists often mean the prompt did not make clear that the tool should be used, which a clearer tool description fixes. And an agent that stalls might simply be queued behind its own parallel calls on a trial key, so lower the concurrency.

Keep a note of the request ID header (X-Request-Id) from failures. If a problem is on the service side, the support page explains how to report it, and that ID speeds things up.

Questions and answers

What is the model name to use in LangChain?

Use uncensored. It is the only model ID, and GET /v1/models returns it.

Is it ChatOpenAI or OpenAI that takes the base URL?

Use ChatOpenAI from langchain-openai for chat completions. It takes base_url in Python and configuration.baseURL in JavaScript.

Can I use OpenAIEmbeddings with this endpoint?

No. Embeddings are not offered, so point the embeddings class at a different service.

Why do I get HTTP 429 in a batch?

You went over 8 concurrent requests or 300 requests per minute on a full key, or 2 and 60 on a trial key. Set max_concurrency and rely on retries.

Your key is one form away

Create an account, copy the key, change the base URL. That is the whole setup.

Get API key