You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Implement context compaction to handle long-running tasks
- Add token usage tracking to Context class to monitor cumulative LLM usage
- Implement need_compaction property that triggers when usage exceeds 80% of context window
- Add compact() method that uses LLM to summarize conversation history with structured format
- Add _format_history_for_compaction() to format messages for summarization
- Integrate compaction checks at start of each agent step in both sync and async loops
- Update token usage after each LLM invocation
- Emit events when compaction occurs for visibility
- Add compact.md template with detailed instructions for history summarization
- Preserve system messages and error tracking through compaction cycle
This allows agents to continue working on extended tasks without hitting context window limits.
Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
0 commit comments