Summary
LibreChat's agents endpoint currently uses a 'discard' context strategy — oldest messages are silently dropped once the context window fills. No summarization is triggered. The summarizeMessages() method on BaseClient throws if called; the summary prompts in summaryPrompts.js are unused scaffolding.
This issue tracks the design and implementation of proactive context compaction for agents.
Problem
- Context grows turn-by-turn until it hits
maxContextTokens
- Oldest messages are then discarded silently — no summarization occurs
- Every prompt reconstructs the full context from MongoDB (no persistent state between turns)
- No cache-friendly compaction means near-full-window token costs on every turn once the window fills
Design
Algorithm
Triggered when current context token count reaches the configured threshold (default 80% of maxContextTokens):
- Keep newest 10% of context as raw messages
- Summarize everything older into a summary targeting ≤5% of the context window (token budget passed to model)
- Prepend summary as a system message
- Result: ~15% used (5% summary + 10% raw), ~85% free after each compaction
Agent Config Fields (schema addition)
| Field |
Type |
Default |
Description |
autoCompact |
boolean |
false |
Enable proactive compaction |
compactThreshold |
number |
80 |
% of context window that triggers compaction |
UI: toggle + plain number input (no slider) in the agent config screen.
Slash Command
/compact — manually triggers compaction on the current conversation regardless of threshold. Intercepted before submission reaches the model; calls POST /api/conversations/:id/compact.
Files Affected
Backend
api/server/controllers/agents/client.js — implement summarizeMessages(), add proactive threshold logic, change contextStrategy
api/app/clients/BaseClient.js — add proactive threshold check in handleContextStrategy
api/app/clients/prompts/summaryPrompts.js — update prompt to accept token budget constraint
- New route:
POST /api/conversations/:id/compact
Schema
packages/data-provider/src/types/agents.ts (or equivalent) — add autoCompact, compactThreshold fields
Frontend
- Agent config screen — autocompact toggle + number input for threshold
- Message submission handler —
/compact command interceptor
client/src/locales/en/translation.json — localization strings
Migrated from jmorrissette-RMDC/LibreChat
Summary
LibreChat's agents endpoint currently uses a
'discard'context strategy — oldest messages are silently dropped once the context window fills. No summarization is triggered. ThesummarizeMessages()method onBaseClientthrows if called; the summary prompts insummaryPrompts.jsare unused scaffolding.This issue tracks the design and implementation of proactive context compaction for agents.
Problem
maxContextTokensDesign
Algorithm
Triggered when current context token count reaches the configured threshold (default 80% of
maxContextTokens):Agent Config Fields (schema addition)
autoCompactbooleanfalsecompactThresholdnumber80UI: toggle + plain number input (no slider) in the agent config screen.
Slash Command
/compact— manually triggers compaction on the current conversation regardless of threshold. Intercepted before submission reaches the model; callsPOST /api/conversations/:id/compact.Files Affected
Backend
api/server/controllers/agents/client.js— implementsummarizeMessages(), add proactive threshold logic, changecontextStrategyapi/app/clients/BaseClient.js— add proactive threshold check inhandleContextStrategyapi/app/clients/prompts/summaryPrompts.js— update prompt to accept token budget constraintPOST /api/conversations/:id/compactSchema
packages/data-provider/src/types/agents.ts(or equivalent) — addautoCompact,compactThresholdfieldsFrontend
/compactcommand interceptorclient/src/locales/en/translation.json— localization stringsMigrated from jmorrissette-RMDC/LibreChat