Codumentor logo Codumentor

Context Management Plugin

The Context Management Plugin proactively maintains multi-resolution state descriptions of conversations and dynamically compresses old context using semantic drift detection with cascading compression.

Overview

Unlike the reactive Context Summarization Plugin (which only activates on context overflow), this plugin:

Cascading Compression Model

The plugin uses a sophisticated multi-level compression scheme:

Context sent to LLM:
┌────────────────────────────────────────────────────────┐
│ [DISTANT HISTORY - 15 tokens, turns 1-C15]             │  ← Oldest, most compressed
│ Brief summary of earliest conversation...              │
├────────────────────────────────────────────────────────┤
│ [OLDER HISTORY - 50 tokens, turns 1-C50]               │
│ Slightly more detailed summary...                      │
├────────────────────────────────────────────────────────┤
│ [PAST HISTORY - 250 tokens, turns 1-C250]              │
│ Moderate detail summary...                             │
├────────────────────────────────────────────────────────┤
│ [RECENT HISTORY - 1000 tokens, turns 1-C1000]          │
│ Detailed summary of recent past...                     │
├────────────────────────────────────────────────────────┤
│ [Raw messages from turn C1000+1 onwards]               │  ← Most recent, uncompressed
│ User: ...                                              │
│ Assistant: ...                                         │
└────────────────────────────────────────────────────────┘

When a new topic shift (drift) is detected:

  1. Previous 1000-token compaction point → cascades to 250-token level
  2. Previous 250-token compaction point → cascades to 50-token level
  3. Previous 50-token compaction point → cascades to 15-token level
  4. New drift turn → becomes new 1000-token compaction point

This provides increasingly detailed context as you approach the present conversation.

Configuration

Basic Configuration

plugins:
  - module: "codumentor.plugins.context_management"
    class: "ContextManagementPlugin"
    priority: 5
    args:
      enabled: true
      target_token_levels: [15, 50, 250, 1000]
      provide_retrieval_tool: true
      min_turns_before_compaction: 3
      compaction_token_threshold: 1000

Configuration Options

OptionTypeDefaultDescription
enabledbooleantrueEnable/disable the plugin
target_token_levelslist[15, 50, 250, 1000]Token levels for state descriptions
provide_retrieval_toolbooleantrueInject history retrieval tool
state_store_pathstringnullCustom path for state storage
state_generation_modelstringnullModel for state generation (default: main model)
drift_detection_modelstringnullModel for drift detection (default: main model)
min_turns_before_compactioninteger3Minimum turns before compaction can occur
compaction_token_thresholdinteger1000Token threshold to trigger compaction
drift_window_sizeinteger10Number of states to analyze for drift
estimate_sufficient_tokensbooleantrueEstimate minimum tokens needed for adequate summary
ui_verbositystring"normal"Controls which events show as UI cards (see below)

UI Verbosity Levels

The ui_verbosity setting controls which context management events appear as cards in the UI:

LevelEvents ShownUse Case
"all"state_updated, drift_detected, context_compactedDebugging, understanding plugin behavior
"normal"drift_detected, context_compactedDefault - see significant events
"minimal"drift_detectedOnly major topic shifts
"off"(none)Silent operation

Example:

plugins:
  - module: "codumentor.plugins.context_management"
    class: "ContextManagementPlugin"
    args:
      ui_verbosity: "minimal"  # Only show topic shifts

Sufficient Tokens Estimation

When estimate_sufficient_tokens is enabled (default), the plugin asks the LLM to estimate the minimum tokens needed for an adequate summary of each turn.

The Key Insight

Different conversations have different information density:

The sufficient tokens estimate answers: "What's the minimum size where a reader would understand the key points AND know what details they might want to retrieve?"

This is "retrieval-aware summarization" - the summary should be self-aware of its incompleteness.

How It's Used

  1. Stored per turn: Each turn tracks its sufficient_tokens estimate
  2. Smart level selection: When building cascading summaries, if a smaller level is adequate (based on sufficient_tokens), it's used instead - saving tokens while maintaining adequacy
  3. Included in events: The state_updated event includes sufficientTokens for UI display

Example

Turn 1: "Help me fix a typo" → sufficient_tokens=25 → use 50-token summary
Turn 2: "Explain the auth system" → sufficient_tokens=180 → use 250-token summary
Turn 3: "Design a new microservice" → sufficient_tokens=600 → use 1000-token summary

Retrieval Hints

The plugin also extracts retrieval hints - short descriptions of what details are available but not included in compressed summaries.

Purpose

Hints help readers know what they can ask about. A summary might say "discussed authentication" but hints tell you: "JWT token expiry, refresh flow, error codes" - specific topics you can retrieve if needed.

