AI Hints
Home›LangGraph Tutorial›Tools in Graphs
LangGraph · Lesson 33 of 120

tools_condition

Tool or finish.

⏱ 2 min read▶ Video at 34:42🎓 Free
Watch this lesson in the LangGraph courseWatch on YouTube ↗Full LangGraph course

After the model replies, there are two possibilities. It asked for tools, so run them. Or it gave a final answer, so finish. Tools condition is a ready made routing function that checks this.

tools_condition
From the video: tools_condition
  • Two possibilities
  • Tools, or finish
  • tools_condition decides

Testing the router

Tools condition looks at the last message. We try it on two states. One where the AI asked for a tool, and one where it simply answered. Run it. The first routes to tools, and the second routes to the end.

tools_condition.pypython
from langchain.messages import AIMessage
from langgraph.prebuilt import tools_condition

wants_tool = AIMessage("", tool_calls=[{"name": "check_stock", "args": {"title": "Dune"}, "id": "1"}])
final_answer = AIMessage("Yes, Dune is in stock.")

print(tools_condition({"messages": [wants_tool]}))
print(tools_condition({"messages": [final_answer]}))
Output
tools
__end__

The agent loop

Now we have every piece of an agent. The model node replies. Tools condition checks for tool calls. If there are some, the tool node runs them, and the results go back to the model. If not, the run ends.

  1. Model
  2. tools_condition
  3. ToolNode
  4. Back to model
  5. END
The agent loop
From the video: The agent loop

Summary

Tools condition routes to the tools when the model asks for them, and to the end otherwise. Next, wiring the full agent loop by hand.

Key takeaways
  • Checks the last message
  • 'tools' or END
  • Ready-made router
Next up: A ReAct Agent — the loop by hand