Summarization Middleware
Search documents
Summarization Middleware (Python)
LangChain· 2025-12-02 17:01
Core Concept - Context engineering is crucial for optimizing agent performance by providing the right information and tools at the right time [2] - Summarization is a key tool for context engineering, especially for agents with long conversation histories [3] Langchain's Summarization Middleware - Langchain introduces a summarization middleware to help agents focus on relevant information [5] - The middleware allows customization of the summarization model, context size (in tokens, messages, or proportion), and retention policy [6] - Langchain's new API documentation provides options to customize the summary prompt and trim context [7] - The middleware can be triggered based on a percentage of the context window size used, leveraging Langchain's model profiles package for model capability information [9][10] Practical Application - The summarization middleware is demonstrated with an agent retrieving information from Wikipedia [8][11] - The demo showcases how the middleware is triggered after a certain amount of context is used, summarizing previous interactions [13] - The custom prompt and summary output are integrated into the final model request [14]