Summarization middleware.
Default prompt used to summarize conversation history.
The <messages> marker (on its own line) and the {messages} placeholder are
part of this constant's public contract, not just cosmetic formatting.
Downstream consumers depend on them: for example, deep agents'
SummarizationMiddleware splices an extra instruction block in immediately
before the <messages> marker via str.replace. Removing, renaming, or
reformatting the marker (or the {messages} placeholder) is a breaking change
for those consumers even though it does not alter any function signature, so
treat edits to it accordingly.
Fraction of model's maximum input tokens.
Absolute number of tokens.
Absolute number of messages.
Return metadata that marks a model call as internal to middleware.
Initialize a chat model from any supported provider using a unified interface.
Two main use cases:
config. Makes it easy to
switch between models/providers without changing your codeRequires the integration package for the chosen model provider to be installed.
See the model_provider parameter below for specific package names
(e.g., pip install langchain-openai).
Refer to the provider integration's API reference
for supported model parameters to use as **kwargs.
Keep internal model calls out of run.messages and the raw event log.
Used by middleware that makes internal model calls and runs before built-in transformers.
For tagged events, streamed message-start events are marked as tool-role and
whole-AIMessage payloads are cleared so MessagesTransformer ignores them.
The mutated events are then dropped from the raw log.
Only events within this transformer's scope are modified.
Base middleware class for an agent.
Subclass this and implement any of the defined methods to customize agent behavior between steps in the main agent loop.
State schema for the agent.
Dictionary-based trigger specification for AND conditions.
All specified thresholds in a single TriggerClause must be met for the clause to
trigger summarization (AND semantics). When multiple clauses are provided in a list,
summarization triggers if any clause is met (OR semantics).
Summarizes conversation history when token limits are approached.
This middleware monitors message token counts and automatically summarizes older messages when a threshold is reached, preserving recent messages and maintaining context continuity by ensuring AI/Tool message pairs remain together.
Transient summary-generation errors (rate limits, timeouts) are retried
in-process, up to 3 attempts total, via Runnable.with_retry. If a summary call
still fails after those attempts, the underlying error propagates rather than
fabricating a summary.
Union type for context size specifications.
Can be either:
ContextFraction: A
fraction of the model's maximum input tokens.ContextTokens: An absolute
number of tokens.ContextMessages: An
absolute number of messages.Depending on use with trigger or keep parameters, this type indicates either
when to trigger summarization or how much context to retain.