Module: Anthropic::Helpers::Tools::CompactionControl

Defined in:
lib/anthropic/helpers/tools/compaction_control.rb

Overview

Configuration for automatic conversation history compaction in tool runners.

When the cumulative token count (input + output) across all messages exceeds the threshold, the message history will be automatically summarized and compressed.

By default, a warning is printed to STDERR the first time compaction occurs for each runner instance. To silence the warning, provide an on_compact callback - when present, the warning is suppressed and you handle compaction events your way.

Examples:

Basic usage with defaults

client.beta.messages.tool_runner(
  model: "claude-sonnet-4-20250514",
  max_tokens: 4000,
  tools: [my_tool],
  messages: [...],
  compaction_control: { enabled: true }
)
# => Prints: "[anthropic-ruby] Context compaction triggered (N tokens)..."

Custom threshold and callback

client.beta.messages.tool_runner(
  # ... other params ...
  compaction_control: {
    enabled: true,
    context_token_threshold: 50_000,
    on_compact: ->(tokens_before, tokens_after) do
      logger.info "Compacted conversation: #{tokens_before} → #{tokens_after} tokens"
    end
  }
)
# => No warning printed; callback handles it

Custom model and summary prompt

client.beta.messages.tool_runner(
  # ... other params ...
  compaction_control: {
    enabled: true,
    model: "claude-3-7-sonnet-20250219",
    summary_prompt: "Summarize the conversation concisely..."
  }
)

See Also:

Instance Attribute Summary collapse

Instance Attribute Details

#context_token_thresholdInteger?



102
# File 'lib/anthropic/helpers/tools/compaction_control.rb', line 102

module CompactionControl; end

#enabledBoolean



102
# File 'lib/anthropic/helpers/tools/compaction_control.rb', line 102

module CompactionControl; end

#modelString, ...



102
# File 'lib/anthropic/helpers/tools/compaction_control.rb', line 102

module CompactionControl; end

#on_compactProc?



102
# File 'lib/anthropic/helpers/tools/compaction_control.rb', line 102

module CompactionControl; end

#summary_promptString?



102
# File 'lib/anthropic/helpers/tools/compaction_control.rb', line 102

module CompactionControl; end