LangChain is a framework for Python and JavaScript that lets you build applications powered by large language models (LLMs). Sending a single prompt to a model takes only a couple of lines of code. However, real apps also need reusable instructions, access to your own data, and a way for the model to call your Python code. In this tutorial, you’ll explore how LangChain’s Python library handles each of these.
Say a group of hospitals just handed you a spreadsheet full of patient reviews, together with a short request: Find out what patients are saying about us. You could read through every row yourself, but by the time you’re done, the next batch of reviews will probably have arrived. Instead, you’ll teach an LLM to do the reading for you.
Along the way, you’ll cover chat models, prompt templates, chains, retrieval, and agents. Step by step, you’ll build up to a RAG app that answers questions about what patients said and an agent that picks the right tool to look up exact facts.
By the end of this tutorial, you’ll understand that:
- LangChain offers a modular interface to LLM providers like OpenAI, so switching models doesn’t mean rewriting your app.
- A
SystemMessagesets a chat model’s behavior, aHumanMessagecarries the user’s input, and anAIMessageholds the model’s reply. - Prompt templates turn prompts into reusable recipes, and the pipe (
|) operator in LCEL chains them with models, parsers, retrievers, and any other runnable. - In retrieval-augmented generation (RAG), a retriever uses semantic search to pull the most relevant reviews and ground the model’s answers in real context.
- An agent lets the language model choose which tools to call instead of following steps you define in code.
To get the most out of this tutorial, you should be comfortable with Python functions, dictionaries, and comprehensions and have some familiarity with decorators and type hints. You don’t need any prior experience with LLMs, but a basic understanding of embeddings and vector databases will help in the RAG section. If you don’t have all of this background knowledge yet, that’s okay! You might learn more by going ahead and getting started.
Each section covers one feature. You’ll run most examples in the Python REPL and a few as scripts from the command line. You’ll start by preparing your environment and installing LangChain.
Get Your Code: Click here to download the free sample code you’ll use to build a RAG app and a tool-calling agent that answer questions about patient reviews with LangChain.
Take the Quiz: Test your knowledge with our interactive “LangChain Tutorial: From Prompts to RAG and AI Agents” quiz. You’ll receive a score upon completion to help you track your learning progress:
Interactive Quiz
LangChain Tutorial: From Prompts to RAG and AI AgentsCheck your grasp of LangChain in Python, from chat messages and prompt templates to LCEL chains, RAG retrieval, and tool-calling agents.
AI Agent From Scratch Live Course: Want to see what frameworks like LangChain do under the hood? In Real Python’s Build an AI Agent From Scratch in Python live course, you write the agent loop by hand, then recognize every piece of it in the tools you already use. Join the waitlist to hear about the next dates.
Set Up a LangChain Environment
To get started, create a folder for this tutorial’s Python files. Set up a virtual environment to install the required tools and libraries. Make sure you have Python 3.10 or later.
Activate your virtual environment and install the following libraries:
(.venv) $ python -m pip install langchain==1.4.3 langchain-openai==1.6.6
With this command, you install the langchain library, which is the main subject of this tutorial. You also install langchain-openai to use OpenAI’s services.
You’ll also need to install python-dotenv to help you manage environment variables:
(.venv) $ python -m pip install python-dotenv==1.2.3
With this package, you can load environment variables from .env files into your Python environment. You’ll find this handy for keeping your API key out of your code.
You’ll use OpenAI’s models, so make sure you have an OpenAI account, a valid API key, and credits in your account. Running the examples in this tutorial might cost only a small amount, but every model and embedding call is billed.
Create a file called .env in your working directory and add your key to it:
OPENAI_API_KEY=<YOUR_OPENAI_API_KEY>
Every script or REPL session in this tutorial calls dotenv.load_dotenv() to read the .env file and load your key into the environment before using any OpenAI model.
Note: Your .env file contains your API key in plain text. If your working directory is under version control, add .env to your .gitignore file so you don’t accidentally commit your key. If you commit your key and publish the repository, revoke the key and generate a new one.
You can run each section’s code on its own, but the concepts build on each other, so work through the sections in order if you’re new to LangChain. In the RAG and agent sections, you’ll create scripts and use reviews.csv from the tutorial’s materials.
You’re now ready to start exploring LangChain!