LangGraph Agents - Controllability - Sub-graphs

LangChain · Intermediate ·🤖 AI Agents & Automation ·1y ago

Key Takeaways

LangGraph Agents utilize subgraphs for controllability, parallelization, and state management, integrating with LSmith for advanced applications and analysis. The video demonstrates how to define and manage subgraphs, inherit state, and process logs for failure analysis.

Full Transcript

hey this is Lance from Lang chain we've been doing a few videos about more advanced Lang graph Concepts and I want to talk today about subgraphs so subgraphs are a way to break up complex kind of graphs into smaller substeps and the key point is that each of those kind of substeps or subgraphs can have their own uh their own state so let's take an example of like let's say you had a multi-agent uh graph and each of your agents you want to have independent state you want to isolate that State per agent this is a great kind of example or use case for subgraphs so here's like a toy example let's say I had like a node that branched out into two different subgraphs and you can see each subgraph P basically inherits state from that parent node but it also has its own state so subgraph one has its own little hello world State the and two has fizzbuzz right that's the overall idea um this Falls within the general kind of theme of controllability we talked about parallelization we talked about map reduce subgraphs are kind of another mechanism of controllability when you're building graphs so let's kind of cover a toy example and I want to actually talk through kind of this information transfer um between the different components and in slightly more explicit terms so let's say I had this toy uh graph that processed logs okay these could be logs from some kind of observability system right and I have some entry graph now in this entry graph I grabb those logs and I save it to some State that's called docs okay and I have two subgraphs one that will summarize what's in those logs and the other that will provide some kind of failure analysis like if there's any mistakes in the logs it'll it'll summarize them right now what's nice is these subgraphs are basically nodes within my entry graph or in My overall what we might call the parent graph and they can inherit the state from the parent so this is a key point when I was kind of covering this initially confused me a little bit so basically I Define my parent graph which is this entry graph these summarization and failure node and failure analysis are subgraphs that are kind of defined as nodes within the parent and they can inherit state from the parent and so all I need to do is in each of these subgraphs in their kind of subgraph state I just Define docs which is automatically kind of inherited from the parent so that's really convenient so I can work with it and that's how I can transfer information from my kind of parent graph in this case the entry graph into each of my subgraph so each of those have access to docs they can do their own thing on them each of them have their kind of own independent like States in here summary failures right those are states that are kind of private to the subgraph and they're going to write out a summary report and a failure report now what's nice is all I need to do is simply specify these in my parent state that then they will be accessible in in my paragraph when I finish so basically uh you know when I kind of set this up up front I say okay here's the things I'm going to want to have access to from my subgraphs I make those a part of my kind of overall parent graph State and by the end once these subgraphs run it'll be present and populated so I can just access it so that's really it so when I finish I'm going to access the summary report and the failure report so that's really it now let's actually see this in code so I'll copy over this to a to a fresh notebook and what you're going to see is so this is just kind of we're defining the structure of our logs this is just you know a toy example here so let's imagine these are logs from some kind of question answer system they contain questions docs answers and grades and feedback right so here's my failure analysis subgraph let's just Define those first so it's going to have docs like we kind of showed before it'll have failures it'll have some failure analysis summary right and it has some nodes and this can be whatever I want get failures generate the sum this is just a placeholder of course right I My Graph Builder super simple it just basically takes in that state it adds the nodes it creates an entry point Fin and uh adds one Edge finishes summarization subgraph is similar to find my state again docs is inherited from the parent it has some kind of independent uh or kind of like private state within the subgraph and then it has this report okay so there we go we can just Define those two subgraphs and we have builders for each of those so that's really all we need done there now uh what we're going to do is we're just going to kind of copy over here so here's some like dummy logs I'm going to process now here's my parent or entry graph now you can see this docs let's go back to our kind of diagram here this docs is what I'm going to pass through to the subgraph so docs is basically defined in My overall graph State and you can see it was also specified in the subgraph states here and right here so this Doc is what I'm going to transfer through to the subgraphs that's cool now we're going to see here and I comment it right right here failure and failure fa summary and Report are going to be populated or generated by our subgraphs and they're going to be basically it will be present or available to us in this overall parent graph State once the subg graph's run so basically all I'm doing here is I'm basically adding placeholder State variables for what I want to get back from my subgraphs that's really it and we can go back and look so failure failure analysis summary is defined um right up here in my failure analysis subgraph and then the report is defined in my summarization subgraph so that's it and this is just some other little things now let's actually just kind of run this and we can see it so this is kind of cool I when I see the graph laid out I can see everything so here's kind of my parent I'm converting the logs to docs and you know this could be some operation where you go from raw locks it's like kind of structured uh kind of structured logs person some schema you then route those to each of your subgraphs those subgraphs run and then you end and when you end in your overall kind of parent State you're going to have access to the failure analysis summary and the report that's exactly what you want uh and so we can go all the way down and we can just go ahead and run this and I'll just you know send in some dummy logs and here we go so you can see we basically kind of show the state as it run and what we can see here is um our failure analysis summary and the report are returned when we invoke the graph because they're present in our overall parent state right here so each subgraphs ran produced the failure analysis summary in the report which is present to us um in the the final uh state of the parent once everything runs and so that's really it subgraphs are really convenient often you're building a more complex systems that you want to kind of encapsulate different subprocesses with its own with their own State um this is obviously really useful and we see many many different teams do this uh when building kind of larger agentic systems and this is just a toy example showing you how everything fits together so I do want to interject here and just show quickly that Lang graph actually works really nicely with lsmith and for applications like this when I have different subgraphs it's actually really nice to be able to go to Lang Smith and for example to dig into what's happening with any subgraph so I've logged to my default project rlm within Langs Smith here's the trace we've been working on and so you can see convert logs to docs that's kind of our starting point if you look at our diagram so there we go and here are the two subgraphs so faal analysis question summarization so basically if I click on this convert logs to docs I can see the input and the output from this particular step so that's great and what I can do is I can basically open up the subgraph and I can see the input and output of the overall sub graph which is great and I can actually open that up I can look at the individual steps so get failures generate summary likewise with question summarization I can look at the individual steps um I can dig into each step I can look at the inputs and outputs and of course in the real in the kind of a realistic case these steps could involve kind of more involved calls which you may want to debug um of course I get useful information about kind of token usage in this case Zero tokens for this particular part of the graph uh zero tokens overall I don't make llm calls here but latency uh as well as token usage will be provided for you so again Lang Smith is very useful for auditing the graphs that you're running particular if you have a larger overall graph that incorporates multiple subgraphs

