Key points
- ChatOpenAI takes base_url, api_key and model, so any OpenAI-compatible endpoint works.
- Use base URL https://api.uncensoredllmhub.com/v1 and model uncensored.
- Tool calling is supported; for structured output, JSON mode is the safest method.
- Respect limits: 8 concurrent requests on a full key, so cap your batch concurrency.
Install and connect
LangChain's OpenAI integration is a separate package from the core library. Install it with pip install langchain-openai. Get a key from the API key page; new users can claim $0.50 of trial credit there with one click, no card needed.
Then create the model object. Three arguments matter: model, base_url and api_key.
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
model="uncensored",
base_url="https://api.uncensoredllmhub.com/v1",
api_key="YOUR_KEY",
temperature=0.7,
)
print(llm.invoke("Give me three names for a lighthouse cat.").content)That is the whole integration. Everything downstream, such as prompts, parsers, agents and retrievers, treats llm like any other chat model. If you prefer environment variables, the OpenAI SDK reads OPENAI_API_KEY and OPENAI_BASE_URL, which makes it easy to switch between environments without touching code.
What the endpoint supports
The service exposes POST /v1/chat/completions, POST /v1/responses and GET /v1/models. ChatOpenAI uses chat completions by default, which is the safe choice. The model is one open-weight model tuned to answer without content refusals for lawful adult use, with a 100,000-token context window shared by prompt and completion.
Supported request features that LangChain relies on: streaming over SSE with usage at the end, tool calling, JSON mode, and the sampling parameters temperature, top_p, stop, seed and the penalties. Not available: embeddings, image or audio generation, fine-tuning. If a retrieval chain needs embeddings, use a different embedding model for that step and keep this endpoint for generation. Full parameter notes are in the docs.
A prompt, a model and a parser
The basic LCEL pattern pipes a prompt into the model and then into an output parser. Streaming comes for free with .stream().
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
llm = ChatOpenAI(model="uncensored", base_url="https://api.uncensoredllmhub.com/v1", api_key="YOUR_KEY")
prompt = ChatPromptTemplate.from_messages([
("system", "You are a concise editor. Reply in {tone} tone."),
("human", "Improve this sentence: {text}"),
])
chain = prompt | llm | StrOutputParser()
print(chain.invoke({"tone": "plain", "text": "The thing was done by the team."}))
for piece in chain.stream({"tone": "friendly", "text": "We regret to inform you of a delay."}):
print(piece, end="", flush=True)The system message sets behaviour for every call. Because the context window is large, you can also feed long reference text through the prompt template without a splitter in many cases. Input costs $0.15 per 1M tokens, so a 20,000-token document in the prompt costs 20,000 / 1,000,000 x $0.15 = $0.003 per call.
Tool calling
Tool calling follows the OpenAI format: you pass function definitions, the model replies with tool_calls, you run the function and return the result as a message with the tool role. LangChain wraps this in bind_tools.
from langchain_core.tools import tool
from langchain_openai import ChatOpenAI
@tool
def add(a: int, b: int) -> int:
"""Add two integers."""
return a + b
llm = ChatOpenAI(model="uncensored", base_url="https://api.uncensoredllmhub.com/v1", api_key="YOUR_KEY")
llm_tools = llm.bind_tools([add])
msg = llm_tools.invoke("What is 41 plus 1? Use the tool.")
for call in msg.tool_calls:
print(call["name"], call["args"]) # run the function yourself, then send a ToolMessage backThe model asks for the call; your code runs it. In an agent framework such as LangGraph, a loop does this for you until the model answers in plain text. Keep tool descriptions short and argument names plain. If the model ignores a tool, tighten the docstring and set tool_choice to force the call while you debug.
Structured output with JSON mode
The endpoint supports JSON mode, response_format: {"type":"json_object"}. In LangChain, ask for it through with_structured_output and choose the JSON mode method, then validate with a Pydantic model.
from pydantic import BaseModel
from langchain_openai import ChatOpenAI
class Ticket(BaseModel):
title: str
priority: str
llm = ChatOpenAI(model="uncensored", base_url="https://api.uncensoredllmhub.com/v1", api_key="YOUR_KEY")
structured = llm.with_structured_output(Ticket, method="json_mode")
print(structured.invoke("Return JSON with title and priority for: login page crashes on Safari, urgent."))Two habits make JSON reliable. First, say "JSON" and name the keys in the prompt, since JSON mode guarantees valid syntax but not your schema. Second, wrap the call in a retry that catches validation errors and sends the error text back to the model once. Do not depend on schema-constrained decoding unless you have tested it with this endpoint.
Concurrency, retries and cost control
LangChain makes parallel calls easy with .batch(), and that is where limits bite. A full key allows 8 concurrent requests and 300 per minute. A trial key allows 2 and 60. Pass config={"max_concurrency": 6} to batch so you stay under the cap.
When the limit is exceeded, the API returns HTTP 429 with code rate_limited or concurrency, and a Retry-After: 5 header on temporary overload. ChatOpenAI has a max_retries argument that handles these with backoff; set it to 3 to 5. Streams occupy a slot until they finish, so do not leave half-read streams open.
Worked cost example: a batch job of 2,000 items, each with 800 input tokens and 150 output tokens. Input: 2,000 x 800 = 1,600,000 tokens = 1.6 x $0.15 = $0.24. Output: 2,000 x 150 = 300,000 tokens = 0.3 x $0.50 = $0.15. The job costs $0.39. Failed or refused requests are not charged, and the X-Balance-USD header lets you watch your balance. See pricing for top-ups, which run from $20 to $500 and can be paid by crypto.
Agents and conversation memory
Two patterns come up right after the first chain works. The first is conversation memory. LangChain's message-history helpers store past turns and prepend them to each call. Since the window is 100,000 tokens, you can keep long histories, but remember that every turn resends them. A 30,000-token history on each of 50 turns means 1,500,000 input tokens, or 1.5 x $0.15 = $0.225 for the session. Summarising old turns once the history passes a size limit keeps that flat.
The second is the agent loop. In LangGraph, a prebuilt agent takes the same llm object plus a list of tools and loops until the model stops asking for tools. Set a maximum number of steps so a confused loop cannot run up input tokens. Log the X-Request-Id response header for any call that behaves oddly, since it is the identifier to quote if you contact support.
Troubleshooting
- 404 on every call. The base URL must end in
/v1and must not include/chat/completions. - Model not found. Use
uncensoredexactly. LangChain does not validate model names for custom endpoints. - Token counting warnings. Some LangChain features estimate tokens with OpenAI tokenizers. The counts can differ slightly from the real model, so rely on the usage returned by the API.
- Long agent loops get expensive. Each loop resends the full history. Trim old tool output before it reaches the next call.
If your chain looks right and still fails, call the endpoint with plain curl to separate a LangChain problem from an account problem.

