AI / LangGraph LangChain Interview questions
What is StateGraph in LangGraph?
StateGraph is the main graph class in LangGraph. You instantiate it with a state type (a TypedDict class), add nodes and edges to it, then compile it into an executable app. The state type defines all the fields that are shared across nodes and how those fields are updated when a node returns a partial update.
from typing import TypedDict, Annotated from langgraph.graph import StateGraph, START, END import operator # Define the shared state structure class AgentState(TypedDict): messages: Annotated[list, operator.add] # append-reducer steps_taken: int # Build the graph graph_builder = StateGraph(AgentState) def call_llm(state: AgentState) -> dict: response = llm.invoke(state["messages"]) return {"messages": [response], "steps_taken": state["steps_taken"] + 1} graph_builder.add_node("llm", call_llm) graph_builder.add_edge(START, "llm") graph_builder.add_edge("llm", END) # Compile to executable graph = graph_builder.compile()
State updates use reducers. The default reducer is last-write-wins (the node's returned value replaces the current value). Using Annotated[list, operator.add] means returned lists are appended to the existing list — the standard pattern for message history in chat agents.
More Related questions...