Learning LangChain
Building AI and LLM Applications with
LangChain and LangGraph
Mayo Oshin and Nuno Campos
Learning LangChain
by Mayo Oshin and Nuno Campos
Copyright © 2025 Olumayowa “Mayo” Olufemi Oshin and O’Reilly Media, Inc. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are
also available for most titles ([Link] For more information, contact our corporate/institutional
sales department: 800-998-9938 or corporate@[Link].
Acquisitions Editor: Nicole Butterfield Indexer: Judith McConville
Development Editor: Corbin Collins Interior Designer: David Futato
Production Editor: Clare Laylock Cover Designer: Karen Montgomery
Copyeditor: nSight, Inc. Illustrator: Kate Dullea
Proofreader: Helena Stirling
February 2025: First Edition
Revision History for the First Edition
2025-02-13: First Release
See [Link] for release details.
The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Learning LangChain, the cover image,
and related trade dress are trademarks of O’Reilly Media, Inc.
The views expressed in this work are those of the authors and do not represent the publisher’s views.
While the publisher and the authors have used good faith efforts to ensure that the information and
instructions contained in this work are accurate, the publisher and the authors disclaim all responsibility
for errors or omissions, including without limitation responsibility for damages resulting from the use
of or reliance on this work. Use of the information and instructions contained in this work is at your
own risk. If any code samples or other technology this work contains or describes is subject to open
source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use
thereof complies with such licenses and/or rights.
978-1-098-16728-8
[LSI]
Table of Contents
Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
1. LLM Fundamentals with LangChain. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Getting Set Up with LangChain 3
Using LLMs in LangChain 4
Making LLM Prompts Reusable 7
Getting Specific Formats out of LLMs 13
JSON Output 14
Other Machine-Readable Formats with Output Parsers 15
Assembling the Many Pieces of an LLM Application 16
Using the Runnable Interface 16
Imperative Composition 18
Declarative Composition 20
Summary 22
2. RAG Part I: Indexing Your Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
The Goal: Picking Relevant Context for LLMs 24
Embeddings: Converting Text to Numbers 25
Embeddings Before LLMs 25
LLM-Based Embeddings 27
Semantic Embeddings Explained 27
Converting Your Documents into Text 30
Splitting Your Text into Chunks 32
Generating Text Embeddings 36
Storing Embeddings in a Vector Store 39
Getting Set Up with PGVector 40
Working with Vector Stores 41
Tracking Changes to Your Documents 44
Indexing Optimization 48
v
MultiVectorRetriever 49
RAPTOR: Recursive Abstractive Processing for Tree-Organized Retrieval 53
ColBERT: Optimizing Embeddings 54
Summary 56
3. RAG Part II: Chatting with Your Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Introducing Retrieval-Augmented Generation 57
Retrieving Relevant Documents 59
Generating LLM Predictions Using Relevant Documents 63
Query Transformation 68
Rewrite-Retrieve-Read 68
Multi-Query Retrieval 71
RAG-Fusion 74
Hypothetical Document Embeddings 78
Query Routing 81
Logical Routing 81
Semantic Routing 84
Query Construction 87
Text-to-Metadata Filter 87
Text-to-SQL 90
Summary 92
4. Using LangGraph to Add Memory to Your Chatbot. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Building a Chatbot Memory System 96
Introducing LangGraph 98
Creating a StateGraph 101
Adding Memory to StateGraph 105
Modifying Chat History 107
Trimming Messages 107
Filtering Messages 110
Merging Consecutive Messages 112
Summary 114
5. Cognitive Architectures with LangGraph. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Architecture #1: LLM Call 118
Architecture #2: Chain 121
Architecture #3: Router 125
Summary 133
6. Agent Architecture. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
The Plan-Do Loop 136
Building a LangGraph Agent 139
Always Calling a Tool First 143
vi | Table of Contents