AI / Agentic AI Interview questions
What is LangGraph and when to use it?
LangGraph is a framework for building stateful, multi-step agent applications using language models. Developed by LangChain, it provides a graph-based approach to orchestrating complex agent workflows where different components (nodes) perform specific tasks and edges define transitions between them. Unlike simple sequential chains, LangGraph enables cyclic flows, conditional branching, and persistent state management—essential for sophisticated agentic applications.
The core abstraction in LangGraph is the StateGraph, which represents application logic as nodes (processing steps) connected by edges (transitions). State flows through the graph, being modified by each node. This architecture naturally expresses agent loops where the agent reasons, acts, observes results, and repeats until goals are achieved. LangGraph's state persistence allows agents to maintain context across multiple invocations, enabling long-running tasks that span multiple user interactions or system restarts.
LangGraph excels in scenarios requiring: Complex control flow with conditional logic (if the agent needs information, query a database; if sufficient data exists, proceed to analysis), human-in-the-loop patterns where workflows pause for human input or approval, multi-agent orchestration coordinating specialized agents, error handling and retries with sophisticated recovery strategies, and streaming execution where partial results are delivered as they're generated.
from langgraph.graph import StateGraph, END
from typing import TypedDict, Annotated
import operator
# Define state structure
class AgentState(TypedDict):
messages: Annotated[list, operator.add]
next_action: str
iteration: int
# Define nodes (processing steps)
def plan_step(state: AgentState):
messages = state["messages"]
# LLM determines what to do next
plan = llm.invoke(f"Based on {messages}, what should we do next?")
return {"next_action": plan, "iteration": state["iteration"] + 1}
def execute_step(state: AgentState):
action = state["next_action"]
# Execute the planned action
result = execute_action(action)
return {"messages": [result]}
def should_continue(state: AgentState):
# Conditional logic: continue or end
if "complete" in state["next_action"].lower():
return "end"
elif state["iteration"] > 10:
return "end"
else:
return "continue"
# Build the graph
workflow = StateGraph(AgentState)
workflow.add_node("planner", plan_step)
workflow.add_node("executor", execute_step)
# Define edges
workflow.set_entry_point("planner")
workflow.add_edge("executor", "planner") # Cycle back
workflow.add_conditional_edges(
"planner",
should_continue,
{
"continue": "executor",
"end": END
}
)
# Compile to runnable
app = workflow.compile()
# Run the agent
result = app.invoke({"messages": ["Analyze sales data"], "iteration": 0})
When to use LangGraph: Choose LangGraph when building agents that need persistent state across turns, complex conditional workflows beyond simple chains, human oversight at specific points, coordination between multiple specialized agents, or sophisticated error recovery. It's particularly valuable for production systems where reliability and observability matter. However, for simple sequential tasks or single-turn question-answering, simpler frameworks or direct LLM API calls may suffice. LangGraph's learning curve is steeper than basic chains, but the investment pays off for complex agentic applications requiring robust control flow and state management.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
