create_summarization_middleware(
modelCreate a Deep Agents SummarizationMiddleware with model-aware defaults.
deepagentsThe Deep Agents SummarizationMiddleware wraps
langchain.agents.middleware.SummarizationMiddleware to add behavior
long-running, file-aware agents need. Prefer LangChain's middleware
directly if none of the below apply:
/conversation_history/{session_id}.md (default path) on the
configured backend before the summary replaces them, and the
summary embeds that path so the agent can re-open it via
read_file when FilesystemMiddleware is registered. LangChain
drops evicted messages with no recovery path.write_file /
edit_file arguments in older messages are clipped at a lower
threshold than full compaction, often reclaiming enough context
to skip summarizing. Configured via truncate_args_settings.ContextOverflowError fallback. On a provider over-budget
rejection the middleware summarizes and retries instead of
bubbling the error up._summarization_event field via wrap_model_call,
leaving state["messages"] intact. LangChain rewrites it with
RemoveMessage(id=REMOVE_ALL_MESSAGES) from before_model.
Preserving the raw log enables replay, evals, and shared state
with SummarizationToolMiddleware's compact_conversation tool.trigger=None and
keep=("messages", 20). This factory picks fraction-based
defaults from the model's profile when max_input_tokens is
exposed, falling back to fixed counts otherwise ā see
compute_summarization_defaults.| Name | Type | Description |
|---|---|---|
model* | BaseChatModel | |
backend* | BackendProtocol | |
summary_prompt | str | Default: DEEPAGENTS_DEFAULT_SUMMARY_PROMPT |
trim_tokens_to_summarize | int |
Default: None |
token_counter | TokenCounter | Default: count_tokens_approximately |
Resolved BaseChatModel instance.
Use resolve_model() first if needed for model strings.
Backend instance for persisting conversation history.
Prompt template for generating summaries.
Token budget for the older history sent to the
summarization model, measured by token_counter. Applied after
setting aside the recent messages selected by the retention policy.
For example, 4000 summarizes up to 4,000 tokens from the end of
that older history; earlier history excluded by trimming is not
summarized. This does not set the summarization trigger or limit
the generated summary's length.
None skips this trimming and summarizes all selected history.
This factory defaults to None, unlike constructing
SummarizationMiddleware directly, which defaults to 4000.
Function to count tokens in messages.