LangChain
Use FreeModel models in LangChain chains and agents, in Python and JavaScript.
Overview
LangChain's OpenAI integration (ChatOpenAI) accepts a custom base URL, so every FreeModel chat model works through it with https://freemodel.in/v1. Chains, agents, streaming and tool binding then work as with any other chat model. For Claude models you can also use ChatAnthropic against FreeModel's Anthropic-format endpoint.
Prerequisites
- Python:
pip install langchain-openai(andlangchain-anthropicfor the Claude section). - JavaScript / TypeScript:
npm install @langchain/openai @langchain/core(and@langchain/anthropic). - A FreeModel key from Dashboard → API Keys, exported as
FREEMODEL_API_KEY. - Free accounts: free models only, such as
gpt-6-luna. Claude Haiku 5.5 and GPT-6.2 Astra are on the free plan; the other Claude models and premium models need a paid plan.
Python
import os
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
model="gpt-6-luna",
api_key=os.environ["FREEMODEL_API_KEY"],
base_url="https://freemodel.in/v1",
)
print(llm.invoke("Reply with the word ok.").content)
Streaming
for chunk in llm.stream("Explain what an API gateway does in three sentences."):
print(chunk.content, end="", flush=True)
Tool binding
from langchain_core.tools import tool
@tool
def get_weather(city: str) -> str:
"""Return the current weather for a city."""
return f"It is sunny in {city}."
llm_with_tools = llm.bind_tools([get_weather])
msg = llm_with_tools.invoke("What is the weather in Pune?")
print(msg.tool_calls)
msg.tool_calls holds the calls the model asked for. Run the tool and send the result back as a ToolMessage, or let a LangChain agent do that loop. The model needs the Tools capability.
JavaScript / TypeScript
In JS, the base URL goes inside configuration:
import { ChatOpenAI } from "@langchain/openai";
const llm = new ChatOpenAI({
model: "gpt-6-luna",
apiKey: process.env.FREEMODEL_API_KEY,
configuration: { baseURL: "https://freemodel.in/v1" },
});
const res = await llm.invoke("Reply with the word ok.");
console.log(res.content);
// streaming
for await (const chunk of await llm.stream("Name three uses of vector search.")) {
process.stdout.write(chunk.content);
}
Claude with ChatAnthropic (paid plan)
Claude models work through ChatOpenAI too. If you prefer LangChain's Anthropic integration, point it at FreeModel's Anthropic-format base URL, https://freemodel.in:
import os
from langchain_anthropic import ChatAnthropic
claude = ChatAnthropic(
model="claude-sonnet-5",
api_key=os.environ["FREEMODEL_API_KEY"],
base_url="https://freemodel.in",
)
print(claude.invoke("Reply with the word ok.").content)
This needs a paid plan; on a free account it returns 403 upgrade_required.
Embeddings
FreeModel does not offer an embedding model yet (/v1/embeddings answers embeddings_not_available). For retrieval, keep OpenAIEmbeddings pointed at another provider and use FreeModel for the chat model.
Which model ids to use
| Account | Ids |
|---|---|
| Free | gpt-6-luna; other free models: GPT-6.2 Astra, GPT-6 Luna, Grok 4.6, Grok 4.7, Claude Haiku 5.5, GLM-5.3 Flash, Qwen 3.8 Omni Flash and DeepSeek V4.1 Flash 1017. |
| Paid | Any id on Models, e.g. claude-sonnet-5 (3×), gpt-6-astra (1.75×), claude-haiku-5.5 (1.5×). |
Use the exact id. FreeModel does not map unlisted names (such as gpt-4o, a common default in examples) to another model. Agents with long histories send many input tokens per step; input and output both count, at the model's multiplier.
Common errors and fixes
LangChain passes the SDK's exceptions through, so the status code is on the error (for example openai.AuthenticationError for 401 in Python).
| Status | What it means | Fix |
|---|---|---|
401 | Bad or missing key. | Check FREEMODEL_API_KEY. An error mentioning OpenAI's own site means base_url was not applied. |
403 upgrade_required | Free account, paid model. | Use a free id, or buy a plan. |
404 model_not_found | Model id not listed. | Copy it from Models. |
402 | Tokens used up (tokens_exhausted). | Renew or upgrade. Refused requests are not charged. |
429 | Plan requests-per-minute limit. | The SDK retries a few times; lower concurrency in batch runs. |
Related pages
- OpenAI SDKs — the client LangChain wraps
- Tool Calling and Streaming
- Tool calling explained — blog