module LittleGhost::Agent::ContextManagement

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.