Skip to content

Commit 6276bc0

Browse files
committed
feat: Improve message compaction with proactive suggestions
- 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
1 parent a5caf46 commit 6276bc0

File tree

6 files changed

+38
-23
lines changed

6 files changed

+38
-23
lines changed

docs/features/message-compaction.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@ This information is used to monitor context window usage and trigger appropriate
1414

1515
### 2. Status Updates
1616

17-
Agents receive periodic status updates (every 5 interactions) with information about:
17+
Agents receive status updates with information about:
1818
- Current token usage and percentage of the maximum
1919
- Cost so far
2020
- Active sub-agents and their status
2121
- Active shell processes and their status
2222
- Active browser sessions and their status
2323

24+
Status updates are sent:
25+
1. Every 5 agent interactions (periodic updates)
26+
2. Whenever token usage exceeds 50% of the maximum (threshold-based updates)
27+
2428
Example status update:
2529
```
2630
--- STATUS UPDATE ---
@@ -54,7 +58,7 @@ The `compactHistory` tool allows agents to compact their message history by summ
5458

5559
## Usage
5660

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:
61+
Agents are instructed to monitor their token usage through status updates and use the `compactHistory` tool when token usage approaches 50% of the maximum:
5862

5963
```javascript
6064
// Example of agent using the compactHistory tool

example-status-update.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ Active Shell Processes: 3
1919
Active Browser Sessions: 1
2020
- bs_12345: https://www.typescriptlang.org/docs/handbook/utility-types.html
2121
22-
If token usage is high (>70%), consider using the 'compactHistory' tool to reduce context size.
22+
Your token usage is high (45%). It is recommended to use the 'compactHistory' tool now to reduce context size.
2323
--- END STATUS ---
2424
```
2525

2626
## About Status Updates
2727

28-
Status updates are sent periodically to the agent (every 5 interactions) to provide awareness of:
28+
Status updates are sent to the agent (every 5 interactions and whenever token usage exceeds 50%) to provide awareness of:
2929

3030
1. **Token Usage**: Current usage and percentage of maximum context window
3131
2. **Cost**: Estimated cost of the session so far

packages/agent/src/core/toolAgent/__tests__/statusUpdates.test.ts

+4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ describe('Status Updates', () => {
4141
expect(statusMessage.content).toContain('Active Shell Processes: 0');
4242
expect(statusMessage.content).toContain('Active Browser Sessions: 0');
4343
expect(statusMessage.content).toContain('compactHistory tool');
44+
expect(statusMessage.content).toContain('If token usage gets high (>50%)');
45+
expect(statusMessage.content).not.toContain('Your token usage is high'); // Not high enough
4446
});
4547

4648
it('should include active agents, shells, and sessions', () => {
@@ -82,6 +84,8 @@ describe('Status Updates', () => {
8284

8385
// Verify
8486
expect(statusMessage.content).toContain('Token Usage: 70,000/100,000 (70%)');
87+
expect(statusMessage.content).toContain('Your token usage is high (70%)');
88+
expect(statusMessage.content).toContain('recommended to use');
8589
expect(statusMessage.content).toContain('Active Sub-Agents: 2');
8690
expect(statusMessage.content).toContain('- agent1: Task 1');
8791
expect(statusMessage.content).toContain('- agent2: Task 2');

packages/agent/src/core/toolAgent/config.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,9 @@ export function getDefaultSystemPrompt(toolContext: ToolContext): string {
146146
'',
147147
'## Resource Management',
148148
'You will receive periodic status updates showing your token usage and active background tasks.',
149-
'If your token usage approaches 70% of the maximum, use the compactHistory tool to reduce context size.',
149+
'If your token usage approaches 50% of the maximum, you should use the compactHistory tool to reduce context size.',
150150
'The compactHistory tool will summarize older messages while preserving recent context.',
151+
'Status updates are sent every 5 iterations and also whenever token usage exceeds 50% of the maximum.',
151152
'',
152153
'You prefer to call tools in parallel when possible because it leads to faster execution and less resource usage.',
153154
'When done, call the agentDone tool with your results to indicate that the sequence has completed.',

packages/agent/src/core/toolAgent/statusUpdates.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ export function generateStatusUpdate(
5151
`Active Browser Sessions: ${activeSessions.length}`,
5252
...activeSessions.map(s => `- ${s.id}: ${s.description}`),
5353
``,
54-
`If token usage is high (>70%), consider using the 'compactHistory' tool to reduce context size.`,
54+
usagePercentage >= 50
55+
? `Your token usage is high (${usagePercentage}%). It is recommended to use the 'compactHistory' tool now to reduce context size.`
56+
: `If token usage gets high (>50%), consider using the 'compactHistory' tool to reduce context size.`,
5557
`--- END STATUS ---`,
5658
].join('\n');
5759

packages/agent/src/core/toolAgent/toolAgentCore.ts

+21-17
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ export const toolAgent = async (
6161

6262
// Variables for status updates
6363
let statusUpdateCounter = 0;
64-
const STATUS_UPDATE_FREQUENCY = 5; // Send status every 5 iterations
64+
const STATUS_UPDATE_FREQUENCY = 5; // Send status every 5 iterations by default
65+
const TOKEN_USAGE_THRESHOLD = 50; // Send status update when usage is above 50%
6566

6667
for (let i = 0; i < config.maxIterations; i++) {
6768
logger.debug(
@@ -157,24 +158,27 @@ export const toolAgent = async (
157158

158159
tokenTracker.tokenUsage.add(tokenUsage);
159160

160-
// Store token information for status updates
161-
lastResponseTotalTokens = totalTokens;
162-
lastResponseMaxTokens = maxTokens;
163-
164-
// Send periodic status updates
161+
// Send status updates based on frequency and token usage threshold
165162
statusUpdateCounter++;
166-
if (statusUpdateCounter >= STATUS_UPDATE_FREQUENCY && totalTokens && maxTokens) {
167-
statusUpdateCounter = 0;
168-
169-
const statusMessage = generateStatusUpdate(
170-
totalTokens,
171-
maxTokens,
172-
tokenTracker,
173-
localContext
174-
);
163+
if (totalTokens && maxTokens) {
164+
const usagePercentage = Math.round((totalTokens / maxTokens) * 100);
165+
const shouldSendByFrequency = statusUpdateCounter >= STATUS_UPDATE_FREQUENCY;
166+
const shouldSendByUsage = usagePercentage >= TOKEN_USAGE_THRESHOLD;
175167

176-
messages.push(statusMessage);
177-
logger.debug('Sent status update to agent');
168+
// Send status update if either condition is met
169+
if (shouldSendByFrequency || shouldSendByUsage) {
170+
statusUpdateCounter = 0;
171+
172+
const statusMessage = generateStatusUpdate(
173+
totalTokens,
174+
maxTokens,
175+
tokenTracker,
176+
localContext
177+
);
178+
179+
messages.push(statusMessage);
180+
logger.debug(`Sent status update to agent (token usage: ${usagePercentage}%)`);
181+
}
178182
}
179183

180184
if (!text.length && toolCalls.length === 0) {

0 commit comments

Comments
 (0)