Skip to content

SlidingWindowPolicy

autogen.beta.policies.sliding_window.SlidingWindowPolicy #

SlidingWindowPolicy(max_events, transparent=False)

Keep the last N events. Drop older events.

Optional transparency: injects a note about how many events were omitted.

Source code in autogen/beta/policies/sliding_window.py
def __init__(self, max_events: int, transparent: bool = False) -> None:
    self._max = max_events
    self._transparent = transparent

name class-attribute instance-attribute #

name = 'sliding_window'

apply async #

apply(prompts, events, context)
Source code in autogen/beta/policies/sliding_window.py
async def apply(
    self,
    prompts: list[str],
    events: list[BaseEvent],
    context: Context,
) -> tuple[list[str], list[BaseEvent]]:
    total = len(events)
    if total <= self._max:
        return prompts, events
    trimmed = events[-self._max :]
    # Skip leading ToolResultsEvents whose matching tool_use was trimmed away.
    while trimmed and isinstance(trimmed[0], ToolResultsEvent):
        trimmed = trimmed[1:]
    if self._transparent:
        prompts = prompts + [f"[{self.name}] Showing last {len(trimmed)} of {total} events."]
    return prompts, trimmed