Context compaction
Long-running agents eventually hit the model's context window. lite-agent's compaction capability shrinks the conversation without breaking tool_call/tool_result pairing — every cut is turn-aligned — and it comes in two composable layers: a deterministic toolkit you wire in as middleware, and an automatic ContextEngine that manages context pressure for you.
Enabling compaction
Wire a compactor into the middleware pipeline with compaction(), and add reactiveCompaction() as the safety net:
compaction(compactor) runs the compactor in beforeModel and swaps in the result, emitting a compaction event only when messages actually changed. reactiveCompaction() catches a context-overflow rejection from the provider and retries with a trimmed context — only if nothing has streamed yet.
When you build on @lite-agent/sdk, you usually configure nothing: the SDK passes context: {} by default, so the ContextEngine below is already active. The low-level core keeps raw-message behavior when context is omitted.
The toolkit
Deterministic passes and ready-made Compactors, all exported from @lite-agent/core:
Every compactor implements the Compactor strategy — maybeCompact(messages, usage, instructions?) → CompactResult. The optional instructions steers manual compaction (like Claude Code's /compact <instructions>); structural compactors ignore it.
The ContextEngine
The ContextEngine is automatic, always-on context management, created by the kernel when context is not false. It owns a durable event log and projects a ContextView per request, escalating through internal pressure levels (externalize → normalize → select → project → recover) and reporting each decision as one context_status event. It prefers provider-native capabilities (clearToolUses, clearThinking, compact) when the ModelProvider exposes them, and accepts planner / archive hooks via KernelContextOptions.
Create one standalone with createContextEngine, or project a view yourself with projectContext.
See also
- The nine strategies — the
Compactorstrategy interface and custom compactor scenarios. - Model providers — which providers expose native context-editing capabilities.
- Session persistence — the event log the ContextEngine builds on.
- Tool-call codecs — how history is encoded after compaction.