sdk-python icon indicating copy to clipboard operation
sdk-python copied to clipboard

[FEATURE] Expose OnEviction hook for context reduction events

Open opieter-aws opened this issue 5 months ago • 4 comments

SDK Language

Both / Not language-specific

Problem Statement

There is currently no event emitted when context compression occurs.

Use Case

We want to use this event to create an ExtractionTrigger for MemoryStores, so facts are ingested/extracted upon context window eviction.

opieter-aws avatar Jun 15 '26 19:06 opieter-aws

Observed in: strands-py/src/strands/_context_manager/modes/agentic/ - context compression and eviction

Problem: The existing InMemoryStorage.evict_after_turns mechanism (in strands-py/src/strands/vended_plugins/context_offloader/storage.py) handles time-based eviction for stored context entries, but there is no corresponding lifecycle hook for the context windows managed by the agentic context manager itself.

When context compression occurs in agentic_context.py, entries are compressed/removed from the active context window, but no event is emitted. This means:

  • Memory plugins cannot react to context compression events to extract facts before they're summarized away
  • Observability tooling cannot track what information was compressed
  • Steering handlers cannot adjust behavior based on what was removed

Relationship to OnEviction (#2804): The existing _evict method on storage backends is turn-cycle-based. What's missing is a real-time hook that fires when the agentic context manager actually compacts or truncates a context window — before the content is lost to summarization. This is distinct from evict_after_turns which runs on a fixed schedule.

Suggested API (matching the Strands hook pattern):

@dataclass
class ContextReductionEvent:
    cycle_count: int
    entries_removed: int
    original_token_count: int
    reduced_token_count: int
    entry_ids: list[str]

class ContextReductionHook:
    def on_context_reduction(self, event: ContextReductionEvent) -> None: ...

This would be wired into the context compactor so memory stores can implement ContextReductionHook and extract facts during compression rather than waiting for the next turn cycle.

Existing precedent: The Strands SDK already has hook patterns (e.g., steering handlers, guardrails). The ContextReductionHook would follow the same OnEvent naming convention and registration pattern.

cschanhniem avatar Jun 19 '26 07:06 cschanhniem

@opieter-aws - I know this was discussed offline - is this still needed vs another technique?

Possibly related to #2805

zastrowm avatar Jun 19 '26 15:06 zastrowm

We've had customer asks for this as well; specifically an hook that triggers when context changes (message added for context reduced, etc.). So marking as a Higher priority for that reason

zastrowm avatar Jun 19 '26 15:06 zastrowm

@zastrowm yes, we still want this event. It will allow us to create an extra trigger for memory extraction.

opieter-aws avatar Jun 22 '26 15:06 opieter-aws