A person waves and holds a document while sitting on a row of machines labeled Input, Process, Filter, and Output, joined by a gold chain, with a brain in a jar on top.

LangChain Tutorial: From Prompts to RAG and AI Agents

by Martin Breuss 0 Comments intermediate ai

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 SystemMessage sets a chat model’s behavior, a HumanMessage carries the user’s input, and an AIMessage holds 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.

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 Agents

Check your grasp of LangChain in Python, from chat messages and prompt templates to LCEL chains, RAG retrieval, and tool-calling agents.

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:

Language: Shell
(.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:

Language: Shell
(.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:

Language: .env
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.

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!

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Article

Already a member? Sign-In

Locked learning resources

The full article is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Article

Already a member? Sign-In

About Martin Breuss

Martin is Real Python's Head of Content Strategy. With a background in education, he's worked as a coding mentor, code reviewer, curriculum developer, bootcamp instructor, and instructional designer.

» More about Martin

Each tutorial at Real Python is created by a team of developers so that it meets our high quality standards. The team members who worked on this tutorial are:

What Do You Think?

What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment below and let us know.

Commenting Tips: The most useful comments are those written with the goal of learning from or helping out other students. Get tips for asking good questions and get answers to common questions in our support portal.


Looking for a real-time conversation? Visit the Real Python Community Chat or join the next “Office Hours” Live Q&A Session. Happy Pythoning!

Become a Member to join the conversation.

Keep Learning

Related Topics: intermediate ai