Maintain context persistence for the agent #26259
Unanswered
juliouyehara
asked this question in
Q&A
Replies: 1 comment 14 replies
-
Hello, @juliouyehara! I'm here to help you with your coding questions and bug fixes. Let's tackle this challenge together! To configure the agent to pass the template context just once and ensure that only output tokens are spent after each new agent call in LangChain, you can use the from langgraph.prebuilt import create_react_agent
from langgraph.prebuilt.chat_agent_executor import AgentState
from langchain_core.prompts import ChatPromptTemplate
# Define the prompt template
prompt = ChatPromptTemplate.from_messages(
[
("system", "You are a helpful assistant."),
("placeholder", "{messages}"),
]
)
# Define the state modifier function
def _modify_state_messages(state: AgentState):
# Pass the template context just once
return prompt.invoke({"messages": state["messages"]}).to_messages()
# Create the agent with the state modifier
app = create_react_agent(model, tools, state_modifier=_modify_state_messages)
# Invoke the agent
messages = app.invoke({"messages": [("human", query)]})
print(
{
"input": query,
"output": messages["messages"][-1].content,
}
) This setup ensures that the template context is passed only once, and subsequent calls will only spend tokens on the output [1]. |
Beta Was this translation helpful? Give feedback.
14 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Checked other resources
Commit to Help
Example Code
Follow this format:
Question: input question to answer
Thought: consider previous and subsequent steps
Action:
Observation: action result
... (repeat Thought/Action/Observation N times)
Thought: I know what to respond
Action:
Description
When I configure the agent, it consumes the tokens in the template, and after each new agent call it continues to consume the template tokens. I want to know if there is a way to pass the template context just once, and after each new call only output tokens are spent.
System Info
platform (windows)
python version: 3.11.9
Beta Was this translation helpful? Give feedback.
All reactions