Using LangChain with a Free LLM API Key — Full Setup Guide
Build LangChain pipelines and RAG applications with a free OpenAI-compatible API key — complete setup guide with working code.
What Is LangChain and Why Do Developers Use It?
LangChain is a Python (and JavaScript) framework for building applications powered by language models. It provides abstractions that make it easier to chain together LLM calls, manage conversation history, connect LLMs to external tools and data sources, and build retrieval-augmented generation (RAG) systems. If you are building anything beyond a simple API wrapper — a document Q&A system, an AI agent, a multi-step reasoning pipeline — LangChain saves you from reinventing these wheels.
The good news: LangChain works with any OpenAI-compatible API. Point it at your own free OpenRouter key and you have a full LangChain environment running at zero cost.
Installation
pip install langchain langchain-openai langchain-community faiss-cpu
These are the core packages you need. faiss-cpu is for the RAG example later in this guide.
Basic LangChain Setup with a Free Key
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage, SystemMessage
# Initialize the LLM pointing at OpenRouter (create a free key at openrouter.ai)
llm = ChatOpenAI(
model="openai/gpt-4o",
openai_api_key="sk-your-key-here",
openai_api_base="https://openrouter.ai/api/v1",
temperature=0.7
)
# Simple single call
response = llm.invoke([
SystemMessage(content="You are a helpful coding assistant."),
HumanMessage(content="What is the difference between a list and a tuple in Python?")
])
print(response.content)
Building a Simple Chain
LangChain's power comes from chaining components. Here is a prompt template → LLM → output parser chain:
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
llm = ChatOpenAI(
model="openai/gpt-4o",
openai_api_key="sk-your-key-here",
openai_api_base="https://openrouter.ai/api/v1"
)
prompt = ChatPromptTemplate.from_messages([
("system", "You are a {role}. Respond in a {style} style."),
("user", "{question}")
])
chain = prompt | llm | StrOutputParser()
result = chain.invoke({
"role": "senior Python developer",
"style": "clear and concise",
"question": "How should I structure a large Python project?"
})
print(result)
Building a RAG Pipeline (Retrieval-Augmented Generation)
RAG lets you answer questions based on your own documents. Here is a complete example using FAISS as the vector store:
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from langchain_community.vectorstores import FAISS
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
# Embeddings need an embeddings-capable endpoint. Google AI Studio's
# OpenAI-compatible endpoint offers free embeddings (create a key at aistudio.google.com)
embeddings = OpenAIEmbeddings(
openai_api_key="your-google-ai-studio-key",
openai_api_base="https://generativelanguage.googleapis.com/v1beta/openai/",
model="text-embedding-004"
)
# Your documents
documents = [
"OpenRouter gives you one key for GPT-4o, Claude, Gemini and 300+ models.",
"Each provider's free key is created on its own site in about a minute.",
"All of these providers are OpenAI-compatible — you only change base_url and api_key.",
]
# Build vector store
vectorstore = FAISS.from_texts(documents, embeddings)
retriever = vectorstore.as_retriever()
# RAG prompt
prompt = ChatPromptTemplate.from_template(
"Answer the question based only on the following context:\n\n{context}\n\nQuestion: {question}"
)
llm = ChatOpenAI(
model="openai/gpt-4o",
openai_api_key="sk-your-key-here",
openai_api_base="https://openrouter.ai/api/v1"
)
chain = (
{"context": retriever, "question": RunnablePassthrough()}
| prompt
| llm
| StrOutputParser()
)
answer = chain.invoke("How many models does one OpenRouter key unlock?")
print(answer)
Total Cost: $0
With your own free keys, all of the above — the chain, the RAG pipeline, the embeddings — runs at zero cost. The rate-limited free tiers are more than enough for development and testing these patterns.
When to Switch from Free Keys to Paid
Free keys are perfect for development. Switch to a paid plan when:
- Your application has external users who depend on it for production workflows
- You need more sustained throughput than a free tier allows
- You need to process large document batches overnight at high volume
- Data privacy is a hard requirement (use the official provider APIs)
Until then, a free provider key and LangChain together give you a complete AI development environment for free. Browse the provider directory to pick one.