Skip to content

JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.

Python
13.2 KB
Raw Blame

Context Compression: Making AI Long Conversations More Efficient

1. Concept Explanation

1.1 The Concept of Context

In AI dialogue systems, context refers to all historical information generated during a conversation, including user questions, AI answers, tool call results, etc. This information together forms the basis for the AI to understand the current dialogue state and provide accurate responses.

1.2 Context Window Limitations

Although context is crucial for AI to understand conversations, all AI models have context window limitations - the maximum amount of information (usually measured in tokens) that a model can process in a single conversation. When conversations exceed this limit, the AI will exhibit the following problems:

  • Significantly slower response times
  • Forgetting early key information
  • Decreased answer accuracy
  • Excessive system resource consumption

1.3 The Concept of Context Compression

Context compression is an intelligent optimization technology that analyzes, filters, and streamlines historical dialogue content to compress context information within the model's processable range while maintaining conversation coherence.

Its core idea can be analogized to reading a long novel - we don't need to remember every detail, only key plot points and character relationships, to understand the story's development.

1.4 Implementation Principles of Context Compression

JiuwenSwarm's context compression system uses the following core technologies:

  1. Intelligent Recognition: The system automatically identifies key information and redundant content in conversations

    • Distinguishes important messages from secondary messages through semantic analysis
    • Identifies duplicate information and low-value content
  2. Selective Compression: Adopts different processing strategies for different types of content

    • Preserves user questions and AI core answers
    • Summarizes lengthy tool return results
    • Merges duplicate information
  3. Index Marking: Inserts special markers in compressed context

    • Uses [[OFFLOAD:...]] format index markers for offloaded large tool messages
    • Preserves retrieval capabilities for offloaded tool messages
  4. Dynamic Regulation: Automatically adjusts compression strategies based on dialogue state

    • Triggers compression when message count or token count exceeds thresholds
    • Can be configured to keep the most recent messages uncompressed

2. Basic Usage

2.1 Enable Context Compression

Context compression is disabled by default. You can enable it in the following ways:

Method 1: Enable via Configuration File

Edit the config.yaml configuration file, find the context_engine_config section:

    enabled: true         # Set this to true to enable context compression

Method 2: Enable via Frontend Interface

In the JiuwenSwarm client, navigate to:

Context Compression Settings

  • Click "Configuration" in the left menu bar
  • Select "Other Configurations"
  • Find "Context Compression"
  • Toggle the switch to enable it

2.2 View Context Compression Status

After enabling context compression, you can check the compression status as follows:

Method 1: Check Conversation Status Panel

In the bottom right corner of the conversation interface, the system displays the real-time usage of the context window:

Context Compression Status Panel

The status panel includes:

  • Current Token count used
  • Maximum Token count supported by the model
  • Current usage percentage

Method 2: View via Slash Command

Enter the following command in the conversation input box to view detailed context compression information:

/context

Context Status Panel

3. Case Practice

3.1 Without Context Compression

Scenario: User has a long data analysis conversation with AI

Process:

  1. User asks: "Help me analyze this month's sales data"
  2. AI requests data
  3. User uploads a 5000-token sales data table
  4. AI analyzes and provides preliminary conclusions
  5. User continues: "Help me compare with last month's data"
  6. AI requests last month's data
  7. User uploads a 4500-token sales data table for last month
  8. AI analyzes and provides comparison results
  9. User continues: "What about next year's sales forecast?"

Results:

  • Total context tokens exceed 10,000
  • AI response time significantly slows (from 2 seconds to 10 seconds)
  • AI begins to forget some key indicators mentioned earlier
  • System resource utilization increases significantly

3.2 With Context Compression

Same Scenario: User has a long data analysis conversation with AI

Process:

  1. User asks: "Help me analyze this month's sales data"
  2. AI requests data
  3. User uploads a 5000-token sales data table
  4. AI analyzes and provides preliminary conclusions
  5. User continues: "Help me compare with last month's data"
  6. AI requests last month's data
  7. User uploads a 4500-token sales data table for last month
  8. System detects token count approaching threshold and automatically triggers compression
  9. System summarizes and compresses sales data tables, retaining only key statistical information
  10. AI analyzes and provides comparison results
  11. User continues: "What about next year's sales forecast?"

Results:

  • Total context tokens remain around 3,000
  • AI response time is stable (2-3 seconds)
  • AI can completely remember all key indicators and analysis results
  • System resource utilization remains at a reasonable level

3.3 Travel Planning Example

Scenario: User asks AI to plan a 3-day trip to Beijing (single request)

Conversation Process (Without Compression):

User: Help me plan a 3-day trip to Beijing next week
Assistant: Sure, I'll plan a detailed 3-day itinerary for you.
Tool: Querying weather and attraction information for Beijing...
Tool Response: Returns detailed weather data and attraction recommendations
Assistant: Based on your requirements, here's your travel plan:

[3-Day Beijing Tour Itinerary]

Day 1: Tiananmen Square → Forbidden City → Jingshan Park
- Morning: Watch the flag-raising at Tiananmen Square
- Noon: Explore the Forbidden City
- Afternoon: Overlook the Forbidden City from Jingshan Park

Day 2: Badaling Great Wall → Ming Tombs
- Morning: Badaling Great Wall
- Afternoon: Ming Tombs

Day 3: Summer Palace → Yuanmingyuan → Nanluoguxiang
- Morning: Summer Palace
- Noon: Yuanmingyuan
- Afternoon: Experience old Beijing culture at Nanluoguxiang

