Tool-Calling Agents and Prebuilt Components
A tool-calling agent in LangGraph is a model node, a tool node and a conditional edge in a cycle. Know how `ToolNode` and `tools_condition` build that loop, how tools reach state through `ToolRuntime` or update it with `Command`, how `create_agent` replaced the deprecated `create_react_agent`, and when a hand-written graph beats the prebuilt agent.
Key points
- 1
The loop as a graph:
START -> model,add_conditional_edges("model", tools_condition),add_edge("tools", "model").tools_conditionchecks only the last message and returns"tools"if it is anAIMessagewith tool calls, otherwise"__end__". Name theToolNode"tools"or pass a path map; otherwisecompile()raises aValueErrorabout an unknown target. - 2
ToolNode(tools)runs every tool call in the lastAIMessagein parallel within one node execution and returns oneToolMessageper call, in call order, each with the matchingtool_call_id.messages_keychanges the state key it reads and writes;tools_condition(state, messages_key=...)has the same option. - 3
handle_tool_errorsdefaults to a handler that turns onlyToolInvocationError(arguments that fail the schema) into an errorToolMessageand re-raises everything else.Trueor a string catches everything, a tuple of types or a type-annotated callable catches only those types, andFalselets everything raise. - 4
runtime: ToolRuntimeis injected byToolNodeand hidden from the model's schema. It carriesstate,context,store,stream_writer,config,tool_call_idandtools. The docs treatInjectedState,InjectedStoreandInjectedToolCallIdas older patterns that still work. - 5
Tools see only the state passed to the
ToolNode. As a graph node it gets the full state; if you calltool_node.invoke(...)yourself, pass the whole state, not just{"messages": ...}. - 6
A tool may return
Command(update=...)to change state. When it targets the current graph, the update must include aToolMessagewithtool_call_id=runtime.tool_call_id, orToolNoderaises aValueError. Keys written by parallel tool calls need a reducer. - 7
create_react_agentis deprecated since LangGraph 1.0 (LangGraphDeprecatedSinceV10, to be removed in 2.0). Migrating tolangchain.agents.create_agentmeans:promptbecomessystem_prompt, hooks become middleware, state must be aTypedDictsubclass ofAgentState, no pre-bound models, noToolNodeastools, and the streamed node name changes fromagenttomodel. - 8
create_agentreturns a compiled LangGraph graph (nodesmodelandtools, plus middleware nodes). It takes a checkpointer,context_schema,state_schema, middleware (before_model,after_model,wrap_model_call,wrap_tool_call) andresponse_formatfor structured output (ToolStrategyorProviderStrategy). - 9
return_direct=Trueshort-circuits only insidecreate_agent, and only when every tool in the batch has it. In a hand-written graph your edges decide, so the flag does nothing unless you route on it. - 10
Choose
create_agentwhen the flow is a model-and-tools loop and the extra needs are cross-cutting (memory, approval, summarisation, typed output). Hand-write aStateGraphfor required deterministic steps, custom loops (grade and rewrite) or parallel branches. Acreate_agentgraph can be a node inside a custom graph. - 11
Agentic RAG in the tutorial:
generate_query_or_respond(model bound to a retriever tool),retrieve(ToolNode), agrade_documentsconditional edge using structured output, thengenerate_answerorrewrite_question, which loops back. Add a retry counter yourself; the tutorial's loop is unbounded. - 12
MCP in langchain 1.4 or later:
langchain.mcp.MCPAdapter(beta,pip install "langchain[mcp]") replaceslangchain-mcp-adapters. Useasync with MCPAdapter(target) as a: tools = await a.list_tools(). ServerisErrorresults become errorToolMessages, transport failures raise, and elicitation pauses the run with an interrupt, so it needs a checkpointer.
Common traps
Putting
tools_conditionon the tools node, or appending an auditAIMessageafter the model: the router sees a message without tool calls and ends the run, leaving the tool calls unanswered.Assuming default
ToolNodeerror handling sends every tool exception to the model: runtime exceptions raise and fail the whole node, including the other parallel calls.Setting
handle_tool_errors=Trueeverywhere: real bugs become error messages that the model retries forever, burning tokens instead of alerting you.Attaching a
RetryPolicyto aToolNode: the whole batch re-runs, repeating side effects of tool calls that already succeeded.A
wrap_tool_callhandler that returns a plain string: it is not aToolMessage(langchain 1.4.7 stores it as aHumanMessage), so the tool call is left without a result.Saying
create_react_agentwas removed in 1.0: it still imports, with a deprecation warning, and is scheduled for removal in 2.0.Adding
create_agentfeatures by hand-writing a graph when middleware,context_schemaorresponse_formatalready cover them, or relying on a prompt to enforce a step that must always run.
Read the source
Further reading
Suggest a resourceNo extra reading for Tool-Calling Agents and Prebuilt Components yet. Found a guide, video or cheat sheet that made it click? Share it and help the next learner.
Test yourself on Tool-Calling Agents and Prebuilt Components
Ten questions, with the answer and explanation after each one.