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

# Compression Events

> Stream CompressionStarted and CompressionCompleted events from an agent using compress_tool_results=True.

Test script to verify compression events are working correctly.

```python compression_events.py theme={null}
"""
Compression Events
=============================

Test script to verify compression events are working correctly.
"""

import asyncio

from agno.agent import Agent
from agno.models.openai import OpenAIResponses
from agno.run.agent import RunEvent
from agno.tools.duckduckgo import DuckDuckGoTools

# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------
agent = Agent(
    model=OpenAIResponses(id="gpt-5-mini"),
    tools=[DuckDuckGoTools()],
    description="Specialized in tracking competitor activities",
    instructions="Use the search tools and always use the latest information and data.",
    compress_tool_results=True,
)


async def main():
    print("--- Running agent with compression events ---")

    stream = agent.arun(
        """
        Research recent activities for these AI companies:
        1. OpenAI - latest news
        2. Anthropic - latest news
        3. Google DeepMind - latest news
        """,
        stream=True,
        stream_events=True,
    )

    async for chunk in stream:
        if chunk.event == RunEvent.run_started.value:
            print(f"[RunStarted] model={chunk.model}")

        elif chunk.event == RunEvent.model_request_started.value:
            print(f"[ModelRequestStarted] model={chunk.model}")

        elif chunk.event == RunEvent.model_request_completed.value:
            print(
                f"[ModelRequestCompleted] tokens: in={chunk.input_tokens}, out={chunk.output_tokens}"
            )

        elif chunk.event == RunEvent.tool_call_started.value:
            print(f"[ToolCallStarted] {chunk.tool.tool_name}")

        elif chunk.event == RunEvent.tool_call_completed.value:
            print(f"[ToolCallCompleted] {chunk.tool.tool_name}")

        elif chunk.event == RunEvent.compression_started.value:
            print("[CompressionStarted]")

        elif chunk.event == RunEvent.compression_completed.value:
            print(
                f"[CompressionCompleted] compressed={chunk.tool_results_compressed} results"
            )
            print(
                f"  Original: {chunk.original_size} chars -> Compressed: {chunk.compressed_size} chars"
            )
            if chunk.original_size and chunk.compressed_size:
                ratio = (1 - chunk.compressed_size / chunk.original_size) * 100
                print(f"  Compression ratio: {ratio:.1f}% reduction")

        elif chunk.event == RunEvent.run_completed.value:
            print("[RunCompleted]")


# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------
if __name__ == "__main__":
    asyncio.run(main())
```

## Run the Example

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

**1. Install dependencies**

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

**2. Export your OpenAI API key**

```bash Mac/Linux theme={null}
export OPENAI_API_KEY="your_openai_api_key_here"
```

```bash Windows theme={null}
$Env:OPENAI_API_KEY="your_openai_api_key_here"
```

**3. Run the example**

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

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

Full source: [cookbook/02\_agents/14\_advanced/compression\_events.py](https://github.com/agno-agi/agno/blob/v3.0.4/cookbook/02_agents/14_advanced/compression_events.py)


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