|
| 1 | +# Message Compaction |
| 2 | + |
| 3 | +When agents run for extended periods, they accumulate a large history of messages that eventually fills up the LLM's context window, causing errors when the token limit is exceeded. The message compaction feature helps prevent this by providing agents with awareness of their token usage and tools to manage their context window. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +### 1. Token Usage Tracking |
| 8 | + |
| 9 | +The LLM abstraction now tracks and returns: |
| 10 | +- Total tokens used in the current completion request |
| 11 | +- Maximum allowed tokens for the model/provider |
| 12 | + |
| 13 | +This information is used to monitor context window usage and trigger appropriate actions. |
| 14 | + |
| 15 | +### 2. Status Updates |
| 16 | + |
| 17 | +Agents receive periodic status updates (every 5 interactions) with information about: |
| 18 | +- Current token usage and percentage of the maximum |
| 19 | +- Cost so far |
| 20 | +- Active sub-agents and their status |
| 21 | +- Active shell processes and their status |
| 22 | +- Active browser sessions and their status |
| 23 | + |
| 24 | +Example status update: |
| 25 | +``` |
| 26 | +--- STATUS UPDATE --- |
| 27 | +Token Usage: 45,235/100,000 (45%) |
| 28 | +Cost So Far: $0.23 |
| 29 | +
|
| 30 | +Active Sub-Agents: 2 |
| 31 | +- sa_12345: Analyzing project structure and dependencies |
| 32 | +- sa_67890: Implementing unit tests for compactHistory tool |
| 33 | +
|
| 34 | +Active Shell Processes: 3 |
| 35 | +- sh_abcde: npm test |
| 36 | +- sh_fghij: npm run watch |
| 37 | +- sh_klmno: git status |
| 38 | +
|
| 39 | +Active Browser Sessions: 1 |
| 40 | +- bs_12345: https://www.typescriptlang.org/docs/handbook/utility-types.html |
| 41 | +
|
| 42 | +If token usage is high (>70%), consider using the 'compactHistory' tool to reduce context size. |
| 43 | +--- END STATUS --- |
| 44 | +``` |
| 45 | + |
| 46 | +### 3. Message Compaction Tool |
| 47 | + |
| 48 | +The `compactHistory` tool allows agents to compact their message history by summarizing older messages while preserving recent context. This tool: |
| 49 | + |
| 50 | +1. Takes a parameter for how many recent messages to preserve unchanged |
| 51 | +2. Summarizes all older messages into a single, concise summary |
| 52 | +3. Replaces the original messages with the summary and preserved messages |
| 53 | +4. Reports on the reduction in context size |
| 54 | + |
| 55 | +## Usage |
| 56 | + |
| 57 | +Agents are instructed to monitor their token usage through status updates and use the `compactHistory` tool when token usage approaches 70% of the maximum: |
| 58 | + |
| 59 | +```javascript |
| 60 | +// Example of agent using the compactHistory tool |
| 61 | +{ |
| 62 | + name: "compactHistory", |
| 63 | + preserveRecentMessages: 10, |
| 64 | + customPrompt: "Focus on summarizing our key decisions and current tasks." |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +## Configuration |
| 69 | + |
| 70 | +The message compaction feature is enabled by default with reasonable defaults: |
| 71 | +- Status updates every 5 agent interactions |
| 72 | +- Recommendation to compact at 70% token usage |
| 73 | +- Default preservation of 10 recent messages when compacting |
| 74 | + |
| 75 | +## Model Token Limits |
| 76 | + |
| 77 | +The system includes token limits for various models: |
| 78 | + |
| 79 | +### Anthropic Models |
| 80 | +- claude-3-opus-20240229: 200,000 tokens |
| 81 | +- claude-3-sonnet-20240229: 200,000 tokens |
| 82 | +- claude-3-haiku-20240307: 200,000 tokens |
| 83 | +- claude-2.1: 100,000 tokens |
| 84 | + |
| 85 | +### OpenAI Models |
| 86 | +- gpt-4o: 128,000 tokens |
| 87 | +- gpt-4-turbo: 128,000 tokens |
| 88 | +- gpt-3.5-turbo: 16,385 tokens |
| 89 | + |
| 90 | +### Ollama Models |
| 91 | +- llama2: 4,096 tokens |
| 92 | +- mistral: 8,192 tokens |
| 93 | +- mixtral: 32,768 tokens |
| 94 | + |
| 95 | +## Benefits |
| 96 | + |
| 97 | +- Prevents context window overflow errors |
| 98 | +- Maintains important context for agent operation |
| 99 | +- Enables longer-running agent sessions |
| 100 | +- Makes the system more robust for complex tasks |
| 101 | +- Gives agents self-awareness of resource usage |
0 commit comments