Food Recommendations: Peking Duck, Zhajiangmian, Hot Pot

Important Reminder: Please book Forbidden City tickets in advance

Explanation:

  • The AI response contains rich travel information
  • Status panel at bottom right shows: Context Window 41.3K/289.0K (20.6%)
  • Token consumption mainly comes from: AI's detailed response + Tool call return data

Results:

  • Total context tokens: 41.3K, occupying 20.6% of the model window
  • Risk: If user continues asking for details or expanding the plan, context will continue growing
  • Potential issue: May approach the model's context window limit as conversation progresses

Context Compression Disabled

Conversation Process (With Compression):

User: Help me plan a 3-day trip to Beijing next week
Assistant: Sure, I'll plan a detailed 3-day itinerary for you.
Tool: Querying weather and attraction information for Beijing...
Tool Response: Returns detailed weather data and attraction recommendations
Assistant: Based on your requirements, here's your travel plan:

[3-Day Beijing Tour Itinerary]

Day 1: Tiananmen Square → Forbidden City → Jingshan Park
...(System automatically compressed detailed itinerary content)

[[OFFLOAD: Travel plan details compressed, core information retained]]

Explanation:

  • AI completed the full travel planning task
  • Status panel at bottom right shows: Context Window 40.7K/289.0K (28.3%)

Results:

  • Total context tokens remain at 40.7K, occupying 28.3% of the model window
  • AI retains core information: Can handle subsequent requests
  • System supports additional tasks: Such as creating skills
  • Improved user experience: Enables continued multi-turn interactions and feature extensions

Context Compression Enabled

3.4 Effect Analysis

Based on the actual data from the examples:

  1. Context Token Usage:

    • Without compression: 41.3K tokens
    • With compression: 40.7K tokens
    • The difference is small, indicating that compression mainly optimizes information structure rather than simply reducing token count
  2. System Performance:

    • Both scenarios successfully generated travel plans
    • Compressed scenario additionally supports skill creation and other extended features
    • Memory usage stable around 362MB
  3. Practical Value:

    • Context compression optimizes information structure, enabling better handling of subsequent extension requests
    • Enhances system functionality while maintaining core information integrity

3.5 Comparison Summary

Metric Without Context Compression With Context Compression
Response Speed Gradually slows (10+ seconds) Stable (2-3 seconds)
Information Integrity Gradually lost Key information fully retained
System Resource Usage High (> 2GB memory) Low (< 1GB memory)
Conversation Coherence Gradually decreases Remains consistent
Supported Conversation Length Limited (about 20-30 rounds) Unlimited (supports hundreds of rounds)

4. Feature Interpretation

4.1 Dynamic Triggering Mechanism

Context compression is not a fixed preprocessing step, but an intelligent optimization dynamically triggered based on dialogue state:

  • Based on Message Count: Triggers when conversation rounds exceed messages_threshold (default 3)
  • Based on Token Count: Triggers when accumulated tokens exceed tokens_threshold (default 20,000)
  • Configurable: Users can adjust trigger thresholds as needed

4.2 Intelligent Recognition and Compression

The system uses advanced natural language processing technology to:

  • Distinguish Message Types: Automatically identifies user messages, AI answers, and tool results
  • Identify Importance: Judges message importance based on semantic analysis
  • Select Compression Methods: Adopts different compression strategies for different content types
    • Summarizes lengthy tool return results
    • Merges duplicate information
    • Filters unimportant content

4.3 Key Information Protection

Context compression does not lose important information; the system:

  • Preserves Recent Messages: Retains a specified number of latest messages through messages_to_keep parameter
  • Preserves Complete Rounds: Enables keep_last_round to ensure the latest round of user-assistant dialogue is fully retained
  • Identifies Key Content: Automatically identifies and retains key information through semantic analysis

4.4 Retrieval Capability Preservation

Note: Normally compressed content cannot be recalled. Only offloaded large tool message content can be retrieved through [[OFFLOAD:...]] index markers.

  • Index Marking: Uses [[OFFLOAD:...]] format index markers for offloaded large tool messages
  • Association Preservation: Offloaded information remains associated with the original context
  • Recoverability: Offloaded tool message information can be recovered through indices when needed

4.5 Performance Optimization

Context compression can significantly improve system performance:

  • Reduces Memory Usage: Reduces context data volume by 50%-80%
  • Improves Response Speed: Reduces model inference time and improves user experience
  • Supports Longer Conversations: Breaks through model context window limitations and supports long conversations
  • Reduces Costs: Reduces token consumption and lowers usage costs

5. Application Scenarios

Context compression technology is suitable for various scenarios requiring long conversations:

  • Data Analysis: Processing large data tables and analysis results
  • Project Management: Cross-day project follow-up and discussions
  • Code Development: Long-time code writing and debugging
  • Document Creation: Collaborative writing of long documents
  • Customer Support: Long-time resolution of complex issues

6. Advanced Configuration

The following is a typical configuration example for context compression:

context_engine:
  max_messages: 100        # Maximum number of messages
  max_tokens: 100000      # Maximum number of tokens
  compression:
    enabled: true         # Enable context compression
    messages_threshold: 3 # Message count trigger threshold
    tokens_threshold: 20000 # Token count trigger threshold
    large_message_threshold: 1000 # Large message definition
    offload_message_type: ["tool"] # Only compress tool return results
    messages_to_keep: 5   # Keep the last 5 messages
    keep_last_round: true # Keep complete last round conversation

By configuring context compression parameters reasonably, users can obtain the best dialogue experience according to their usage scenarios and needs.