Module: LittleGhost::Agent::ContextManagement

Included in:
LittleGhost::Agent
Defined in:
lib/little_ghost/agent/context_management.rb

Overview

Keep long conversations within the model's available context window. The capability summarizes older turns while preserving trusted instructions and recent messages.

class CustomerSupportAgent < LittleGhost::Agent
manage_context compression_threshold: 0.75,
  preserve_recent_messages: 12
end

As a support thread reaches the threshold, the next model request contains a generated summary and targets retaining its 12 most recent conversation messages. System and developer messages remain intact, and tool-use/result pairs are never split merely to hit the requested count.

Context management is inactive until manage_context is declared. The configured window is a fallback: provider metadata takes precedence when it advertises a positive context-window size. Compaction uses the current model with the request's settings, cancellation token, and deadline.

Proactive compaction failures leave the original request unchanged and emit diagnostic instrumentation. A provider context-overflow error triggers one compaction replacement through the model-error callback; cancellation, deadlines, and cleanup failures still escape as control flow.

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

DEFAULT_CONTEXT_WINDOW_TOKENS =

:nodoc:

200_000
DEFAULT_COMPRESSION_THRESHOLD =

:nodoc:

0.85
DEFAULT_SUMMARY_RATIO =

:nodoc:

0.3
DEFAULT_PRESERVE_RECENT_MESSAGES =

:nodoc:

10
ESTIMATED_CHARS_PER_TOKEN =

:nodoc:

4
OUTPUT_LIMIT_STOP_REASONS =

:nodoc:

%i[max_tokens limit_output_tokens limit_total_tokens limit_turns].freeze
SUMMARIZATION_PROMPT =

:nodoc:

<<~PROMPT # :nodoc:
You are a conversation summarizer. Provide a concise summary of the conversation history.

Format requirements:
- Create a structured, concise summary in bullet-point format.
- Do not respond conversationally, address the user directly, or comment on tool availability.
- Preserve key topics, questions, significant tool executions and results, code or technical information, and key insights.
- Do not assume tool executions failed unless otherwise stated.
- Write the summary in the third person.

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object

:nodoc:



49
50
51
52
# File 'lib/little_ghost/agent/context_management.rb', line 49

def self.included(base) # :nodoc:
  base.extend(ClassMethods)
  base.class_attribute :context_management_configuration_value
end