# SummarizationMiddleware

**URL:** <https://forum.langchain.com/t/summarizationmiddleware/2461>\
**Category:** LangGraph\
**Tags:** python-help\
**Created:** [December 9, 2025, 7:59pm UTC](https://forum.langchain.com/t/summarizationmiddleware/2461 "2025-12-09T19:59:30Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mohiuddin1221](https://yyz1.discourse-cdn.com/flex007/user_avatar/forum.langchain.com/mohiuddin1221/32/1822_2.png) [@mohiuddin1221](https://forum.langchain.com/u/mohiuddin1221)\
**Post date:** [December 9, 2025, 7:59pm UTC](https://forum.langchain.com/t/summarizationmiddleware/2461/1 "2025-12-09T19:59:30Z")

</div>

![Screenshot from 2025-12-10 01-45-09](https://canada1.discourse-cdn.com/flex007/uploads/langchain/original/2X/1/1cae59f33c6275f1af86b670c1a2be690e07f5e8.png)

I am using SummarizationMiddleware ,

```
        SummarizationMiddleware(
            model = model,
            trigger=[("tokens", 200), ("messages", 2)],
            keep = ("messages", 3)
        ),

```

my total token 8598 but SummarizationMiddleware are not calling

---

<div class="post-metadata">

**Author:** ![pawel-twardziak](https://yyz1.discourse-cdn.com/flex007/user_avatar/forum.langchain.com/pawel-twardziak/32/960_2.png) [@pawel-twardziak](https://forum.langchain.com/u/pawel-twardziak)\
**Post date:** [December 9, 2025, 8:11pm UTC](https://forum.langchain.com/t/summarizationmiddleware/2461/2 "2025-12-09T20:11:49Z")

</div>

Hi @mohiuddin1221

could you share the full agent code, so that I can reproduce the issue?

---

<div class="post-metadata">

**Author:** ![mohiuddin1221](https://yyz1.discourse-cdn.com/flex007/user_avatar/forum.langchain.com/mohiuddin1221/32/1822_2.png) [@mohiuddin1221](https://forum.langchain.com/u/mohiuddin1221)\
**Post date:** [December 9, 2025, 8:48pm UTC](https://forum.langchain.com/t/summarizationmiddleware/2461/3 "2025-12-09T20:48:12Z")

</div>

async def agent(state: InputState, runtime: Runtime[Context]):  
client = MultiServerMCPClient(  
{  
“RedshopInternalMcpSercer”: {  
“url”: “[http://127.0.0.1:8000/mcp/](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.

---

<div class="post-metadata">

**Author:** ![pawel-twardziak](https://yyz1.discourse-cdn.com/flex007/user_avatar/forum.langchain.com/pawel-twardziak/32/960_2.png) [@pawel-twardziak](https://forum.langchain.com/u/pawel-twardziak)\
**Post date:** [December 11, 2025, 2:35pm UTC](https://forum.langchain.com/t/summarizationmiddleware/2461/4 "2025-12-11T14:35:47Z")

</div>

@mohiuddin1221

I am looking at it today

---

<div class="post-metadata">

**Author:** ![pawel-twardziak](https://yyz1.discourse-cdn.com/flex007/user_avatar/forum.langchain.com/pawel-twardziak/32/960_2.png) [@pawel-twardziak](https://forum.langchain.com/u/pawel-twardziak)\
**Post date:** [December 11, 2025, 6:12pm UTC](https://forum.langchain.com/t/summarizationmiddleware/2461/5 "2025-12-11T18:12:59Z")

</div>

hi @mohiuddin1221

could you also share `InputState` and `Context`? Use code block for code snippet please.

---

<div class="post-metadata">

**Author:** ![mohiuddin1221](https://yyz1.discourse-cdn.com/flex007/user_avatar/forum.langchain.com/mohiuddin1221/32/1822_2.png) [@mohiuddin1221](https://forum.langchain.com/u/mohiuddin1221)\
**Post date:** [December 11, 2025, 9:38pm UTC](https://forum.langchain.com/t/summarizationmiddleware/2461/6 "2025-12-11T21:38:55Z")

</div>

@dataclass  
class Context:  
user\_id: str  
user\_name: str  
user\_preference: str

class InputState(TypedDict):  
user\_input: str  
rephased\_question: str  
answer: str

---

<div class="post-metadata">

**Author:** ![pawel-twardziak](https://yyz1.discourse-cdn.com/flex007/user_avatar/forum.langchain.com/pawel-twardziak/32/960_2.png) [@pawel-twardziak](https://forum.langchain.com/u/pawel-twardziak)\
**Post date:** [December 11, 2025, 11:08pm UTC](https://forum.langchain.com/t/summarizationmiddleware/2461/7 "2025-12-11T23:08:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mohiuddin1221](https://yyz1.discourse-cdn.com/flex007/user_avatar/forum.langchain.com/mohiuddin1221/32/1822_2.png) [@mohiuddin1221](https://forum.langchain.com/u/mohiuddin1221)\
**Post date:** [December 12, 2025, 11:47pm UTC](https://forum.langchain.com/t/summarizationmiddleware/2461/8 "2025-12-12T23:47:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pawel-twardziak](https://yyz1.discourse-cdn.com/flex007/user_avatar/forum.langchain.com/pawel-twardziak/32/960_2.png) [@pawel-twardziak](https://forum.langchain.com/u/pawel-twardziak)\
**Post date:** [December 13, 2025, 12:00am UTC](https://forum.langchain.com/t/summarizationmiddleware/2461/9 "2025-12-13T00:00:54Z")

</div>

Can you share you entire agent code?
