HELP IN ADDING MEMORY TO REACT AGENT #794

Closed
opened 2026-02-20 17:41:47 -05:00 by yindo · 4 comments
Owner

Originally created by @yadgire-saurabh on GitHub (Jul 11, 2025).

I have built a react agent with a pre_model_hook [code blocks attached]

`
def pre_model_hook(state):
"""Trims messages to fit in the context window."""
trimmed_messages = trim_messages(
state["messages"],
strategy="last",
token_counter=count_tokens_approximately,
max_tokens=10000,
start_on="human",
end_on=("human", "tool"),
allow_partial = True,
)
return {"llm_input_messages": trimmed_messages}

agent_executor = create_react_agent(
llm,
tools=mcp_server_tools,
state_schema=CustomAgentState,
prompt=prompt_func,
pre_model_hook=pre_model_hook,
checkpointer=checkpointer
)
`

I have a few questions:

How can I see the contents of y memory/pre_model_hook without streaming output (I am using invoke)
Is the pre_model_hook used as memory/context for subsequent calls
How is post_model_hook different?
If I pass tools as ToolNode([mcp_tools]), it does not work
Can you help me with any of these issues I am facing?

Image

EDIT: The trimmed messages only shows the last HumanMessage.
Here is my agent graph image

Thanks!

Originally created by @yadgire-saurabh on GitHub (Jul 11, 2025). I have built a react agent with a pre_model_hook [code blocks attached] ` def pre_model_hook(state): """Trims messages to fit in the context window.""" trimmed_messages = trim_messages( state["messages"], strategy="last", token_counter=count_tokens_approximately, max_tokens=10000, start_on="human", end_on=("human", "tool"), allow_partial = True, ) return {"llm_input_messages": trimmed_messages} agent_executor = create_react_agent( llm, tools=mcp_server_tools, state_schema=CustomAgentState, prompt=prompt_func, pre_model_hook=pre_model_hook, checkpointer=checkpointer ) ` I have a few questions: How can I see the contents of y memory/pre_model_hook without streaming output (I am using invoke) Is the pre_model_hook used as memory/context for subsequent calls How is post_model_hook different? If I pass tools as ToolNode([mcp_tools]), it does not work Can you help me with any of these issues I am facing? <img width="1098" height="1216" alt="Image" src="https://github.com/user-attachments/assets/e29ef294-5099-41b8-9cc6-4ff82dfa0fcb" /> EDIT: The trimmed messages only shows the last HumanMessage. Here is my agent graph image Thanks!
yindo added the invalidawaiting author revision labels 2026-02-20 17:41:47 -05:00
yindo closed this issue 2026-02-20 17:41:47 -05:00
Author
Owner

@yoadsn commented on GitHub (Jul 11, 2025):

@yadgire-saurabh I hope I understood your question and might be able to help with any of them.

If you want to see the state after the pre_model_hook you will need to use a checkpointer, and inspect the "state history" of the agent. (the CompiledStateGraph).
You can also stream the updates as you mentioned - I think for debugging that more convenient.

The pre_model_hook is called before the "agent" node any time the node is about to be invoked (after "start" or after "tools" are done executing).

The post_model_hook is called after the "agent" node any time it's done producing a result (with or without a tool call).

I am not sure why the tool passing does not work for you, I suggest debugging the Library code itself - I do that often when things do not behave as they should.

Reg "message trimming" - I am not using that functionality myself, and so cannot comment on that.

hth

@yoadsn commented on GitHub (Jul 11, 2025): @yadgire-saurabh I hope I understood your question and might be able to help with any of them. If you want to see the state after the `pre_model_hook` you will need to use a checkpointer, and inspect the "state history" of the agent. (the CompiledStateGraph). You can also stream the updates as you mentioned - I think for debugging that more convenient. The `pre_model_hook` is called before the "agent" node any time the node is about to be invoked (after "start" or after "tools" are done executing). The `post_model_hook` is called after the "agent" node any time it's done producing a result (with or without a tool call). I am not sure why the tool passing does not work for you, I suggest debugging the Library code itself - I do that often when things do not behave as they should. Reg "message trimming" - I am not using that functionality myself, and so cannot comment on that. hth
Author
Owner

@yadgire-saurabh commented on GitHub (Jul 11, 2025):

Thanks for the detailed response. I already have a checkpointer added at the time of invoking the react agent (a chat assistant). The thread_id is the user ID which remains unchanged throughout the chat instance. I think the issue is - I compiled the graph for every user input in the chat.

Will debug!

@yadgire-saurabh commented on GitHub (Jul 11, 2025): Thanks for the detailed response. I already have a checkpointer added at the time of invoking the react agent (a chat assistant). The thread_id is the user ID which remains unchanged throughout the chat instance. I think the issue is - I compiled the graph for every user input in the chat. Will debug!
Author
Owner

@dlrklc commented on GitHub (Jul 13, 2025):

Hi @yadgire-saurabh, for tool passing this works for me when I use create_react_agent
For example, if there is a calculator tool

from langchain.tools import Tool
calculator_tool = Tool(`
    name="calculator",
    func=calculate,
    description="Use this tool to perform basic arithmetic calculations. Input should be a math expression like '2 + 2'."
)

or if I created with @tool decorator

from langchain_core.tools import tool

@tool
def calculator_tool(expression: str) -> str:
    return str(eval(expression))

And then,

agent = create_react_agent(llm=llm, tools=[calculator_tool], prompt=prompt)

Maybe the reason is not having square brackets around mcp_server_tools in your code.

Hope this helps!

@dlrklc commented on GitHub (Jul 13, 2025): Hi @yadgire-saurabh, for tool passing this works for me when I use `create_react_agent` For example, if there is a calculator tool ```python from langchain.tools import Tool calculator_tool = Tool(` name="calculator", func=calculate, description="Use this tool to perform basic arithmetic calculations. Input should be a math expression like '2 + 2'." ) ``` or if I created with `@tool` decorator ```python from langchain_core.tools import tool @tool def calculator_tool(expression: str) -> str: return str(eval(expression)) ``` And then, ```python agent = create_react_agent(llm=llm, tools=[calculator_tool], prompt=prompt) ``` Maybe the reason is not having square brackets around mcp_server_tools in your code. Hope this helps!
Author
Owner

@eyurtsev commented on GitHub (Jul 14, 2025):

@yadgire-saurabh could you please provide an MRE or check whether @dlrklc identified your problem correctly

@eyurtsev commented on GitHub (Jul 14, 2025): @yadgire-saurabh could you please provide an MRE or check whether @dlrklc identified your problem correctly
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langgraph#794