How It Works

  1. Extracted per turn: After generating summaries, the LLM identifies 2-5 retrievable details
  2. Stored alongside states: Hints are persisted in the state store
  3. Included in cascading summaries: When context is compacted, hints from all compaction points are collected and shown:
[CONVERSATION HISTORY - CASCADING SUMMARIES]
...summaries...

[RETRIEVABLE DETAILS: JWT token handling, error response format, rate limiting config]

[END SUMMARIES - Conversation continues below]

This makes the summary explicitly self-aware of its own incompleteness - the reader knows exactly what's been compressed away.

How It Works

1. Per-Turn State Generation

After each Q/A exchange, the plugin:

  1. Captures the user message and assistant response
  2. Loads previous cumulative state descriptions
  3. Generates updated state descriptions at all token levels (in parallel)
  4. Stores the new states for future reference

2. Drift Detection

The plugin analyzes the sequence of 15-token state descriptions to detect semantic shifts:

[1-5]: User discussing Python decorators and class design
[1-6]: User continuing Python decorators discussion
[1-7]: User now asking about database schema design  ← Drift detected!
[1-8]: User exploring PostgreSQL indexing strategies

When significant drift is detected, compaction points cascade down to compress older content.

3. Context Substitution

Before each LLM call, if compaction is beneficial:

  1. Estimates tokens for messages up to the highest compaction point
  2. If exceeds threshold, builds cascading summary header
  3. Replaces prefix messages with multi-level summaries
  4. Preserves raw messages for recent context

4. History Retrieval Tool

The plugin optionally injects a retrieve_conversation_history tool:

Tool: retrieve_conversation_history
Arguments:
  - start_turn: First turn to retrieve (1-indexed)
  - end_turn: Last turn to retrieve (inclusive)
  - detail_level: "brief" (50 tokens), "medium" (250), "full" (1000), or "raw"

This allows the agent to fetch more details about earlier conversation when needed.

Storage

State is persisted per session:

{state_store_path}/{session_id}/
├── context_state.json    # State descriptions and compaction points
└── raw_messages.jsonl    # Original messages for retrieval

State Format

{
  "session_id": "abc123",
  "compaction_points": {
    "15": 3,
    "50": 5,
    "250": 8,
    "1000": 12
  },
  "current_turn": 15,
  "states": {
    "1": {"15": "...", "50": "...", "250": "...", "1000": "..."},
    "2": {"15": "...", "50": "...", "250": "...", "1000": "..."}
  },
  "sufficient_tokens": {
    "1": 75,
    "2": 120
  },
  "retrieval_hints": {
    "1": ["initial setup", "config options"],
    "2": ["auth flow details", "error handling approach"]
  }
}

UI Events

The plugin emits the following UI events (filtered by ui_verbosity):

EventDescriptionVerbosity
plugin.context_management.state_updatedState descriptions generated (includes sufficientTokens, retrievalHints)"all" only
plugin.context_management.drift_detectedSemantic drift detected, compaction points cascaded"all", "normal", "minimal"
plugin.context_management.context_compactedContext was compacted before LLM call"all", "normal"

Examples

Example 1: Long Technical Conversation

A conversation that starts with Python, shifts to database design, then to deployment:

Turns 1-5: Python code review → compressed to 15 tokens
Turns 1-10: Python + early DB design → compressed to 50 tokens
Turns 1-15: Python + DB design + schema → compressed to 250 tokens
Turns 1-20: Recent DB + deployment planning → 1000-token summary
Turns 21-25: Current deployment discussion → raw messages

Example 2: Disabled Retrieval Tool

plugins:
  - module: "codumentor.plugins.context_management"
    class: "ContextManagementPlugin"
    args:
      enabled: true
      provide_retrieval_tool: false  # Don't inject tool

Example 3: Custom Token Levels

plugins:
  - module: "codumentor.plugins.context_management"
    class: "ContextManagementPlugin"
    args:
      target_token_levels: [10, 100, 500, 2000]  # Custom levels
      compaction_token_threshold: 2000  # Higher threshold

Comparison with Context Summarization

FeatureContext SummarizationContext Management
TriggerContext overflowEvery turn
ApproachReactive truncationProactive summarization
StateNonePersistent multi-level
CompressionSingle levelCascading multi-level
Drift detectionNoYes, LLM-based
RetrievalNoYes, via tool
Adaptive sizingNoYes, via sufficient_tokens
Self-aware gapsNoYes, via retrieval hints

Use Context Summarization for simple overflow handling.
Use Context Management for sophisticated long-conversation support.

Testing

Run tests with:

python -m pytest tests/test_plugins/test_context_management_plugin.py -v

See Also