How do ContextEditingMiddleware and SummarizationMiddleware interact when used together?Combining ContextEditingMiddleware + SummarizationMiddleware — execution order and behavior when both trigger?
rushant001
·
2026-04-20
·
via LangChain Forum - Topics tagged python-help
Hi everyone, I’m using ContextEditingMiddleware and SummarizationMiddleware together in create_agent , and I’d like to understand the exact execution semantics when they’re combined. The docs describe each one individually but don’t explain how they interact. My code: agent_executor = create_agent( model=self.llm_service.llm, tools=self.tools, system_prompt=system_prompt, middleware=[ # Layer 1: clean up old tool results (lightweight, no LLM call) ContextEditingMiddleware( edits=[ ClearToolUsesEdit( trigger=self.clear_trigger, keep=self.clear_tool_keep, ), ], ), # Layer 2: summarization (heavier, uses a cheap LLM) SummarizationMiddleware( model=LLMService(model_name=self.summary_model_name).llm, trigger=self.summary_trigger, keep=self.summary_keep, trim_tokens_to_summarize=self.summary_trim_tokens, ), ], ) Questions I’d love help with: Execution order — Which one runs first? Does the order in the middleware=[] list matter, or is it determined by hook type ( before_model vs wrap_model_call )? Data flow — Does one middleware’s output feed into the other? Does the cleanup affect what the summary LLM sees? Does the summary output become the input that cleanup operates on? Both triggered at once — If the conversation exceeds both thresholds in the same call: Do both run? Which result reaches the main LLM? Are the effects combined, or does one override the other? State vs request — I noticed SummarizationMiddleware uses before_model and ContextEditingMiddleware uses wrap_model_call . Does that mean summarization persistently modifies state["messages"] while context editing only modifies the per-call request? What are the multi-turn implications? Recommended pattern — Is the “cheap cleanup first, summarization as fallback” pattern (often suggested in community tutorials) actually supported by this combination, or does it require custom middleware? A concrete walkthrough showing the message list before and after each middleware runs — especially for the “both triggered” case — would be really helpful. Using langchain 1.2.15 . Thanks! 1 post - 1 participant Read full topic
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。