Module: RubyLLM::Providers::OpenAIResponses::Compaction

Defined in:
lib/ruby_llm/providers/openai_responses/compaction.rb

Overview

Server-side compaction support for long-running agent sessions. Automatically compacts conversation context when token count exceeds threshold.

Class Method Summary collapse

Class Method Details

.apply_compaction(payload, params) ⇒ Hash

Apply compaction settings to payload

Parameters:

  • payload (Hash)

    The request payload

  • params (Hash)

    Additional parameters that may contain compaction options

Returns:

  • (Hash)

    Updated payload with context_management



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ruby_llm/providers/openai_responses/compaction.rb', line 26

def apply_compaction(payload, params)
  if params[:compact_threshold]
    payload[:context_management] = [
      { type: 'compaction', compact_threshold: params[:compact_threshold] }
    ]
  elsif params[:context_management]
    payload[:context_management] = params[:context_management]
  end

  payload
end

.compact_urlString

URL for explicit Responses API compaction.

Returns:

  • (String)

    The URL path



40
41
42
# File 'lib/ruby_llm/providers/openai_responses/compaction.rb', line 40

def compact_url
  'responses/compact'
end

.compaction_params(compact_threshold: 200_000) ⇒ Hash

Build context_management parameter for compaction

Parameters:

  • compact_threshold (Integer) (defaults to: 200_000)

    Token count threshold to trigger compaction (minimum 1000)

Returns:

  • (Hash)

    Parameters to merge into request payload via with_params



14
15
16
17
18
19
20
# File 'lib/ruby_llm/providers/openai_responses/compaction.rb', line 14

def compaction_params(compact_threshold: 200_000)
  {
    context_management: [
      { type: 'compaction', compact_threshold: compact_threshold }
    ]
  }
end

.input_tokens_urlString

URL for Responses API input token counting.

Returns:

  • (String)

    The URL path



46
47
48
# File 'lib/ruby_llm/providers/openai_responses/compaction.rb', line 46

def input_tokens_url
  'responses/input_tokens'
end