Type something to search...
LangGraph : Tool calling Agents

LangGraph : Tool calling Agents

This article assumes reader have basic overview of LangGraph / Multi Agentic designs. With this in place, lets try to understand fundaments of implementing Tool calling Agents.

Folks who are into Generative AI would be aware of concepts like function calling / tool calling. The concept is pretty simple, to generate an inference base on users prompt a Model can take help of single or multiple tools. Tools are nothing but functions which acts as a catalyst in providing required information to Model for better inference. Further, a tool can be built-in tool / custom defined tool.

When I was searching for options to implement Tool calling for my multi agentic graph, I stumble upon multiple examples to implement the same using AgentExecutor / ToolNode / ReAct framework / create_tool_calling_agent, but non of them suggested how to implement using Custom Agents which is an integral part of any multi agentic workflow. If you want to design complex workflows, then we need to create Custom Agents (rather than using ReAct / create_tool_calling_agents). I hope this confluence helps people, who plans to implement the same.

Lets keep the graph simple to understand. There are three Agents

  1. Autonomous Agent which acts as a StartNode in the graph
  2. Output Agent which acts as an EndNode in the graph
  3. Toolbox Agent which acts as catalyst to provide required information to Autonomous Agent.

Autonomous Agent can respond to queries related to Weather and Stock Price. (Disclaimer : I understand, Agent should not be designed with such diverse task, but consider this as an hypothetical scenario, just for an example). Moving ahead, whenever a User prompt consist of task related to

  1. Weather, the Autonomous Agent would perform tool calling on get_weather function.
  2. Stock price, the Autonomous Agent would perform tool calling on get_stock_price function.
  3. Weather and Stock price (both), then Autonomous Agent would call both these tools in parallel using batch function.

Fundamental part of any Agentic Graph is graph design. Here we are having three Nodes defined, named as

  1. autonomous_agent : Job of this agent is to answer to users prompt about weather or stock price
  2. output : Job of this agent is to use the final inference generated by autonomous_agent and respond back to user, as-is. No logic / parsing.
  3. toolbox : Job of this agent is to understand, Model requires help from which tool / tools → Invoke that respective tool / tools → respond back to autonomous_agent with output generated by tool / tools.

The next import part to any graph are its Edges. It defines workflow, if you see carefully,

  1. Entry point is set as autonomous_agent
  2. From autonomous_agent we have a conditional edge. A conditional edge allows graph to have edges defined dynamically between two Nodes. Here, should_continue() decides which Node should be called next.
  3. toolbox is having a static Edge mapped with autonomous_agent. This means the once toolbox finishes its execution, the flow would go by default to autonomous_agent.
  4. Exit point is defined as output agent.

Following are the two tools which would help autonomous_agent in generating inference. It has a declarative definition which helps models in understanding what feature it implements and what arguments are required as input. For the sake of example, implementation are kept minimalistic.

Lets look into the flow of should_continue(). This code block decide which Node should be called next. If you look closely it return a string. Based on this, Graph decides after completion of autonomous_agent which Node should be invoked. I wont speak about state and its definition / schema as part of this article (in the interest of not diluting the original topic). But just remember, that

  1. If autonomous_agent is asking to invoke tool, then → call toolbox node. (which tool ?? we don’t need to bother here)
  2. If autonomous_agent has completed its inference and does not have value in tool_calls then → call output node.

Now, lets consider scenario where autonomous_agent wants help from tool / tools → in this case toolbox Node would be called. If you look at the Graph definition (image) shown above, toolbox node invokes following function. Lets dive deep and understand, what all activity it does.

  1. This function converts a static function _invoke_tool into a RunnableLambda (I will explain what this function does in a while).
  2. It calls batch method from Runnable interface and takes tool / tools information from tool_calls property as an input.
  3. The val which is received from tools are then iterated and set in a state property tool_response.
  4. This tool_response, is later used as placeholder in Prompt while invoking the Model

The RunnableLambda created above, invokes this function. It calls tools based on tool name. Let dive deep into its implementation :

  1. This information is received as an input parameter to this method from call_tools() when batch method is invoked.
  2. It can take single / multiple tools as input.
  3. As each tool is a Runnable we can call its invoke function to get output from respective tool.
  4. The value is then set into ToolMessage object and returned back.
  5. This information is set into state property of tool_response in call_tools()

Remember, we have defined Edges against each Node (refer image above). We have static edge mapped from toolbox to autonomous_agent. Hence, now this would invoke the autonomous_agent invoke(). The value which tools have generated is set into the prompt before Model is invoked for inference. This time you would notice response from Model will have

  1. tool_call property as empty.
  2. content property will have inference value.

Hope this article has helps in understanding the fundamental concept of Tool calling Agents. If it has helped you in anyways, feel free to share it ahead.

Just for reference, following is the output generated by code. All three scenarios mentioned above are covered in test scenario.

Github : https://github.com/shaktipawar/langgraph_tool_calling

Happy to help !!

Shakti.

https://www.linkedin.com/in/shakti-pawar/

Related Posts

10 Creative Ways to Use ChatGPT Search The Web Feature

10 Creative Ways to Use ChatGPT Search The Web Feature

For example, prompts and outputs Did you know you can use the “search the web” feature of ChatGPT for many tasks other than your basic web search? For those who don't know, ChatGPT’s new

Read More
📚 10 Must-Learn Skills to Stay Ahead in AI and Tech 🚀

📚 10 Must-Learn Skills to Stay Ahead in AI and Tech 🚀

In an industry as dynamic as AI and tech, staying ahead means constantly upgrading your skills. Whether you’re aiming to dive deep into AI model performance, master data analysis, or transform trad

Read More
10 Powerful Perplexity AI Prompts to Automate Your Marketing Tasks

10 Powerful Perplexity AI Prompts to Automate Your Marketing Tasks

In today’s fast-paced digital world, marketers are always looking for smarter ways to streamline their efforts. Imagine having a personal assistant who can create audience profiles, suggest mar

Read More
10+ Top ChatGPT Prompts for UI/UX Designers

10+ Top ChatGPT Prompts for UI/UX Designers

AI technologies, such as machine learning, natural language processing, and data analytics, are redefining traditional design methodologies. From automating repetitive tasks to enabling personal

Read More
100 AI Tools to Finish Months of Work in Minutes

100 AI Tools to Finish Months of Work in Minutes

The rapid advancements in artificial intelligence (AI) have transformed how businesses operate, allowing people to complete tasks that once took weeks or months in mere minutes. From content creat

Read More
17 Mindblowing GitHub Repositories You Never Knew Existed

17 Mindblowing GitHub Repositories You Never Knew Existed

Github Hidden Gems!! Repositories To Bookmark Right Away Learning to code is relatively easy, but mastering the art of writing better code is much tougher. GitHub serves as a treasur

Read More