LangChain Integration
LangChain is the leading framework for building applications powered by language models. It provides composable components for chains, agents, RAG, and memory, with support for dozens of LLM providers.
LangChain's ChatOpenAI class supports custom base URLs for OpenAI-compatible endpoints.
Prerequisites
- An LLM API account with an API key
- LangChain installed or accessible
Setup
Get Your LLM API Key
- Log in to your LLM API dashboard
- Click Create Key to Start
- Copy your new API key immediately — it will only be shown once
- Store the key securely (e.g., in a password manager or
.env file)
INFO
LLM API is an OpenAI-compatible gateway that gives you access to dozens of AI models through a
single API key and endpoint.
Configure LLM API in LangChain
- Install LangChain:
pip install langchain-openai
- Configure:
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
model="openai/gpt-4o",
api_key="your-llm-api-key-here",
base_url="https://api.llmapi.ai/v1"
)
- Or set environment variables:
export OPENAI_API_KEY="your-llm-api-key-here"
export OPENAI_API_BASE="https://api.llmapi.ai/v1"
Test the Integration
Verify that LangChain can successfully communicate with LLM API by sending a test request. All requests will now be routed through LLM API.
INFO
All LangChain chains, agents, and tools work seamlessly with LLM API through ChatOpenAI.
Benefits of Using LLM API with LangChain
- Multi-Provider Access: Use models from OpenAI, Anthropic, Google, and more through a single API
- Cost Control: Track and limit your AI spending with detailed usage analytics
- Unified Billing: One account for all providers instead of managing multiple API keys
- Caching: Reduce costs with response caching for repeated requests
INFO
View all available models on the models page.