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.
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.
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.
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.