Skip to content

feat: proactive context compaction for agents #1

Description

@rmdevpro

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):

  1. Keep newest 10% of context as raw messages
  2. Summarize everything older into a summary targeting ≤5% of the context window (token budget passed to model)
  3. Prepend summary as a system message
  4. 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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions