Context compaction and PreCompactionMemoryFlushConfig

Survive 100-turn sessions without forgetting critical facts

Every LLM has a finite context window. When a long thread approaches max_context_tokens, PraisonAI compacts older turns — and PreCompactionMemoryFlushConfig extracts and saves vital facts to durable memory *before* old messages are trimmed.

Example

from praisonai import Agent, ExecutionConfig, PreCompactionMemoryFlushConfig

flush_cfg = PreCompactionMemoryFlushConfig(enabled=True, min_turns_to_flush=6, max_flush_tokens=500)
exec_cfg = ExecutionConfig(
    context_compaction=True,
    max_context_tokens=8000,
    pre_compaction_memory_flush=flush_cfg,
)
agent = Agent(
    name='LongSessionAgent',
    instructions='Confirm project decisions concisely in one sentence.',
    execution=exec_cfg,
    output='silent',
)
print('Compaction:', exec_cfg.context_compaction, '| Flush:', flush_cfg.enabled)
print('Reply:', agent.start('Record decision: PostgreSQL 16 chosen for billing.'))

Try it Yourself

Example explained

  • PreCompactionMemoryFlushConfig(enabled=True) — Flushes important decisions/facts to long-term memory right before old turns are compressed.
  • ExecutionConfig(context_compaction=True, max_context_tokens=8000) — Prevents token-limit crashes on marathon sessions.

Why flushing BEFORE compaction matters

Naive sliding-window truncation silently drops requirements stated in Turn 1. Pre-compaction flushing preserves decisions permanently while freeing 70%+ of context tokens.

Common pitfall: leaving compaction disabled on coding or support bots

Uncompacted threads get slower and costlier on every turn until they fail with a 400 context_length_exceeded error.

Reference: documentation