Compaction Forecasting
The agent loop monitors token consumption trend and automatically saves memory 5 turns before the session would hit the compaction threshold. This ensures context is preserved without waiting for an abrupt compaction event.
How It Works
- After each turn, the loop estimates average tokens per turn from session history
- It calculates
turnsUntilCompaction = (threshold − currentTokens) / avgTokensPerTurn - Threshold is 70% of
maxContextTokens - If
turnsUntilCompactionis between 1 and 5, an early memory save is triggered (postTurnAutoSave) - No configuration required — it runs automatically
Algorithm
typescript
const compactionThreshold = agentConfig.model.maxContextTokens * 0.7
const avgTokensPerTurn = currentMsgTokens / Math.max(1, allMessages.length / 2)
const turnsUntilCompaction = Math.floor(
(compactionThreshold - currentMsgTokens) / avgTokensPerTurn
)
if (turnsUntilCompaction > 0 && turnsUntilCompaction <= 5) {
postTurnAutoSave(uid, userMessage, revisedContent)
}ℹCompaction forecasting is additive — it triggers an early save but does not prevent normal compaction from occurring at 85% of
maxContextTokens.