checkpointer
when creating
an agent.
create_react_agent()
manages short-term memory as a part of your agent’s state.
By storing these in the graph’s state, the agent can access the full context for a given conversation while maintaining separation between different threads.State is persisted to a database (or memory) using a checkpointer so the thread can be resumed at any time.
Short-term memory updates when the agent is invoked or a step (like a tool call) is completed, and the state is read at the start of each step.AgentState
to manage short term memory, specifically the conversation history via a messages
key.
Users can subclass AgentState
to add additional fields to the state.
This custom state can then be accessed via tools and dynamic prompt / model functions.
strategy
(e.g., keep the last maxTokens
) to use for handling the boundary.
To trim message history in an agent, use pre_model_hook
with the trim_messages
function:
RemoveMessage
.
For RemoveMessage
to work, you need to use a state key with add_messages
reducer,.
The default AgentState
provides this.
To remove specific messages:
user
messageassistant
messages with tool calls to be followed by corresponding tool
result messages.pre_model_hook
with a prebuilt SummarizationNode
abstraction:
InjectedState
annotation.
This annotation hides the state from the tool signature (so the model doesn’t see it), but the tool can access it.