Class: SwarmSDK::ContextCompactor
- Inherits:
-
Object
- Object
- SwarmSDK::ContextCompactor
- Defined in:
- lib/swarm_sdk/context_compactor.rb,
lib/swarm_sdk/context_compactor/metrics.rb,
lib/swarm_sdk/context_compactor/token_counter.rb
Overview
ContextCompactor implements intelligent conversation history compression
The Hybrid Production Strategy combines three compression techniques:
- Tool result pruning - Aggressively truncate tool outputs (80% of tokens!)
- Checkpoint creation - LLM-generated summaries of conversation chunks
- Sliding window - Keep recent messages in full detail
Usage
# From Agent::Chat
metrics = chat.compact_context
# With options
metrics = chat.compact_context(
tool_result_max_length: 500,
checkpoint_threshold: 50,
sliding_window_size: 20,
summarization_model: "claude-3-haiku-20240307"
)
Metrics
Returns a Metrics object with compression stats:
- original_message_count / compressed_message_count
- original_tokens / compressed_tokens
- compression_ratio (e.g., 0.15 = 15% of original)
- messages_removed / messages_summarized
- time_taken
Defined Under Namespace
Classes: Metrics, TokenCounter
Constant Summary collapse
- DEFAULT_OPTIONS =
Default configuration
{ tool_result_max_length: 500, # Truncate tool results to N chars checkpoint_threshold: 50, # Create checkpoint after N messages sliding_window_size: 20, # Keep last N messages in full summarization_model: "claude-3-haiku-20240307", # Fast model for summaries preserve_system_messages: true, # Always keep system messages preserve_error_messages: true, # Always keep error messages }.freeze
Instance Method Summary collapse
-
#compact ⇒ ContextCompactor::Metrics
Compact the conversation history using hybrid production strategy.
-
#initialize(chat, options = {}) ⇒ ContextCompactor
constructor
Initialize compactor for a chat instance.
Constructor Details
#initialize(chat, options = {}) ⇒ ContextCompactor
Initialize compactor for a chat instance
48 49 50 51 52 |
# File 'lib/swarm_sdk/context_compactor.rb', line 48 def initialize(chat, = {}) @chat = chat = DEFAULT_OPTIONS.merge() @agent_name = chat.provider.respond_to?(:agent_name) ? chat.provider.agent_name : :unknown end |
Instance Method Details
#compact ⇒ ContextCompactor::Metrics
Compact the conversation history using hybrid production strategy
Returns metrics about the compression operation.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/swarm_sdk/context_compactor.rb', line 59 def compact start_time = Time.now = @chat. # Emit compression_started event LogStream.emit( type: "compression_started", agent: @agent_name, message_count: .size, estimated_tokens: TokenCounter.(), ) # Step 1: Prune tool results pruned = prune_tool_results() # Step 2: Create checkpoint if needed checkpointed = create_checkpoint_if_needed(pruned) # Step 3: Apply sliding window = apply_sliding_window(checkpointed) # Replace messages in chat () # Calculate metrics time_taken = Time.now - start_time metrics = ContextCompactor::Metrics.new( original_messages: , compressed_messages: , time_taken: time_taken, ) # Emit compression_completed event LogStream.emit( type: "compression_completed", agent: @agent_name, original_message_count: metrics., compressed_message_count: metrics., original_tokens: metrics.original_tokens, compressed_tokens: metrics.compressed_tokens, compression_ratio: metrics.compression_ratio, messages_removed: metrics., messages_summarized: metrics., time_taken: metrics.time_taken.round(3), ) metrics end |