← Back to Blog
How to Use OkRouter with LangChain
Complete integration guide for Python and JavaScript with 20% cost savings
LangChain is one of the most popular frameworks for building AI applications. By integrating OkRouter with LangChain, you get access to 500+ AI models at 20% lower cost than OpenRouter, while maintaining full compatibility with LangChain's ecosystem.
Python Integration
Installation
pip install langchain langchain-openaiBasic Usage
from langchain_openai import ChatOpenAI
# Initialize OkRouter with LangChain
llm = ChatOpenAI(
model="claude-3-5-sonnet-20241022",
openai_api_key="sk-okr-your-api-key",
openai_api_base="https://api.okrouter.com/v1",
temperature=0.7
)
# Use it like any LangChain LLM
response = llm.invoke("Write a Python function to calculate fibonacci")
print(response.content)With LangChain Chains
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
# Create a prompt template
prompt = PromptTemplate(
input_variables=["topic"],
template="Write a blog post about {topic}"
)
# Create a chain
chain = LLMChain(llm=llm, prompt=prompt)
# Run the chain
result = chain.run(topic="AI development")
print(result)JavaScript/TypeScript Integration
Installation
npm install langchain @langchain/openaiBasic Usage
import { ChatOpenAI } from "@langchain/openai";
// Initialize OkRouter with LangChain
const llm = new ChatOpenAI({
modelName: "claude-3-5-sonnet-20241022",
openAIApiKey: "sk-okr-your-api-key",
configuration: {
baseURL: "https://api.okrouter.com/v1",
},
temperature: 0.7,
});
// Use it
const response = await llm.invoke("Write a TypeScript function");
console.log(response.content);Popular Models for LangChain
Claude 3.5 Sonnet$2.40/1M tokens
claude-3-5-sonnet-20241022GPT-4 Turbo$8.00/1M tokens
gpt-4-turboDeepSeek V3$0.27/1M tokens
deepseek-chatStart Building with LangChain + OkRouter
Get your free API key and save 20% on AI costs
Get Free API Key