Module: RubyPi

Defined in:
lib/ruby_pi.rb,
lib/ruby_pi/errors.rb,
lib/ruby_pi/version.rb,
lib/ruby_pi/llm/model.rb,
lib/ruby_pi/agent/core.rb,
lib/ruby_pi/agent/loop.rb,
lib/ruby_pi/llm/gemini.rb,
lib/ruby_pi/llm/openai.rb,
lib/ruby_pi/agent/state.rb,
lib/ruby_pi/agent/events.rb,
lib/ruby_pi/agent/result.rb,
lib/ruby_pi/llm/fallback.rb,
lib/ruby_pi/llm/response.rb,
lib/ruby_pi/tools/result.rb,
lib/ruby_pi/tools/schema.rb,
lib/ruby_pi/configuration.rb,
lib/ruby_pi/llm/anthropic.rb,
lib/ruby_pi/llm/tool_call.rb,
lib/ruby_pi/tools/executor.rb,
lib/ruby_pi/tools/registry.rb,
lib/ruby_pi/extensions/base.rb,
lib/ruby_pi/llm/stream_event.rb,
lib/ruby_pi/tools/definition.rb,
lib/ruby_pi/context/transform.rb,
lib/ruby_pi/llm/base_provider.rb,
lib/ruby_pi/context/compaction.rb

Overview

lib/ruby_pi/context/compaction.rb

RubyPi::Context::Compaction — Token estimation and context window management.

When the conversation history grows too large for the model’s context window, Compaction summarizes older messages while preserving the most recent ones. Token estimation uses a simple heuristic (~4 characters per token) to avoid external tokenizer dependencies. When compaction triggers, older messages are replaced with a single summary message generated by the LLM, and a :compaction event is emitted.

Defined Under Namespace

Modules: Agent, Context, Extensions, LLM, Schema, Tool, Tools Classes: ApiError, AuthenticationError, Configuration, Error, NotImplementedError, ProviderError, RateLimitError, TimeoutError

Constant Summary collapse

VERSION =

The current version of the RubyPi gem, following Semantic Versioning.

"0.1.0"

Class Method Summary collapse

Class Method Details

.configurationRubyPi::Configuration

Returns the global configuration object.

Returns:



57
58
59
# File 'lib/ruby_pi.rb', line 57

def configuration
  @configuration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ RubyPi::Configuration

Yields the global configuration object to a block for mutation.

Examples:

Configure API keys

RubyPi.configure do |config|
  config.openai_api_key = ENV["OPENAI_API_KEY"]
  config.max_retries = 5
end

Yields:

Returns:



71
72
73
74
# File 'lib/ruby_pi.rb', line 71

def configure
  yield(configuration) if block_given?
  configuration
end

.reset_configuration!void

This method returns an undefined value.

Resets the global configuration to default values.



79
80
81
# File 'lib/ruby_pi.rb', line 79

def reset_configuration!
  @configuration = Configuration.new
end