-
I am building an agent using LangGraph, which should decide whether to use its internal knowledge or external tools to answer user queries. The agent should attempt to respond using its own knowledge first and then, if necessary, call external tools (such as an API) to gather real-time data. However, I am encountering an issue where the agent repeatedly switches between itself and the tools, resulting in a Here is an overview of the setup:
This is my code:
From the LangSmith trace, it appears that the agent keeps bouncing between the agent node and the tool node, which leads to hitting the recursion limit. Once the tool is executed, I expect the agent to format the response and return it to the user, but instead, it keeps retrying the agent-tool cycle. Expected Behavior: The agent should: 1.Try to respond using its knowledge base. Is there a better way to handle tool invocation in a manner that stops the agent from recursively switching between the tool and itself? Am I missing a termination condition or misplacing the logic for handling the tool response in the workflow? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi, the issue is you're forcing the model to always call a tool, with tool_choice=required. Thus the model will always return a tool call, and the stop condition should_continue will always return "tools" |
Beta Was this translation helpful? Give feedback.
Hi, the issue is you're forcing the model to always call a tool, with tool_choice=required. Thus the model will always return a tool call, and the stop condition should_continue will always return "tools"