invoke
method:
type="tool_call"
, it will return a ToolMessage:
model.bind_tools()
to register the tools with the model.
Extended example: attach tools to a chat model
ToolNode
or implement your own custom node.
ToolNode
is a specialized node for executing tools in a workflow. It provides the following features:
handle_tool_errors=True
, enabled by default). See handling tool errors for more details.ToolNode
operates on MessagesState
:
MessagesState
, where the last message is an AIMessage
containing the tool_calls
parameter.MessagesState
updated with the resulting ToolMessage
from executed tools.Single tool call
Multiple tool calls
ToolNode
will execute both tools in parallelUse with a chat model
.bind_tools()
to attach the tool schema to the chat modelUse in a tool-calling agent
ToolNode
. You can also use LangGraph’s prebuilt agent.@tool
decorator.
args_schema
:
Type | Usage Scenario | Mutable | Lifetime |
---|---|---|---|
Configuration | Static, immutable runtime data | ❌ | Single invocation |
Short-term memory | Dynamic, changing data during invocation | ✅ | Single invocation |
Long-term memory | Persistent, cross-session data | ✅ | Across multiple sessions |
RunnableConfig
at invocation and access them in the tool:
Extended example: Access config in tools
InjectedState
:
Command
to update user_name
and append a confirmation message:
Command
and update graph state, you can either use prebuilt create_react_agent
/ ToolNode
components, or implement your own tool-executing node that collects Command
objects returned by the tools and returns a list of them, e.g.:Access long-term memory
InMemoryStore
is a store that stores data in memory. In a production setting, you would typically use a database or other persistent storage. Please review the store documentation for more options. If you’re deploying with LangGraph Platform, the platform will provide a production-ready store for you.
put
method. Please see the BaseStore.put API reference for more details.
users
namespace to group user data.
get_store
function is used to access the store. You can call it from anywhere in your code, including tools and prompts. This function returns the store that was passed to the agent when it was created.
get
method is used to retrieve data from the store. The first argument is the namespace, and the second argument is the key. This will return a StoreValue
object, which contains the value and metadata about the value.
store
is passed to the agent. This enables the agent to access the store when running tools. You can also use the get_store
function to access the store from anywhere in your code.
Update long-term memory
InMemoryStore
is a store that stores data in memory. In a production setting, you would typically use a database or other persistent storage. Please review the store documentation for more options. If you’re deploying with LangGraph Platform, the platform will provide a production-ready store for you.
UserInfo
class is a TypedDict
that defines the structure of the user information. The LLM will use this to format the response according to the schema.
save_user_info
function is a tool that allows an agent to update user information. This could be useful for a chat application where the user wants to update their profile information.
get_store
function is used to access the store. You can call it from anywhere in your code, including tools and prompts. This function returns the store that was passed to the agent when it was created.
put
method is used to store data in the store. The first argument is the namespace, and the second argument is the key. This will store the user information in the store.
user_id
is passed in the config. This is used to identify the user whose information is being updated.
return_direct=True
to immediately return a tool’s result without executing additional logic.
This is useful for tools that should not trigger further processing or tool calls, allowing you to return results directly to the user.
Extended example: Using return_direct in a prebuilt agent
create_react_agent
or ToolNode
, you will also
need to implement the control flow to handle return_direct=True
.tool_choice
parameter in the bind_tools method.
Force specific tool usage via tool_choice:
Extended example: Force tool usage in an agent
tool_choice
option in model.bind_tools()
:return_direct=True
to end the loop after execution.
recursion_limit
to restrict the number of execution steps.
tool_choice
parameter is used to configure which tool should be used by the model when it decides to call a tool. This is useful when you want to ensure that a specific tool is always called for a particular task or when you want to override the model’s default behavior of choosing a tool based on its internal logic.Note that not all models support this feature, and the exact configuration may vary depending on the model you are using.parallel_tool_calls=False
via the model.bind_tools()
method:
Extended example: disable parallel tool calls in a prebuilt agent
ToolNode
catches exceptions raised during tool execution and returns them as ToolMessage
objects with a status indicating an error.
create_react_agent
) leverages ToolNode
:
ToolNode
:
langgraph-bigtool
prebuilt library for a ready-to-use implementation.
tools
parameter of create_react_agent
. For example, to use the web_search_preview
tool from OpenAI:
tools
parameter shown in the examples above.