Connect LangChain in four lines
If you want to use langchain open source models without hosting anything, the shortest path is an OpenAI-compatible API. LangChain's OpenAI chat class does not care who answers, as long as the endpoint speaks the same protocol. You change the base URL and the key, and the rest of your chains, prompts and agents stay as they are.
This service hosts one open-weight model, run by an independent provider and tuned to answer without content refusals for lawful adult use. It is not the official API of any model vendor and it is not a router across vendors. Install the package and create the model object:
pip install langchain-openai langchain-core
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
model="uncensored",
base_url="https://api.uncensoredmodelhub.com/v1",
api_key="YOUR_API_KEY",
temperature=0.7,
)
print(llm.invoke("Give me three names for a fantasy tavern.").content)
That is the whole connection. Keep the key in an environment variable in real projects and read it with os.environ instead of writing it into the file. If you do not have a key yet, the key page shows how to sign up and claim $0.50 of trial credit, which needs no card.
Node.js and LangChain.js
The JavaScript package follows the same idea, with the base URL passed through a configuration object.
import { ChatOpenAI } from "@langchain/openai";
const llm = new ChatOpenAI({
model: "uncensored",
apiKey: process.env.API_KEY,
temperature: 0.7,
configuration: { baseURL: "https://api.uncensoredmodelhub.com/v1" },
});
const reply = await llm.invoke("Summarize the plot of a heist film in two lines.");
console.log(reply.content);
If you see a 404 or an HTML page in the error, the base URL is usually off. It must end in /v1. The two endpoints behind the scenes are POST /v1/chat/completions and GET /v1/models, and nothing else is needed for LangChain's chat model.
Is an open source model in LangChain better hosted or local?
LangChain can also run open models on your own machine, for example through Ollama or a local server. That route has real advantages, and it is honest to say so. Local models cost no per-token fee, work offline, and keep every byte on your hardware. The price is hardware: you need enough GPU memory for the model you pick, you maintain the runtime, and throughput is limited by your machine.
| Question | Local server | This hosted API |
|---|---|---|
| Upfront cost | Hardware or a rented GPU | None; prepaid credit from $20 |
| Per-token cost | None, but power and hardware wear | $0.15 per 1M in, $0.50 per 1M out |
| Context window | Depends on memory you have | 100,000 tokens |
| Parallel requests | Depends on your setup | 8 on a full key |
| Data location | Your machine | Hosted; prompts not used for training |
A common pattern is to prototype against the hosted endpoint, then decide with real token counts whether a local deployment makes sense. Because both sides speak the OpenAI protocol, switching is a one-line change to base_url, and the chains you built stay untouched. The page on what an uncensored model is explains what the tuning changes about behavior.
Tools and a simple agent
Tool calling goes through the normal OpenAI-style tools field, and LangChain's bind_tools builds that for you from a decorated Python function. The model returns tool call requests, and results go back as role: "tool" messages.
from langchain_core.tools import tool
from langgraph.prebuilt import create_react_agent
@tool
def word_count(text: str) -> int:
'''Count the words in a piece of text.'''
return len(text.split())
agent = create_react_agent(llm, [word_count])
result = agent.invoke({"messages": [("user", "How many words are in 'the quick brown fox jumps'?")]})
print(result["messages"][-1].content)
The agent loop calls the model, runs the tool when asked, and sends the output back until the model answers in plain text. Two cautions apply. Give every tool a docstring written for the model, because that text is what it reads to pick a tool. And set a recursion limit on agents so a confused run cannot keep spending credit; LangGraph accepts a recursion_limit in the config.
For direct control without an agent, use llm.bind_tools([word_count]) and inspect .tool_calls on the returned message yourself.
Structured output with JSON mode
For extraction jobs, pass the JSON mode parameter through bind. The prompt must tell the model what keys you want, since JSON mode only pushes it to produce a valid JSON object.
import json
from langchain_core.prompts import ChatPromptTemplate
json_llm = llm.bind(response_format={"type": "json_object"})
prompt = ChatPromptTemplate.from_messages([
("system", "Return a JSON object with keys title, genre and summary."),
("human", "{idea}"),
])
chain = prompt | json_llm
raw = chain.invoke({"idea": "A lighthouse keeper finds a door in the sea floor."})
data = json.loads(raw.content)
Validate the result with Pydantic or a plain key check before you use it. The mode does not enforce a schema, so a missing field is possible, and a retry with a clearer prompt usually fixes it.
Streaming and concurrency
Streaming runs over server-sent events and works with the standard stream method. Usage is sent at the end of the stream.
for chunk in llm.stream("Write a four-line poem about rain on a tin roof."):
print(chunk.content, end="", flush=True)
When you process many items, batch runs them in parallel, and this is where limits matter. A full key allows 8 concurrent requests and 300 requests per minute. A trial key allows 2 and 60. A streamed request holds its slot until the stream ends. Pass a cap so LangChain never exceeds what your key allows:
prompts = [f"Write a one-line tagline for product {i}." for i in range(40)]
results = llm.batch(prompts, config={"max_concurrency": 6})
If you still see HTTP 429, the error code tells you which limit you hit: rate_limited for requests per minute, concurrency for too many at once. A 429 with a Retry-After: 5 header marks temporary overload. ChatOpenAI has a max_retries option that retries on such errors with backoff; set it to 3 or 4 for batch jobs.
A retrieval chain without an embeddings endpoint
This API covers generation only, so a retrieval chain needs two providers: one for embeddings and one for the chat step. LangChain makes that easy, because the vector store and the chat model are separate objects. Use any embedding model you like, from a local library or another service, to index your documents. Then pass the retrieved text to the chat model through the prompt.
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
prompt = ChatPromptTemplate.from_messages([
("system", "Answer using only the context. If the answer is not there, say so.\n\nContext:\n{context}"),
("human", "{question}"),
])
chain = prompt | llm | StrOutputParser()
answer = chain.invoke({"context": "\n\n".join(top_chunks), "question": "What is the refund window?"})
Here top_chunks is whatever your retriever returned. Keep it to the few passages that matter. Four chunks of 300 tokens add 1,200 tokens per call, which is fine, but forty chunks add 12,000 and make every request ten times heavier.
Troubleshooting checklist
- Authentication errors: confirm the key is in
api_key, not in a different parameter, and that noOPENAI_API_KEYenvironment variable is silently overriding what you meant to pass. - Model not found: the model ID is exactly
uncensored. CallGET /v1/modelsto see what the server lists. - Odd tool arguments: read the raw arguments string, tighten the tool description, and lower the temperature to around 0.2 for agent runs.
- Tracing and debugging: turn on LangChain's verbose or debug mode locally to see each prompt that goes out and each reply that comes back.
- Repeatable tests: pass
seedand a low temperature when you compare prompt versions, so differences come from your change and not from sampling luck.
Cost of a LangChain workload
Billing is per token from prepaid credit. There is no subscription, failed or refused requests are not charged, and credit never expires. Top-ups are any whole amount from $20 to $500, by crypto (USDT on TRC20, USDC on Base), with a 5% bonus from $50, 10% from $100 and 15% from $200. The pricing page has the details.
Work an example. A support bot answers 2,000 questions in a day. Each request sends about 1,500 tokens (system prompt, retrieved snippets, question) and returns about 250 tokens.
- Input: 2,000 x 1,500 = 3,000,000 tokens, so 3 x $0.15 = $0.45.
- Output: 2,000 x 250 = 500,000 tokens, so 0.5 x $0.50 = $0.25.
- Total: $0.70 per day for 2,000 answers, or $0.00035 each.
Retrieval chains are where costs can drift, because each extra retrieved chunk adds input tokens to every call. Log usage_metadata from responses, watch the average prompt size, and trim the chunk count before you scale up. The API docs list every supported parameter if you want to go further.
