Getting Started with LangChain: Build Your First LLM Application
A practical tutorial to get started with LangChain — install, create chains, add memory, build RAG pipelines, and deploy. Step-by-step code examples included.
Introduction
LangChain makes it incredibly easy to build applications powered by large language models. In this tutorial, you will learn how to set up LangChain, create your first chain, add memory for conversation, and build a simple document Q&A system using RAG. By the end, you will have a working LLM application that you can extend with your own data and tools.
Prerequisites
You need Python 3.9+ and pip installed. A basic understanding of Python is helpful but not required. For the LLM backend, you can use OpenAI API or a local model via Ollama. We will use OpenAI for simplicity, but everything works the same with local models.
Step 1: Install LangChain
Install LangChain and the OpenAI integration package:
pip install langchain langchain-openai python-dotenvCreate a .env file with your API key:
OPENAI_API_KEY=sk-your-key-hereStep 2: Your First Chain
Create a file app.py and write your first LangChain application:
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
load_dotenv()
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
prompt = ChatPromptTemplate.from_messages([
("system", "You are a helpful AI assistant."),
("human", "{input}")
])
chain = prompt | llm
result = chain.invoke({"input": "What is LangChain?"})
print(result.content)Run it: python app.py. You should see a clear explanation of LangChain generated by the model.
Step 3: Add Conversation Memory
To make your chatbot remember past conversations, add memory:
from langchain.memory import ConversationBufferMemory
from langchain.chains import ConversationChain
memory = ConversationBufferMemory()
conversation = ConversationChain(llm=llm, memory=memory)
print(conversation.predict(input="Hi, my name is Alice."))
print(conversation.predict(input="What is my name?"))Step 4: Build a RAG Pipeline
RAG (Retrieval-Augmented Generation) lets your LLM answer questions about your documents. Install additional packages:
pip install langchain-community chromadb pypdfLoad a PDF, split it, embed it, and ask questions:
from langchain_community.document_loaders import PyPDFLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_openai import OpenAIEmbeddings
from langchain_community.vectorstores import Chroma
from langchain.chains import RetrievalQA
loader = PyPDFLoader("document.pdf")
docs = loader.load()
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
splits = text_splitter.split_documents(docs)
vectorstore = Chroma.from_documents(splits, OpenAIEmbeddings())
qa = RetrievalQA.from_chain_type(llm=llm, retriever=vectorstore.as_retriever())
print(qa.invoke("What is this document about?"))Step 5: Deploy with LangServe
Turn your chain into a REST API using LangServe (also available on Run This Ai). Install langserve and create a simple server:
pip install langserve uvicorn
# Then create a server.py and run it with uvicornConclusion
You have built your first LangChain application with chains, memory, and RAG. From here you can explore agents, custom tools, LangGraph for complex workflows, and LangSmith for observability. LangChain's ecosystem grows with you — start simple and scale up as your needs grow.