GitHub
Examples

Pruning Engine

Manage memory lifecycle with intelligent forgetting policies.

Lifecycle Module
Scenario: Forgetting old weather reports

When to Use

Infinite memory is expensive and noisy. Use the Pruning Engine to automatically remove low-value information based on policies. Perfect for:

  • Removing old "small talk" (weather, greetings)
  • Enforcing data retention policies (e.g., "Delete financial data after 7 years")
  • Cleaning up temporary facts

Key Operations

set_ttl()

Define retention policy for item types

analyze()

Dry-run to see what would be deleted

Code Example

We set a policy that "weather reports" should be forgotten after 1 day, while "medical history" is kept for a year.

# 1. Set Retention Policies (TTL)
client.memory.pruning.set_ttl(
item_type="weather_report",
ttl_seconds=86400, # 1 day
priority=0 # Low priority
)
client.memory.pruning.set_ttl(
item_type="family_medical_history",
ttl_seconds=31536000, # 1 year
priority=10 # High priority
)
# 2. Run Pruning Analysis (Dry Run)
report = client.memory.pruning.analyze(
kg_name="KG_Companion_Demo",
criteria={"expired_only": True},
dry_run=True
)
print(f"Candidates for deletion: {report['candidates_count']}")

Key Takeaways

  • Pruning prevents context pollution. Too much irrelevant memory confuses the LLM.
  • Structure your data with item_type (e.g., 'weather', 'medical') to enable granular pruning policies.
  • Always run analyze() (dry run) before execute() in production.