From the course: LangGraph.js: Building Agents with Javascript
Unlock this course with a free trial
Join today to access over 25,500 courses taught by industry experts.
Handling tool outputs in agent flow
From the course: LangGraph.js: Building Agents with Javascript
Handling tool outputs in agent flow
When an LLM returns a tool call, your code needs to do 3 things to handle this type of response. First, it needs to execute the tool then it will package the tool results into a tool message. And finally, it will send this tool message back to the LLM along with the tool call ID. To understand this better, let us zoom in by looking at how this process looks in code. Step 1. Your code detects the tool to be invoked and the arguments to invoke it with. It then performs the invocation and gets the result. Step 2. The tool call result is then packaged into a ToolMessage object. This will receive the tool invocation result as content and also the tool call ID that was returned in the ToolMessage. And the final step, this ToolMessage will be sent back to the LLM to package the result into a final response. The tool call ID is very critical in this flow. This is because the LLM can recommend the same tool to be called more than once, but with different arguments. And the LLM must be able to…