Skip to content

Add automatic compaction of historical messages for agents #339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Mar 21, 2025

Conversation

bhouston
Copy link
Member

Overview

This PR implements issue #338 - Agent self-managed message compaction. It adds functionality to prevent context window overflow errors when agents run for extended periods.

Features

  1. Enhanced LLM Abstraction:

    • Added token limit tracking for all LLM providers (Anthropic, OpenAI, Ollama)
    • Each provider now returns total tokens used and maximum context window size
  2. Status Update Mechanism:

    • Agents receive periodic status updates (every 5 interactions)
    • Updates include token usage, cost, and information about active sub-agents, shells, and browser sessions
  3. Message Compaction Tool:

    • Added a compactHistory tool that agents can use to reduce context size
    • Tool summarizes older messages while preserving recent context
    • Agents are instructed to use this tool when token usage approaches 70% of maximum
  4. Documentation:

    • Added documentation for the message compaction feature
    • Updated README.md to mention the new feature
    • Created example status update file

Testing

Added tests for:

  • The compactHistory tool functionality
  • Status update generation

Benefits

  • Prevents context window overflow errors
  • Maintains important context for agent operation
  • Enables longer-running agent sessions
  • Makes the system more robust for complex tasks

Closes #338

semantic-release-bot and others added 6 commits March 21, 2025 18:15
# [mycoder-agent-v1.6.0](mycoder-agent-v1.5.0...mycoder-agent-v1.6.0) (2025-03-21)

### Features

* **browser:** add system browser detection for Playwright ([00bd879](00bd879)), closes [#333](#333)
# [mycoder-v1.6.0](mycoder-v1.5.0...mycoder-v1.6.0) (2025-03-21)

### Features

* **browser:** add system browser detection for Playwright ([00bd879](00bd879)), closes [#333](#333)
Implements #338 - Agent self-managed message compaction:

1. Enhanced LLM abstraction to track token limits for all providers
2. Added status update mechanism to inform agents about resource usage
3. Created compactHistory tool for summarizing older messages
4. Updated agent documentation and system prompt
5. Added tests for the new functionality
6. Created documentation for the message compaction feature

This feature helps prevent context window overflow errors by
giving agents awareness of their token usage and tools to
manage their context window.
- Change token usage threshold from 70% to 50% for compaction recommendations
- Add threshold-based status updates (send updates when usage exceeds 50%)
- Update documentation and tests to reflect these changes
- Make compaction recommendations more proactive at high usage
@bhouston
Copy link
Member Author

Based on user feedback, I've made the following improvements:

  1. Changed the token usage threshold from 70% to 50% for compaction recommendations
  2. Added threshold-based status updates - now status updates are sent:
    • Every 5 agent interactions (periodic updates)
    • Whenever token usage exceeds 50% of the maximum (threshold-based updates)
  3. Made compaction recommendations more proactive - when usage is high, the message now actively recommends using the compactHistory tool
  4. Updated documentation and tests to reflect these changes

These changes make the message compaction feature more proactive and help ensure that agents don't wait until it's too late to compact their message history.

- Added message-compaction.md to packages/docs/docs/usage
- Updated usage index to include message compaction
- Added compactHistory tool to the tools table
@bhouston
Copy link
Member Author

I've updated the docs website to include information about message compaction:

  1. Added message-compaction.md to the usage section with detailed documentation
  2. Updated the usage index to include message compaction in the list of features
  3. Added the compactHistory tool to the tools table

The documentation covers:

  • How message compaction works
  • Status updates and their frequency
  • How to use the compactHistory tool
  • Benefits of message compaction
  • Model token limits

This ensures that users will be able to find information about this feature in the official documentation.

@bhouston
Copy link
Member Author

I've fixed several TypeScript errors and test failures in the message compaction implementation:\n\n- Fixed variable redeclaration in ollama.ts provider\n- Fixed variable usage before declaration in openai.ts provider\n- Fixed context access in compactHistory.ts\n- Updated tests to work with the new implementation\n- Fixed import order issues\n\nAll tests are now passing and the build is successful.

@bhouston bhouston merged commit 33f6318 into main Mar 21, 2025
Copy link

🎉 This PR is included in version mycoder-agent-v1.7.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add automatic compaction of historical messages for agents
2 participants