Original Description

For more complex systems, sub-graphs are a useful design principle. Sub-graphs allow you to create and manage different states in different parts of your graph. This allows you build things like multi-agent teams, where each team can track its own separate state. Documentation: https://langchain-ai.github.io/langgraph/how-tos/subgraph/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from LangChain · LangChain · 0 of 60

← Previous Next →
1 Chat With Your Documents Using LangChain + JavaScript
Chat With Your Documents Using LangChain + JavaScript
LangChain
2 LangChain SQL Webinar
LangChain SQL Webinar
LangChain
3 LangChain "OpenAI functions" Webinar
LangChain "OpenAI functions" Webinar
LangChain
4 LangSmith Launch
LangSmith Launch
LangChain
5 LangChain x Pinecone: Supercharging Llama-2 with RAG
LangChain x Pinecone: Supercharging Llama-2 with RAG
LangChain
6 LangChain Expression Language
LangChain Expression Language
LangChain
7 Building LLM applications with LangChain with Lance
Building LLM applications with LangChain with Lance
LangChain
8 Benchmarking Question/Answering Over CSV Data
Benchmarking Question/Answering Over CSV Data
LangChain
9 LangChain "RAG Evaluation" Webinar
LangChain "RAG Evaluation" Webinar
LangChain
10 Fine-tuning in Your Voice Webinar
Fine-tuning in Your Voice Webinar
LangChain
11 Tabular Data Retrieval
Tabular Data Retrieval
LangChain
12 Building an LLM Application with Audio by AssemblyAI
Building an LLM Application with Audio by AssemblyAI
LangChain
13 Superagent Deepdive Webinar
Superagent Deepdive Webinar
LangChain
14 Lessons from Deploying LLMs with LangSmith
Lessons from Deploying LLMs with LangSmith
LangChain
15 Shortwave Assistant Deepdive Webinar
Shortwave Assistant Deepdive Webinar
LangChain
16 Cognitive Architectures for Language Agents
Cognitive Architectures for Language Agents
LangChain
17 Effectively Building with LLMs in the Browser with Jacob
Effectively Building with LLMs in the Browser with Jacob
LangChain
18 Data Privacy for LLMs
Data Privacy for LLMs
LangChain
19 "Theory of Mind" Webinar with Plastic Labs
"Theory of Mind" Webinar with Plastic Labs
LangChain
20 LangChain Templates
LangChain Templates
LangChain
21 Using Natural Language to Query Postgres with Jacob
Using Natural Language to Query Postgres with Jacob
LangChain
22 Building a Research Assistant from Scratch
Building a Research Assistant from Scratch
LangChain
23 Benchmarking RAG over LangChain Docs
Benchmarking RAG over LangChain Docs
LangChain
24 Skeleton-of-Thought: Building a New Template from Scratch
Skeleton-of-Thought: Building a New Template from Scratch
LangChain
25 Benchmarking Methods for Semi-Structured RAG
Benchmarking Methods for Semi-Structured RAG
LangChain
26 LangSmith Highlights: Getting Started
LangSmith Highlights: Getting Started
LangChain
27 LangSmith Highlights: Debugging
LangSmith Highlights: Debugging
LangChain
28 LangSmith Highlights: Datasets
LangSmith Highlights: Datasets
LangChain
29 LangSmith Highlights: Evaluation
LangSmith Highlights: Evaluation
LangChain
30 LangSmith Highlights: Human Annotation
LangSmith Highlights: Human Annotation
LangChain
31 LangSmith Highlights: Monitoring
LangSmith Highlights: Monitoring
LangChain
32 LangSmith Highlights: Hub
LangSmith Highlights: Hub
LangChain
33 SQL Research Assistant
SQL Research Assistant
LangChain
34 Getting Started with Multi-Modal LLMs
Getting Started with Multi-Modal LLMs
LangChain
35 Build a Full Stack RAG App With TypeScript
Build a Full Stack RAG App With TypeScript
LangChain
36 Auto-Prompt Builder (with Hosted LangServe)
Auto-Prompt Builder (with Hosted LangServe)
LangChain
37 LangChain v0.1.0 Launch: Introduction
LangChain v0.1.0 Launch: Introduction
LangChain
38 LangChain v0.1.0 Launch: Observability
LangChain v0.1.0 Launch: Observability
LangChain
39 LangChain v0.1.0 Launch: Integrations
LangChain v0.1.0 Launch: Integrations
LangChain
40 LangChain v0.1.0 Launch: Composability
LangChain v0.1.0 Launch: Composability
LangChain
41 LangChain v0.1.0 Launch: Streaming
LangChain v0.1.0 Launch: Streaming
LangChain
42 LangChain v0.1.0 Launch: Output Parsing
LangChain v0.1.0 Launch: Output Parsing
LangChain
43 LangChain v0.1.0 Launch: Retrieval
LangChain v0.1.0 Launch: Retrieval
LangChain
44 LangChain v0.1.0 Launch: Agents
LangChain v0.1.0 Launch: Agents
LangChain
45 Build and Deploy a RAG app with Pinecone Serverless
Build and Deploy a RAG app with Pinecone Serverless
LangChain
46 Hosted LangServe + LangChain Templates
Hosted LangServe + LangChain Templates
LangChain
47 LangGraph: Intro
LangGraph: Intro
LangChain
48 LangGraph: Agent Executor
LangGraph: Agent Executor
LangChain
49 LangGraph: Chat Agent Executor
LangGraph: Chat Agent Executor
LangChain
50 LangGraph: Human-in-the-Loop
LangGraph: Human-in-the-Loop
LangChain
51 LangGraph: Dynamically Returning a Tool Output Directly
LangGraph: Dynamically Returning a Tool Output Directly
LangChain
52 LangGraph: Respond in a Specific Format
LangGraph: Respond in a Specific Format
LangChain
53 LangGraph: Managing Agent Steps
LangGraph: Managing Agent Steps
LangChain
54 LangGraph: Force-Calling a Tool
LangGraph: Force-Calling a Tool
LangChain
55 LangGraph: Multi-Agent Workflows
LangGraph: Multi-Agent Workflows
LangChain
56 Streaming Events: Introducing a new `stream_events` method
Streaming Events: Introducing a new `stream_events` method
LangChain
57 Building a web RAG chatbot: using LangChain, Exa (prev. Metaphor), LangSmith, and Hosted Langserve
Building a web RAG chatbot: using LangChain, Exa (prev. Metaphor), LangSmith, and Hosted Langserve
LangChain
58 OpenGPTs
OpenGPTs
LangChain
59 Open Source RAG with Nomic's New Embedding Model (and ChromaDB and Ollama)
Open Source RAG with Nomic's New Embedding Model (and ChromaDB and Ollama)
LangChain
60 LangGraph: Persistence
LangGraph: Persistence
LangChain

