> ## Documentation Index
> Fetch the complete documentation index at: https://agno-v2.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# LiteLLM Memory

> Keep the last three runs in context and inspect stored session messages with a LiteLLM agent.

```python memory.py theme={null}
"""
Litellm Memory
==============

Cookbook example for `litellm/memory.py`.
"""

from agno.agent import Agent
from agno.models.litellm import LiteLLM
from rich.pretty import pprint

# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------

agent = Agent(
    model=LiteLLM(id="gpt-5.6-luna"),
    # Set add_history_to_context=true to add the previous chat history to the context sent to the Model.
    add_history_to_context=True,
    # Number of historical responses to add to the messages.
    num_history_runs=3,
    description="You are a helpful assistant that always responds in a polite, upbeat and positive manner.",
)

# -*- Create a run
agent.print_response("Share a 2 sentence horror story", stream=True)
# -*- Print the messages in the memory
pprint(
    [m.model_dump(include={"role", "content"}) for m in agent.get_session_messages()]
)

# -*- Ask a follow up question that continues the conversation
agent.print_response("What was my first message?", stream=True)
# -*- Print the messages in the memory
pprint(
    [m.model_dump(include={"role", "content"}) for m in agent.get_session_messages()]
)

# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------

if __name__ == "__main__":
    pass
```

## Run the Example

<Snippet file="create-venv-step.mdx" />

**1. Install dependencies**

```bash theme={null}
uv pip install -U agno litellm
```

**2. Export your LiteLLM API key**

```bash Mac/Linux theme={null}
export LITELLM_API_KEY="your_litellm_api_key_here"
```

```bash Windows theme={null}
$Env:LITELLM_API_KEY="your_litellm_api_key_here"
```

**3. Run the example**

Save the code above as `memory.py`, then run:

```bash theme={null}
python memory.py
```

Full source: [cookbook/90\_models/litellm/memory.py](https://github.com/agno-agi/agno/blob/v3.0.4/cookbook/90_models/litellm/memory.py)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.