SDKs & Integration

LangChain

Use OneRouter as your LangChain model provider for RAG, agents, structured output, and tool calling — with access to every model through a single endpoint.

Setup

OneRouter works as a drop-in ChatOpenAI provider. Just change openai_api_base:

python
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    model="gpt-4o",
    openai_api_key="sk-your-key",
    openai_api_base="https://api.onerouter.app/v1",
    temperature=0.7,
)
Model switching: Change model="gpt-4o" to model="claude-opus-4-8" or any other model ID — your LangChain code stays identical. No provider-specific code paths needed.

Basic Chain (LCEL)

python
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful assistant."),
    ("user", "{input}"),
])

chain = prompt | llm | StrOutputParser()
response = chain.invoke({"input": "Explain quantum computing in simple terms."})
print(response)

RAG (Retrieval-Augmented Generation)

Use OneRouter for both embeddings and generation in a single RAG pipeline:

python
from langchain_core.documents import Document
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_openai import OpenAIEmbeddings

# 1. Embed your documents
embeddings = OpenAIEmbeddings(
    model="text-embedding-3-large",
    openai_api_key="sk-your-key",
    openai_api_base="https://api.onerouter.app/v1",
)

# 2. Split and store (use a vector DB for production)
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
docs = text_splitter.create_documents([your_text])
vector_store = FAISS.from_documents(docs, embeddings)

# 3. Retrieve + generate
from langchain.chains import create_retrieval_chain
from langchain.chains.combine_documents import create_stuff_documents_chain

qa_chain = create_retrieval_chain(vector_store.as_retriever(), create_stuff_documents_chain(llm, prompt))
result = qa_chain.invoke({"input": "What is the refund policy?"})

Tool-Calling Agents

Create agents that can call your functions — all models that support function calling work with this pattern:

python
from langchain.agents import create_tool_calling_agent, AgentExecutor

def get_weather(city: str) -> str:
    return f"Weather in {city}: 22°C, partly cloudy"

tools = [get_weather]
agent = create_tool_calling_agent(llm, tools, prompt)
executor = AgentExecutor(agent=agent, tools=tools)

result = executor.invoke({"input": "What's the weather in Tokyo?"})
print(result["output"])

Other Compatible Libraries

Every OpenAI-compatible library works with OneRouter — just change the base URL:

LibraryHow to Configure
LlamaIndexSet OPENAI_API_BASE="https://api.onerouter.app/v1" env var
Vercel AI SDKcreateOpenAI({ baseURL: 'https://api.onerouter.app/v1', apiKey: 'sk-...' })
CrewAISet OPENAI_API_BASE environment variable
AutoGenConfigure base_url in the OpenAI client config
FlowiseAdd custom OpenAI-compatible credential with OneRouter base URL
DifyAdd OpenAI-compatible model provider with OneRouter endpoint