feat(hooks): add Before/AfterReduceContextEvent for context reduction observability#2227
Open
VincentBai-dotcom wants to merge 1 commit intostrands-agents:mainfrom
Open
Conversation
…ager Move event emission from the single overflow call site in agent.py into ConversationManager.reduce_context as a concrete template method. Subclasses implement _reduce_context; the framework wraps it with Before/After event emission so every call path (reactive overflow, proactive apply_management, per-turn, direct tool calls) gets events automatically. An __init_subclass__ shim detects third-party subclasses that override reduce_context directly, transparently re-wires them to _reduce_context, and emits a DeprecationWarning. Closes strands-agents#2048
f1994a2 to
250cd55
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Observability plugins need to detect when conversation context compaction occurs. The only existing approach was polling
conversation_manager.removed_message_countand computing deltas — fragile, timing-dependent, and can miss events or produce duplicates. The SDK emits hook events for model calls, tool calls, and invocations, but had no event for context reduction.Resolves: #2048
Public API Changes
Two new hook events and a template-method change to
ConversationManager:ConversationManager.reduce_context()is now a concrete template method that wraps_reduce_context()with event emission. Subclasses should override_reduce_context()instead. Third-party subclasses that still overridereduce_context()directly continue to work — an__init_subclass__shim transparently re-wires them and emits aDeprecationWarning.Events fire on all code paths: reactive overflow in the event loop, proactive
apply_management, per-turn hooks, and direct tool calls.Breaking Changes
ConversationManagersubclasses that overridereduce_context()directly will see aDeprecationWarningnudging them to override_reduce_context()instead. Existing behavior is preserved — no code changes required.Migration
Type of Change
New feature
Testing
hatch run prepareChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.