Video summary

How to build Subgraphs in LangGraph

Main summary

Key takeaways

Educational

Building Subgraphs in LangGraph

Subgraphs help organize complex LangGraph workflows, especially multi-agent systems. A subgraph is a graph embedded in and executed within a larger parent graph—in effect, a graph used as a component of another graph.

Why use subgraphs?

As GenAI applications grow, workflows may include tools, retrieval, conditional routing, retries, memory, human-in-the-loop steps, evaluation, and guardrails. Keeping everything in one graph can make it difficult to build and maintain.

For example, a software-development agent’s work could be divided among planning, front-end and back-end coding, testing, code review, and DevOps agents. Each agent can be represented by a subgraph with its own internal logic and potentially its own tools, memory, and evaluation.

Benefits of using subgraphs include:

  • Modularity: Break a large workflow into smaller, focused parts.
  • Reusability: Reuse components, such as a coding agent, across different teams or tasks.
  • Maintainability: Isolate parts of the workflow to make debugging and updates easier.
  • Failure isolation: A problem in one subgraph need not bring down the entire workflow.
  • State separation: Let subgraphs maintain their own state and reduce unwanted interactions between components.
  • Observability: Trace individual subgraphs to inspect performance, including token usage and latency.

Two ways to add subgraphs in LangGraph

The key difference is how the parent graph and subgraph communicate and share state.

1. Invoke a subgraph inside a parent node

The parent graph and subgraph are built independently. A node in the parent graph calls the subgraph, passes it the required input, and uses the relevant part of its returned state.

Example: Translate an answer into Hindi

  1. Define a subgraph state with fields for the English input text and the Hindi translation.
  2. Create a translation node that asks an LLM to translate the text naturally and clearly without adding content.
  3. Connect the subgraph’s start, translation, and end steps, then compile it.
  4. Define a separate parent state containing the user’s question, the English answer, and the Hindi answer.
  5. In the parent graph, create a generation node that produces and stores an English answer.
  6. Create a translation node that invokes the subgraph with the English answer.
  7. Extract the Hindi result from the subgraph’s returned state and store it in the parent state.
  8. Compile and invoke the parent graph. Its final state contains the question and both answer versions.

In this approach, the parent and subgraph have separate states.

2. Add the subgraph directly as a node

Build the subgraph and add it directly to the parent graph as a node. In the approach shown, the subgraph uses the same state keys as the parent.

For the same translation example:

  1. Define one shared state containing the question, English answer, and Hindi answer.
  2. Build the translation subgraph so it reads the English answer and writes the Hindi translation to the shared state.
  3. Build the parent graph with a node that generates the English answer.
  4. Add the compiled subgraph directly as the next node, rather than creating a parent node that invokes it.
  5. Connect the steps, compile the parent graph, and invoke it to get the English and Hindi answers.

Further LangGraph features

The speaker recommends consulting the official LangGraph documentation for additional examples, including persistence, streaming subgraph output, and viewing subgraph state. The video also notes that a parent graph’s checkpointer can checkpoint its subgraphs.

Speakers and sources

  • Nitesh — the video’s narrator and instructor.
  • LangGraph documentation — referenced for subgraph patterns and additional examples.

Rate this summary

Your feedback will help improve summaries.

Improve this summary

Reprocess with a stronger model when the summary feels incomplete or inaccurate.

Pro

Translate summary in another language

Pro

Ask questions to this video

Chat for follow-up questions, clarifications, and source-backed answers.

Coming soon

Share this summary

Original video