Sliding Window API¶
pydantic_ai_summarization.sliding_window
¶
Sliding window history processor for managing conversation context.
This module provides a simple, zero-cost strategy for managing context window limits by keeping only the most recent messages without LLM-based summarization.
SlidingWindowProcessor
dataclass
¶
History processor that keeps only the most recent messages.
This is the simplest and most efficient strategy for managing context limits. It has zero LLM cost and near-zero latency, making it ideal for scenarios where preserving exact conversation history is less important than performance.
Unlike SummarizationProcessor, this processor simply discards old messages without creating a summary. This means some context may be lost, but the operation is instantaneous and free.
Attributes:
| Name | Type | Description |
|---|---|---|
trigger |
ContextSize | list[ContextSize] | None
|
Threshold(s) that trigger window trimming. |
keep |
ContextSize
|
How many messages to keep after trimming. |
token_counter |
TokenCounter
|
Function to count tokens (only used for token-based triggers/keep). |
max_input_tokens |
int | None
|
Maximum input tokens (required for fraction-based config). |
Example
Source code in src/pydantic_ai_summarization/sliding_window.py
| Python | |
|---|---|
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | |
trigger = None
class-attribute
instance-attribute
¶
Threshold(s) that trigger window trimming.
Examples:
- ("messages", 100) - trigger when 100+ messages
- ("tokens", 100000) - trigger when 100k+ tokens
- ("fraction", 0.8) - trigger at 80% of max tokens (requires max_input_tokens)
- [("messages", 100), ("tokens", 50000)] - trigger on either condition
keep = ('messages', _DEFAULT_WINDOW_SIZE)
class-attribute
instance-attribute
¶
How many messages to keep after trimming.
Examples:
- ("messages", 50) - keep last 50 messages
- ("tokens", 10000) - keep last 10k tokens worth
- ("fraction", 0.3) - keep last 30% of max_input_tokens
token_counter = field(default=count_tokens_approximately)
class-attribute
instance-attribute
¶
Function to count tokens in messages. Only used for token-based triggers/keep.
max_input_tokens = None
class-attribute
instance-attribute
¶
Maximum input tokens for the model (required for fraction-based triggers).
__post_init__()
¶
Validate configuration and set up trigger conditions.
__call__(messages)
async
¶
Process messages and trim if needed.
This is the main entry point called by pydantic-ai's history processor mechanism.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
messages
|
list[ModelMessage]
|
Current message history. |
required |
Returns:
| Type | Description |
|---|---|
list[ModelMessage]
|
Trimmed message history if threshold was reached, otherwise unchanged. |
Source code in src/pydantic_ai_summarization/sliding_window.py
create_sliding_window_processor(trigger=('messages', _DEFAULT_TRIGGER_MESSAGES), keep=('messages', _DEFAULT_WINDOW_SIZE), max_input_tokens=None, token_counter=None)
¶
Create a sliding window history processor.
This is a convenience factory function for creating SlidingWindowProcessor instances with sensible defaults.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trigger
|
ContextSize | list[ContextSize] | None
|
When to trigger window trimming. Can be: - ("messages", N) - trigger when N+ messages - ("tokens", N) - trigger when N+ tokens - ("fraction", F) - trigger at F fraction of max_input_tokens - List of tuples to trigger on any condition Defaults to ("messages", 100). |
('messages', _DEFAULT_TRIGGER_MESSAGES)
|
keep
|
ContextSize
|
How many messages to keep after trimming. Defaults to ("messages", 50). |
('messages', _DEFAULT_WINDOW_SIZE)
|
max_input_tokens
|
int | None
|
Maximum input tokens (required for fraction-based triggers). |
None
|
token_counter
|
TokenCounter | None
|
Custom token counting function. Defaults to approximate counter. |
None
|
Returns:
| Type | Description |
|---|---|
SlidingWindowProcessor
|
Configured SlidingWindowProcessor. |
Example
from pydantic_ai import Agent
from pydantic_ai_summarization import create_sliding_window_processor
# Simple: keep last 30 messages when reaching 60
processor = create_sliding_window_processor(
trigger=("messages", 60),
keep=("messages", 30),
)
# Token-based: keep ~50k tokens when reaching 100k
processor = create_sliding_window_processor(
trigger=("tokens", 100000),
keep=("tokens", 50000),
)
agent = Agent(
"openai:gpt-4.1",
history_processors=[processor],
)