How to Build Your First AI App With LangChain in 2026

October 12, 2026

LangChain is an open-source Python and JavaScript framework for connecting large language models to your own data, tools, and APIs. This guide builds a small but real app: a chain that reads a PDF and answers questions about it.

Quick answer: Install LangChain with pip install langchain langchain-openai, set your API key, and you can have a working question-answering chain in about 30 minutes.

I keep the scope tight on purpose. Most tutorials try to cover agents, memory, and 12 integrations at once, and you end up copying code you don’t understand. Here you get one chain that works, then a few notes on where to go next.

What do you need before starting?

Python 3.10 or newer, and an API key from any provider LangChain supports. OpenAI works out of the box, but the same code runs on Anthropic, Google, or a local Ollama model with a three-line swap.

You do not need a GPU. The heavy lifting happens on the provider’s servers. Budget roughly $0.01-$0.05 per run on a mid-size document with GPT-4o-mini class models.

How do you install LangChain?

LangChain split into packages in late 2024, so install the core plus the integration you need. The old monolithic langchain package still works but wires up everything, which is slower to import.

pip install langchain langchain-openai langchain-community pypdf

Then export your key. Never hardcode it.

export OPENAI_API_KEY="sk-..."

How do you load and split a PDF?

Loaders live in langchain-community. Splitting matters more than people expect: chunks of 800-1000 characters with 100 characters of overlap tend to retrieve better than one giant blob.

from langchain_community.document_loaders import PyPDFLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter

docs = PyPDFLoader("report.pdf").load()
chunks = RecursiveCharacterTextSplitter(
    chunk_size=900, chunk_overlap=120
).split_documents(docs)

How do you store the chunks and retrieve answers?

Vector stores turn text into embeddings so you can search by meaning. Chroma is the least painful local option, and you can always move to a hosted store later.

from langchain_openai import OpenAIEmbeddings, ChatOpenAI
from langchain_chroma import Chroma
from langchain_core.runnables import RunnablePassthrough
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate

store = Chroma.from_documents(chunks, OpenAIEmbeddings())
retriever = store.as_retriever(search_kwargs={"k": 4})

prompt = ChatPromptTemplate.from_template(
    "Answer using only this context:\n{context}\n\nQuestion: {question}")

chain = ({"context": retriever, "question": RunnablePassthrough()}
         | prompt | ChatOpenAI(model="gpt-4o-mini") | StrOutputParser())

print(chain.invoke("What was Q3 revenue?"))

The | operator is the part worth understanding. Each piece takes the previous output and passes it forward. Debugging is easier when you print the output of each step instead of the final answer only.

Is LangChain free?

The library is MIT licensed and costs nothing. You pay your model provider. Running GPT-4o-mini over a 40-page PDF costs a few cents per question; the same query on a frontier model can be 20-50x more.

What are the common mistakes?

  • Chunks too large, so the retriever pulls irrelevant text and the model drifts.
  • Forgetting overlap, which cuts sentences in half at boundaries.
  • Not setting k, so you get the default handful of chunks whether or not they help.
  • Ignoring langchain_text_splitters as a separate package and hitting import errors.

Who this suits: developers who want to prototype RAG or tool-calling apps without building the glue from scratch. Who should skip it: if you only need a chat completion call, the raw OpenAI SDK is less code and fewer version upgrades.

Quick Summary

  • LangChain is a free open-source framework for connecting LLMs to data and tools.
  • Install with pip install langchain langchain-openai plus a vector store package.
  • Split documents into 800-1000 character chunks with overlap for better retrieval.
  • You pay only your model provider, typically cents per query.
  • Best for RAG and agent prototypes; overkill for a single chat call.