This video teaches how to use LangGraph Agents with subgraphs for controllability, parallelization, and state management, and how to integrate with LSmith for advanced analysis and logging. By following the steps, viewers can build complex agent systems and manage state effectively. The key insight is that subgraphs can inherit state from parent graphs and have their own state, enabling flexible and scalable system design.

Key Takeaways
  1. Define a parent graph with subgraphs
  2. Specify the state to be inherited by subgraphs
  3. Run subgraphs and access their output
  4. Define sub-graphs
  5. Create builders for each sub-graph
  6. Copy over dummy logs to process
  7. Run the graph to see the state and return values
💡 Subgraphs can inherit state from parent graphs and have their own state, enabling flexible and scalable system design.

Related Reads

📰
AI Agents and Open-Source Bounties: Revolutionizing Software Development Through Collaboration
Learn how AI agents and open-source bounties are transforming software development through collaborative efforts, and discover ways to apply these concepts to your own projects
Dev.to AI
📰
Gate Agent Evals by Severity, Not a Flat Pass-Rate
Learn to gate agent evals by severity for more nuanced deploy decisions, rather than relying on a flat pass-rate
Dev.to AI
📰
Beyond the chat window: Designing trustworthy AI assistants for public services
Design trustworthy AI assistants for public services by considering user experience and transparency
Medium · AI
📰
What Is Vibe Video? The New Way to Create Films by Talking to AI
Learn how to create films by talking to AI using Vibe Video and Claude, a capable AI agent
Medium · AI
Up next
AI Agents Are Starting to Talk to Each Other... Without Us.
PlivoAI
Watch →