> ## 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.

# Prune History • AI Agent SDK

> prune_history: Prune chat history to keep only the last N messages.

# prune\_history

<div className="flex items-center gap-2">
  <Badge color="purple">Method</Badge>
</div>

> This is a method of the [**Agent**](../classes/Agent) class in the [**agent**](../modules/agent) module.

Prune chat history to keep only the last N messages.

Useful for cleaning up large history after image analysis sessions
to prevent context window saturation.

## Signature

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
def prune_history(keep_last: int) -> int
```

## Parameters

<ParamField query="keep_last" type="int" required={false} default="5">
  Number of recent messages to keep
</ParamField>

### Returns

<ResponseField name="Returns" type="int">
  Number of messages deleted
</ResponseField>

## Source

<Card title="View on GitHub" icon="github" href="https://github.com/MervinPraison/PraisonAI/blob/main/src/praisonai-agents/praisonaiagents/agent/agent.py#L4213">
  `praisonaiagents/agent/agent.py` at line 4213
</Card>
