Add dual context percentage fields to working memory endpoints#38
Merged
Add dual context percentage fields to working memory endpoints#38
Conversation
- Add context_usage_percentage field to WorkingMemoryResponse model
- Add _calculate_context_usage_percentage() helper function
- Update GET /v1/working-memory/{session_id} to return percentage
- Update PUT /v1/working-memory/{session_id} to return percentage based on final state (after potential summarization)
- Percentage calculated as (current_tokens / token_threshold) * 100 where token_threshold = context_window * 0.7
- Returns None when no model info provided, otherwise 0-100% value
Resolves #37
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-authored-by: Andrew Brookins <abrookins@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR adds context usage percentage tracking to working memory endpoints to help monitor how much of the context window is being utilized before auto-summarization is triggered. The change provides visibility into memory usage patterns and helps understand when summarization occurs.
Key Changes:
- Added
context_usage_percentagefield toWorkingMemoryResponsemodel - Implemented calculation logic to determine percentage of context window used
- Updated GET and PUT working memory endpoints to include the percentage in responses
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| agent_memory_server/models.py | Added context_usage_percentage field to WorkingMemoryResponse |
| agent_memory_server/api.py | Implemented context usage calculation and updated endpoints to return percentage |
| tests/test_full_integration.py | Code formatting improvements for assert statements |
🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
Resolve TypeError by properly handling the context_usage_percentage field in WorkingMemoryResponse creation to avoid duplicate keyword arguments. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
- Add context_usage_percentage field to WorkingMemoryResponse model - Add comprehensive test suite for the new field covering: - Field creation and default values - Serialization behavior - Validation of different percentage values - Dictionary-to-model conversion 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
Address review comments by making the 0.7 threshold configurable instead of hardcoded. Added summarization_threshold setting that can be configured via environment variable or config file. - Added summarization_threshold to Settings (default: 0.7) - Updated both _calculate_context_usage_percentage and _summarize_working_memory to use settings.summarization_threshold - Improved maintainability and consistency between functions - Allows users to customize when summarization is triggered 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
- Add context_percentage_total_used field showing actual context window usage (0-100%) - Add context_percentage_until_summarization field showing percentage until auto-summarization triggers (0-100%) - Update API calculation function to return both values as tuple - Update server and SDK models with new fields - Update comprehensive test coverage for both fields - Remove old single context_usage_percentage field - Maintain configurable summarization threshold (default 70%) 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
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.
Summary
Add dual context percentage fields to working memory endpoints to provide comprehensive visibility into context window usage and auto-summarization triggers.
Changes
New Fields Added
context_percentage_total_used: Shows actual percentage of total context window currently used (0-100%)context_percentage_until_summarization: Shows percentage until auto-summarization triggers (0-100%, reaches 100% at summarization threshold)Implementation Details
_calculate_context_usage_percentages()to return both values as a tuple/v1/working-memory/{session_id}and PUT/v1/working-memory/{session_id}endpointsWorkingMemoryResponse) with new fieldsSUMMARIZATION_THRESHOLD)Backward Compatibility
context_usage_percentagefieldBenefits
Users now receive complete context information:
Testing
Example Response
{ "session_id": "example-session", "messages": [...], "context_percentage_total_used": 45.5, "context_percentage_until_summarization": 65.0, ... }Resolves #37
🤖 Generated with Claude Code