AI / LangGraph LangChain Interview questions
What are subgraphs in LangGraph?
A subgraph in LangGraph is a compiled graph that is used as a node inside a parent graph. Subgraphs allow you to encapsulate complex, reusable agent logic and compose multiple graphs hierarchically — exactly like functions in programming, where a subgraph is the 'function' and the parent graph is the 'caller'.
from langgraph.graph import StateGraph, START, END # --- Define the subgraph --- class SubgraphState(TypedDict): messages: Annotated[list, operator.add] search_results: list sub_builder = StateGraph(SubgraphState) sub_builder.add_node("search", search_node) sub_builder.add_node("summarise", summarise_node) sub_builder.add_edge(START, "search") sub_builder.add_edge("search", "summarise") sub_builder.add_edge("summarise", END) research_subgraph = sub_builder.compile() # --- Use it as a node in the parent graph --- class ParentState(TypedDict): messages: Annotated[list, operator.add] parent_builder = StateGraph(ParentState) parent_builder.add_node("research", research_subgraph) # subgraph as node parent_builder.add_node("answer", answer_node) parent_builder.add_edge(START, "research") parent_builder.add_edge("research", "answer") parent_builder.add_edge("answer", END) graph = parent_builder.compile()
State key overlap between parent and subgraph determines how data flows between them. Keys present in both states are automatically mapped. Subgraphs can have their own checkpointers for independent persistence, or inherit the parent's checkpointer.
More Related questions...