> ## Documentation Index
> Fetch the complete documentation index at: https://praison.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# compaction • AI Agent SDK

> Auto Context Compaction Module for PraisonAI Agents.

# compaction

<Badge color="blue">AI Agent</Badge>

Auto Context Compaction Module for PraisonAI Agents.

Provides automatic context management for long conversations:

* Anti-injection framing (prevents instruction hijacking)
* Structured summary templates with organized sections
* Iterative updates across multiple compactions
* Message summarization with LLM integration
* Context window management
* Token counting and limits
* Intelligent message pruning

Zero Performance Impact:

* All imports are lazy loaded via **getattr**
* Compaction only runs when needed
* No overhead when context is within limits

Usage:
from praisonaiagents import Agent, CompactionConfig
from praisonaiagents.compaction import COMPACTION\_PREFIX, SUMMARY\_TEMPLATE

# Agent-centric usage (recommended)

agent = Agent(
name="Assistant",
context=True  # Anti-injection + structured template enabled
)

# With custom configuration

agent = Agent(
name="Assistant",
context=CompactionConfig(
structured\_template=True,
compaction\_prefix="\[REFERENCE ONLY] Use as background...",
iterative\_update=True
)
)

# Access module constants

print(COMPACTION\_PREFIX)  # Default anti-injection prefix
print(SUMMARY\_TEMPLATE)   # Structured template with placeholders

## Import

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
from praisonaiagents import compaction
```
