Back to Blog
TutorialsJune 5, 20258 min read

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 FreeLLMKeys 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 FreeLLMKeys

from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage, SystemMessage

# Initialize the LLM pointing at FreeLLMKeys
llm = ChatOpenAI(
    model="gpt-4o",
    openai_api_key="sk-your-key-here",
    openai_api_base="https://aiapiv2.pekpik.com/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="gpt-4o",
    openai_api_key="sk-your-key-here",
    openai_api_base="https://aiapiv2.pekpik.com/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

# Use the same FreeLLMKeys endpoint for embeddings too
embeddings = OpenAIEmbeddings(
    openai_api_key="sk-your-key-here",
    openai_api_base="https://aiapiv2.pekpik.com/v1",
    model="text-embedding-3-small"
)

# Your documents
documents = [
    "FreeLLMKeys provides free API keys updated daily.",
    "Keys are OpenAI-compatible and expire in 24-48 hours.",
    "Supported models include GPT-4o, Claude, Gemini, and DeepSeek.",
]

# 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="gpt-4o",
    openai_api_key="sk-your-key-here",
    openai_api_base="https://aiapiv2.pekpik.com/v1"
)

chain = (
    {"context": retriever, "question": RunnablePassthrough()}
    | prompt
    | llm
    | StrOutputParser()
)

answer = chain.invoke("How long do FreeLLMKeys keys last?")
print(answer)

Total Cost: $0

With a FreeLLMKeys key, all of the above — the chain, the RAG pipeline, the embeddings — runs at zero cost. The rate limit of 3–20 RPM is 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 than 3–20 RPM sustained
  • You need to process large document batches overnight (free key budget may run out)
  • Data privacy is a hard requirement (use the official provider APIs)

Until then, FreeLLMKeys and LangChain together give you a complete AI development environment for free.

F
FreeLLMKeys Team
Building tools for the AI developer community