LangGraph interview question

Agentic AILangGraphAI Agentseasy

What control flow does this LangGraph graph implement?

python
from typing import Annotated, TypedDict
from langgraph.graph import StateGraph, START, END
from langgraph.graph.message import add_messages
from langgraph.prebuilt import ToolNode

class State(TypedDict):
    messages: Annotated[list, add_messages]

def call_model(state: State) -> dict:
    return {"messages": [llm_with_tools.invoke(state["messages"])]}

def should_continue(state: State) -> str:
    last = state["messages"][-1]
    return "tools" if last.tool_calls else END

builder = StateGraph(State)
builder.add_node("agent", call_model)
builder.add_node("tools", ToolNode(tools))
builder.add_edge(START, "agent")
builder.add_conditional_edges("agent", should_continue, ["tools", END])
builder.add_edge("tools", "agent")
graph = builder.compile()
  1. AA cycle: agent runs, and if its last message has tool calls the tools node runs them and returns to agent; this repeats until the model replies without tool calls, then END.
  2. BA single pass: agent runs once, then tools runs once, then the graph ends, because edges must point forward and the tools → agent edge is dropped when the graph compiles.
  3. CTwo parallel branches: agent and tools both start from START in the first step, and should_continue merges their outputs into the message list before moving on to END.
  4. DA cycle that never ends by itself: the tools → agent edge always fires after agent, so the run stops only when it hits LangGraph's recursion limit and raises an error.
Show the answer and explanation

Correct answer: A, A cycle: agent runs, and if its last message has tool calls the tools node runs them and returns to agent; this repeats until the model replies without tool calls, then END.

Explanation LangGraph

This is the canonical tool-calling agent written as a LangGraph StateGraph:

  • State: a messages list with the add_messages reducer, so messages returned by a node are appended (or merged by id) instead of overwriting the list.
  • Nodes: agent calls the model; tools (the prebuilt ToolNode) executes every tool call in the last AI message and returns tool messages.
  • Edges: START → agent; a conditional edge from agent uses should_continue to choose tools or END; a normal edge tools → agent closes the loop.

The run is therefore agent → tools → agent → tools → … → agent → END, stopping when the model answers without tool calls. This cycle is the main thing LangGraph adds over DAG-style chains such as LCEL pipelines, where data flows one way and there is no native loop back.

Why the other readings are wrong:

  • Edges may point to earlier nodes; cycles are a core feature, not something the compiler drops.
  • Only agent is connected to START; tools runs only when the conditional edge selects it.
  • The tools → agent edge fires after tools, not after agent. The exit lives in the conditional edge.

The recursion limit (configurable per run) is still a useful safety net: if the model keeps requesting tools, LangGraph raises an error once the limit is hit, which you should catch and handle gracefully.

Interview tip: be ready to extend this graph: a human-approval node before tools for risky calls, a checkpointer for persistence, or a summarisation node when messages grows too long. LangGraph's prebuilt agent helpers build essentially this graph for you.

Practice it as a quiz

Answer this and the other free questions with instant grading, streaks and weak-topic tracking, or unlock all 4,000 questions.

More Agentic AI questions

  1. A retail client wants to benchmark a customer-service agent that must follow a written returns policy, call order-management APIs and handle multi-turn…
  2. You are building an MCP server for a retailer's merchandising team. (1) summarize_reviews needs an LLM to condense 500 reviews, but the server must not hold…
  3. A telecom network-diagnosis orchestrator fans out to 8 sub-agents in parallel (one per system: radio network, core, billing, …) and waits for all of them…
  4. A bank's document pipeline in Google's Agent Development Kit (ADK) must always run extract → validate → summarise, in that order. It is built as an LlmAgent…
  5. A bank moves its claims-triage planner from a general chat model to a reasoning model. The old prompt says "Think step by step", lists a fixed 10-step script…