Lang Graph
Lang Graph
Advanced
Intermediate
Basic
Course Overview 💬
1. Levels of Autonomy in LLM applications (Code -> LLM Call -> Chain -> Router ->
Agent)
5. Graph Structure
- Direct Acyclic Graph (DAG) vs Cyclic Graph
6. What is LangGraph?
15. Persistence
Course Overview 💬
1. Code
We all know that everything is hard-coded and it is not even really a cognitive
architecture.
Disadvantage:
The problem? You'd need to write rules for every possible scenario - making it
impossible to handle real-world complexity.
Levels of Autonomy in LLM applications
2. LLM call
A single LLM call means your app basically does one main thing - you give it an
input, it processes it, and gives you back an output.
Think of chatbots that just take your message and respond, or apps that
translate text.
This was a huge leap from hard-coded rules, even though it's still pretty simple
and is only in the 2nd stage of autonomy
Levels of Autonomy in LLM applications
2. LLM call
Example User Input: You are an expert LinkedIn post writer. Write me a post on "AI
Agents Taking over Content Creation"
Levels of Autonomy in LLM applications
2. LLM call
A single LLM call means your app basically does one main thing - you give it an
input, it processes it, and gives you back an output.
Think of chatbots that just take your message and respond, or apps that
translate text.
This was a huge leap from hard-coded rules, even though it's still pretty simple
and is only in the 2nd stage of autonomy
Disadvantage:
Trying to get everything done in one shot often leads to confused or mixed-up
responses - just like how a single person can't be an expert at everything.
Levels of Autonomy in LLM applications
3. Chains
Think of chains like having multiple specialists instead of one generalist. Instead
of asking one AI to do everything, we break it down into steps where each AI is
really good at one thing.
Imagine a customer service chatbot: The first AI reads your complaint and
figures out exactly what product you're talking about
The second AI finds the right solution from the company's help docs, and the
third AI turns that solution into a friendly response.
Each step is simple, but together they create a much smarter system than a
single LLM call could.
Levels of Autonomy in LLM applications
3. Chains (contd.)
This is where we first started seeing AI apps that could handle more complex
tasks - not just by being smarter, but by breaking big problems into smaller,
manageable pieces.
Disadvantage:
The downside? These fixed sequences are like a rigid assembly line - they always
follow the same steps defined by the human.
Levels of Autonomy in LLM applications
3. Chains (contd.)
Prompt
LLM Output
template {title}
LinkedIn
Twitter Prompt
Post title LLM Output
template {title}
Blog post
Prompt
LLM Output
template {title}
4. Router
Now this is where it gets interesting - routers are like smart traffic cops for your
AI. Instead of having a fixed path like in chains, the AI itself decides what steps
to take next.
Imagine a personal assistant bot: when you ask it something, it first figures out
if you need help with scheduling, research, or calculations, then routes your
request to the right tool or chain for the job.
Levels of Autonomy in LLM applications
Prompt
LLM Output
template {title}
Twitter chain
4. Router
Now this is where it gets interesting - routers are like smart traffic cops for your
AI. Instead of having a fixed path like in chains, the AI itself decides what steps
to take next.
Imagine a personal assistant bot: when you ask it something, it first figures out
if you need help with scheduling, research, or calculations, then routes your
request to the right tool or chain for the job.
Disadvantage:
While it can choose different paths, it still can't remember previous
conversations or learn from mistakes.
Levels of Autonomy in LLM applications
And many more, and THIS is where LangGraph comes into the picture
Levels of Autonomy in LLM applications
5. State Machine
Approval
LinkedIn Script
writer agent
Step
Tool 1
Social Media
Example User Input: Write me a LinkedIn Publisher agent
post on "AI Agents Taking over Content Tool 2
Creation"
Tool 3
Levels of Autonomy in LLM applications
Chain/Router vs Agent
Whereas in a state machine, we can go back in the chain, have cycles and the
flow is controlled by the LLM, hence it is called an Agent
Understanding AI Agents
AI Agents & Tools
In the case of Chains and Router, they follow our specific instruction.
But with agents, they actually take it a step further. They can decide for
themselves what steps to take on their own.
AI Agents & Tools
Tools are specific functions that Agents can use to complete tasks
Just like a chef's kitchen tools (knife for cutting, oven for baking, blender for
mixing), tools are the special abilities we give to AI - like giving it a calculator
tool, or a search engine tool, or a calendar tool
AI Agents & Tools (Re-Act Agent Pattern)
This is one of the best known patterns in AI today to build agents. It stands for
Reasoning + Acting
Re-Act Agent
Tools LLM
Let's Jump Into The Code
2. What are its drawbacks and where does LangGraph come into the picture?
ReAct Agents
tool 2
Start End
tool 1 tool 2
Start End
tool 2 tool 1
Start End
ReAct Agents
tool 1 tool 2
Start End
Tools LLM
tool 1 tool 2
Start End
Tools LLM
2. State Persistence:
Automatically saves and manages state, supporting pause and resume for
long-running conversations.
4. Streaming Processing:
Supports streaming output and real-time feedback on execution status to
enhance user experience.
1. Nodes
2. Edges
3. Conditional Edges
4. State
Example: Reflection Agent pattern
_start_
Generate
tweet LLM
Like how you're looking at your reflection in the mirror, reflection means
looking at yourself or your actions
For example:
A reflection agent pattern is an AI system pattern that can look at its own
outputs and think about them/make it better - just like how we look at
ourselves in a mirror and self-reflect, make ourselves better
1. A generator agent
2. A reflector agent
Example: Basic Reflection Agent pattern
_start_
Tweet generation
agent
Tweet critique
_END_
agent
Reflection Agent pattern in LangGraph
Types of Reflection Agents in LangGraph
What is a MessageGraph?
It is a class that LangGraph provides that we can use to orchestrate the flow
of messages between different nodes
What is a MessageGraph?
The reflexion agent, similar to reflection agent, not only critiques it's own
responses but also fact checks it with external data by making API calls (Internet
Search)
In the Reflection agent pattern, we had to rely on the training data of LLMs but in
this case, we're not limited to that.
Reflexion Agents in LangGraph
Episodic memory
This is crucial for making agents feel more context-aware, personalized, and
human-like over time.
Reflexion Agent System
Let's Implement a Reflexion Agent!
LLM Response Parser System
The system converts unstructured LLM outputs into well-defined Python objects
through a series of structured parsing steps, ensuring data validation and
consistent formatting.
Function calling:
Similar to how we make tools available to the LLM, we can also send a schema to
the LLM and force it to structure it's JSON output according to the schema
Pydantic:
A Python library that defines data structures using classes
Provides automatic validation of JSON data against these class definitions
LLM Response Parser System
3. Pydantic Parser
LangChain:
It parses the response of the LLM into one of these two classes: AgentAction or
AgentFinish
Re-Act Agent using LangGraph
AgentAction:
This is a LangChain class that represents an action the agent wants to take. It
typically contains:
Re-Act Agent using LangGraph
AgentFinish:
This represents the agent completing its task with a final answer. It typically
contains:
ReAct Agent
2. AgentExecutor:
Takes the agent from create_react_agent and manages the execution loop
Receives the user's question and feeds it to the agent
Identifies which tool to run based on the agent's output (AgentAction or No tool
if AgentFinish)
Executes the tool and captures the result
Feeds the result back to the agent for the next decision
Continues this loop until the agent produces an AgentFinish
Returns the final answer to the user
ReAct Agent - LangGraph
Key Advantage:
LangGraph turns the hidden "black box" loop into a visible, editable workflow
You can now add custom nodes, modify the flow, or insert additional logic
Re-Act Agent using LangGraph
Results from the tool flow back to "reason" node for the
reason node
next decision
When the agent has the final answer, it takes the right
path to "end"
act node End
This visualization makes the "black box" of
AgentExecutor transparent and modifiable
State in LangGraph - Deep Dive
1. What is StateGraph?
Start
increment
should_continue End
ReAct Agent in LangGraph - Deep Dive
1, Basic Chatbot
- No memory
- No tools
We'll learn:
When you build a basic chatbot using LangGraph, you run into an immediate
limitation: by default, your chatbot has amnesia.
Every time a user starts a conversation, the bot has no recollection of previous
interactions.
This happens because without memory management, each invocation of your
graph is completely independent.
This is where the concept of checkpointers in LangGraph come into the picture
Memory & Checkpointers
What is a Checkpointer?
Think of it like saving your progress in a video game. When you reach a
checkpoint:
1. The current state of everything is saved
2. If something goes wrong later, you can return to this saved point
3. You don't have to start over from the beginning
Memory & Checkpointers
Thread ID:
A thread ID is simply a unique identifier for each specific conversation or workflow execution. Think
of it like:
- A unique session ID for a user
- A conversation ID that groups related messages together
3. The thread ID helps the system know which saved state belongs to which conversation
Without thread IDs, all your conversations would share the same state, which would cause confusion
and errors.
Human In The Loop
This is especially useful in LLM-based applications, where the underlying model may
generate occasional inaccuracies.
Use-cases:
1. Reviewing tool calls: Humans can review, edit, or approve tool calls requested by the LLM
before tool execution.
2. Validating LLM outputs: Humans can review, edit, or approve content generated by the
LLM
3. Providing context: Enable the LLM to explicitly request human input for clarification or
additional details or to support multi-turn conversations.
Human In The Loop (Design Patterns)
There are typically three different actions that you can do with a human-in-the-loop
workflow:
1. Approve or Reject:
Pause the graph before a critical step, such as an API call, to review and approve the action.
If the action is rejected, you can prevent the graph from executing the step, and potentially
take an alternative action. This pattern often involve routing the graph based on the human's
input.
Human In The Loop (Design Patterns)
There are typically three different actions that you can do with a human-in-the-loop
workflow:
1. Approve or Reject:
A human can review and edit the state of the graph. This is useful for
correcting mistakes or updating the state with additional information.
Human In The Loop (Design Patterns)
A human can review and edit the output from the LLM before proceeding.
This is particularly critical in applications where the tool calls requested by the LLM
may be sensitive or require human oversight.
Human In The Loop (Design Patterns)
Start
generate_post
post collect_feedback
END
Human In The Loop
Drawbacks of input():
This is why we use a special method that LangGraph provides called "interrupt"
Human In The Loop
START
node_a
node_b
node_c
END
Command Class
START
node_a
node_b
node_c node_d
END
Interrupts
1. Resume - Continue execution with input from the user without modifying the state
2. Update and Resume - Update the state and then continue execution
4. Branch - Create a new branch from the current execution state to explore alternative
paths
Each of these operations gives you different ways to control the flow of your graph when it's
interrupted
Structured Outputs
It is often useful to have a model return output that matches a specific schema that we
define
We have options to get outputs in formats such as - JSON, Dictionary, string, YAML, HTML
Structured Outputs
3. Uses Python's type hints (like str, int) to enforce correct data types
1. Define a class with the fields you need (name, capital, language)
1. Knowledge-base construction:
[1, 0 ,1,
Chunk 0, 0]
Source
Documents Chunking [1, 0 ,1,
(~1K tokens) Chunk Vector DB
(~1M tokens) 0, 0]
2. Query Processing:
LLM
Chunk """
Chunk 1 Text
Chunk 2 Text
[1, 0 ,1, Retriever Chunk Chunk 3 Text
Query 0, 0]
Question
Query """
Classification-Driven Retrieval System
Advanced multi-step RAG System
Advanced multi-step RAG System
Without rephrasing, the follow-up query lacks context on its own and would likely return
irrelevant results if sent directly to a retrieval system.
The rephrasing node transforms "What about weekends?" into "What are Peak Performance
Gym's weekend hours?"
Multi-Agent Architectures
We know that an agent is a system that uses an LLM to decide the control flow of an
application.
As you develop these systems, they might grow more complex over time, making them
harder to manage and scale.
● Agent has too many tools at its disposal and makes poor decisions about which tool to
call next
● context grows too complex for a single agent to keep track of
● there is a need for multiple specialization areas in the system (e.g. planner, researcher,
math expert, etc.)
To tackle these, you might consider breaking your application into multiple smaller,
independent agents and composing them into a multi-agent system.
Multi-Agent Architectures
These independent agents can be as simple as a prompt and an LLM call, or as complex as a
ReAct agent
● Modularity: Separate agents make it easier to develop, test, and maintain agentic
systems.
● Specialization: You can create expert agents focused on specific domains, which helps
with the overall system performance.
● Control: You can explicitly control how agents communicate (as opposed to relying on
function calling).
Multi-Agent Architectures
Subgraphs
Subgraphs allow you to build complex systems with multiple components that are themselves
graphs. A common use case for using subgraphs is building multi-agent systems.
The main question when adding subgraphs is how the parent graph and subgraph
communicate, i.e. how they pass the state between each other during the graph execution.
● parent graph and subgraph share schema keys. In this case, you can add a node with
the compiled subgraph
● parent graph and subgraph have different schemas. In this case, you have to add a
node function that invokes the subgraph: this is useful when the parent graph and the
subgraph have different state schemas and you need to transform state before or after
calling the subgraph
Supervisor Multi-agent Architecture
In this architecture, we define agents as nodes and add a supervisor node (LLM) that
decides which agent nodes should be called next.
We use Command to route execution to the appropriate agent node based on supervisor's
decision.
Supervisor Multi-agent Architecture
Supervisor
User Prompt
If we're building a responsive app for the users, real-time updates are key to keeping them
engaged.
1. Workflow progress (e.g., get state updates after each graph node is executed).
.stream and .astream are sync and async methods for streaming back outputs from a graph
run.
There are several different modes you can specify when calling these methods (e.g.
`graph.stream(input, stream_mode="values")):
1. stream_mode = "values"
This streams the full value of the state after each step of the graph.
2. stream_mode = "updates"
This streams the updates to the state after each step of the graph
Streaming in LangGraph
In particular, with LLM calls it is common to stream the tokens as they are generated.
We can do this using the `.astream_events` method, which streams back events as they
happen inside nodes
Deployment: