How to Pass Config to the Tools
At runtime, we may need to pass values to the tools, such as user ID. For security reasons, this value should be set by application logic rather than controlled by the LLM. The LLM should only manage its expected parameters.
The LangChain tools use the Runnable interface, where the method invoke accepts runtime information through the parameter RunnableConfig.
In the following example, we will set up an agent that uses tools to manage the user’s favorite pets (adding, reading, and deleting entries), while fixing the user ID through application logic and allowing the chat model to control other parameters.
from typing import List
from langchain_core.tools import tool
from langchain_core.runnables.config import RunnableConfig
from langgraph.prebuilt import ToolNode
user_to_pets = {}
@tool
def update_favorite_pets( # Note: The config parameter does not need to be added to the docstring as we do not want it included in the function signature attached to the LLM
pets: List[str],
config: RunnableConfig,) -> None:
"""Add favorite pets list.
Args:
pets: The list of favorite pets to set.
"""
user_id = config.get("configurable", {}).get("user_id")
user_to_pets[user_id] = pets
@tool
def delete_favorite_pets(config: RunnableConfig) -> None:
"""Delete favorite pets list."""
user_id = config.get("configurable", {}).get("user_id")
if user_id in user_to_pets:
del user_to_pets[user_id]
@tool
def list_favorite_pets(config: RunnableConfig) -> None:
"""List favorite pets (if any)."""
user_id = config.get("configurable", {}).get("user_id")
return ", ".join(user_to_pets.get(user_id, []))
tools = [update_favorite_pets, delete_favorite_pets, list_favorite_pets]
tool_node = ToolNode(tools)
from langgraph.graph import StateGraph, MessagesState
from langchain_ollama import ChatOllama
import base_conf
model_with_tools = ChatOllama(base_url=base_conf.base_url,
model=base_conf.model_name,
temperature=0).bind_tools(tools)
As you can see, to pass the config into the tools, it is only necessary to use the RunnableConfig type as the input parameter for the tools. This input parameter will not be recognized by the LLM, but during the tool call, the tool node will pass the config.
The agent takes some queries as input and repeatedly calls the tools until it has enough information to resolve the query. This is a very simple process that has appeared many times in previous tutorials.
from typing import Literal
from langgraph.graph import StateGraph, MessagesState, START, END
def should_continue(state: MessagesState):
messages = state["messages"]
last_message = messages[-1]
if last_message.tool_calls:
return "tools"
return END
def call_model(state: MessagesState):
messages = state["messages"]
response = model_with_tools.invoke(messages)
return {"messages": [response]}
builder = StateGraph(MessagesState)
# Define the two nodes we will cycle between
builder.add_node("agent", call_model)
builder.add_node("tools", tool_node)
builder.add_edge(START, "agent")
builder.add_conditional_edges("agent", should_continue, ["tools", END])
builder.add_edge("tools", "agent")
graph = builder.compile()
Let’s take a look at the effect
from langchain_core.messages import HumanMessage
user_to_pets.clear() # Clear the state
print(f"User information before running: {user_to_pets}")
inputs = {"messages": [HumanMessage(content="My favorite pets are cats and dogs")]}
for chunk in graph.stream( inputs, {"configurable": {"user_id": "123"}}, stream_mode="values"):
chunk["messages"][-1].pretty_print()
print(f"User information after running: {user_to_pets}")
User information before running: {}
================================ Human Message =================================
My favorite pets are cats and dogs
================================== Ai Message ==================================
Tool Calls: update_favorite_pets (a15a67f9-ab80-48e2-842a-586d0c00a8f1) Call ID: a15a67f9-ab80-48e2-842a-586d0c00a8f1 Args: pets: ['猫', '狗']
================================= Tool Message =================================
Name: update_favorite_pets
null
================================== Ai Message ==================================
I have updated your favorite pets list; now your favorites are cats and dogs. Do you need any other help?
User information after running: {'123': ['猫', '狗']}
As you can see, the config information for {“user_id”: “123”} has been successfully retrieved!