I am using SummarizationMiddleware ,
SummarizationMiddleware(
model = model,
trigger=[("tokens", 200), ("messages", 2)],
keep = ("messages", 3)
),
my total token 8598 but SummarizationMiddleware are not calling
Hi @mohiuddin1221
could you share the full agent code, so that I can reproduce the issue?
async def agent(state: InputState, runtime: Runtime[Context]):
client = MultiServerMCPClient(
{
“RedshopInternalMcpSercer”: {
“url”: “http://127.0.0.1:8000/mcp/”,
“transport”: “streamable_http”,
}
}
)
tools = await client.get_tools()
all_tools = tools + [send_email_tool]
agent_instance = create_agent(
model=model,
tools=all_tools,
state_schema=InputState,
system_prompt=create_Aget_prompt(state.get("user_input"), state.get("rephased_question")),
context_schema=Context,
checkpointer=InMemorySaver(),
middleware=[
HumanInTheLoopMiddleware(
interrupt_on={
"send_email_tool": {
"allowed_decisions": ["approve", "edit", "reject"],
},
}
),
SummarizationMiddleware(
model = model,
trigger=[("tokens", 200), ("messages", 2)],
keep = ("messages", 3)
),
ToolCallLimitMiddleware(thread_limit=5, run_limit=5),
handle_tool_errors,
],
)
rephased_question = state.get("rephased_question")
print("rephased_question", rephased_question)
result = await agent_instance.ainvoke(
{"rephased_question": rephased_question}
)
print("Result message..................", result)
return result
If the code quality needs improvement, then please give me suggestions.
hi @mohiuddin1221
could you also share InputState and Context? Use code block for code snippet please.
@dataclass
class Context:
user_id: str
user_name: str
user_preference: str
class InputState(TypedDict):
user_input: str
rephased_question: str
answer: str
Ok, I see the issue.
This middleware operates only on the messages key in the agent state. It does not summarize (or otherwise transform) the whole state. There is no messages on your state.
1 Like
class InputState(TypedDict):
user_input: str
rephased_question: str
answer: str
thread_name: str
messages: Annotated[list[AnyMessage], add_messages]
#############
SummarizationMiddleware(
model = model,
trigger=[(“tokens”, 200)],
keep = (“messages”, 3)
),
I am pushing the state fields like user_input, rephased_question, and answer into every turn of messages. All messages are present, but still, the SummarizationMiddleware is not being called.
Can you share you entire agent code?