Skip to main content
The gateway now ships in the praisonai-bot package. praisonai serve gateway still works exactly as documented here; for a standalone install see praisonai-bot Migration.
Idle-session compaction runs in the background so a returning user resumes an already-compacted session with no latency spike, and idle transcripts stay bounded.

Quick Start

1

Enable in gateway.yaml (minimal)

This uses the defaults (idle ≥ 30 min, ≥ 8000 tokens, sweep every 5 min). Requires a durable session store — the shipped default is durable, so no extra flags are needed.
2

Tune for a busier gateway

3

Verify it fires

Grep the gateway logs for the arm line and per-session shrink lines:

How It Works

Each sweep enumerates persisted sessions off-thread, filters idle over-budget candidates, summarises them off the turn critical path, and persists a checkpoint the next user message resumes from. The sweep reuses the same Context Compaction engine, constructed as ContextCompactor(max_tokens=min_tokens, target_tokens=int(min_tokens * 0.75), strategy=CompactionStrategy.SUMMARIZE).

Configuration Options

Every knob lives under lifecycle.idle_compaction: in gateway.yaml.
Validation: non-positive values disable the feature entirely (not just the offending knob) — the gateway logs Invalid idle_compaction config; disabling: non-positive values not allowed: ....
This feature does not surface under health()["lifecycle"] — unlike Scale to Zero, verify it from the gateway logs (Gateway idle-compaction sweep armed and the per-session [idle-compaction] lines).

Requirements

The feature needs list_sessions + get_working_history + append_compaction_checkpoint. The shipped SQLite transcript store (session.store: "sqlite", default) implements all three. The legacy "file" backend may not — see Gateway Session Persistence and SQLite Transcript Store. If no durable store is bound, the gateway logs Gateway idle_compaction requires a persistent session store; disabling.
Ephemeral (session.persist: false) gateways cannot use idle_compaction — there is nothing to enumerate.

Hot Reload

Enabling or disabling idle_compaction via a gateway.yaml reload takes effect without a full process restart — _reconcile_lifecycle cancels the old task and starts the new one. See Gateway Hot Reload.

Which Compaction Surface?

Four related-but-different compaction surfaces exist; this decision diagram shows which fits which scenario.

Common Patterns

24/7 gateway with many long-lived users

Compact 30-minute-idle chats over 8k tokens every 5 minutes — cost drops and returning users pay no first-message latency.

Aggressive shrink for tight-context models

Shrink smaller transcripts more often to keep sessions under a tight model context window.

Very busy gateway (many idle sessions)

Bump scan_limit and max_per_sweep so older idle sessions aren’t starved — stores return newest-first.

Best Practices

Without persist: true and a store implementing get_working_history / append_compaction_checkpoint, the sweep disables itself. Verify with praisonai gateway start logs — a healthy start prints Gateway idle-compaction sweep armed.
Sub-minute sweeps rarely pay off on real workloads and can pressure the store. Start at the default 300 and lower only if you observe transcripts growing between sweeps.
Default cooldown_seconds: 3600 matches the default idle threshold. If you lower idle_after_seconds, lower cooldown_seconds proportionally so cooled sessions become candidates again on a sensible cadence.
This is normal on active sessions — the sweep skips the checkpoint and retries. Persistent recurrence means your idle_after_seconds is too low for the traffic pattern; increase it so live sessions aren’t candidates.

Scale to Zero

Sibling lifecycle policy — idle quiesce for serverless hosts.

Drain Trigger

Sibling lifecycle policy — epoch-safe external drain marker.

Compacted Session Resume

How the checkpoint this feature writes is consumed on the next user turn.

Context Compaction

The in-run compactor this feature reuses.

Session Persistence

The durable session store this feature requires.

Gateway Overview

How lifecycle policies wire into WebSocketGateway.