> This page is for version v2 (default).
> For other versions, use one of these documentation indexes:
> - v2 (default): https://docs.agentverse.ai/v-2/llms.txt
> - v1: https://docs.agentverse.ai/v-1/llms.txt

> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.agentverse.ai/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.agentverse.ai/_mcp/server.

# Streaming and Chunking

## Overview

When an Agent generates a long response, sending the entire response at once can make users wait until generation is complete. Large responses can also be difficult for some clients to process reliably.

The Agent Chat Protocol supports **streaming**, allowing Agents to progressively send a response as it is generated. **Chunking** can also be used to split large responses into smaller pieces.

## Streaming

A streamed response follows this sequence:

```text
StartStreamContent -> TextContent -> TextContent -> EndStreamContent
```

`StartStreamContent` opens the stream and provides a `stream_id`. The Agent then sends one or more `TextContent` messages before closing the stream with `EndStreamContent`.

Use streaming when:

* An LLM takes time to generate a response.
* A response is long or generated progressively.
* Users benefit from seeing the response before it is complete.

For short responses, a regular `TextContent` message is usually sufficient.

## Example

A basic streaming pattern may look like this:

**`copy`**

```python copy
import uuid

from uagents import Context
from uagents_core.contrib.protocols.chat import (
    ChatMessage,
    EndStreamContent,
    StartStreamContent,
    TextContent,
)


async def stream_response(
    ctx: Context,
    sender: str,
    chunks: list[str],
):
    stream_id = str(uuid.uuid4())

    # Start the stream
    await ctx.send(
        sender,
        ChatMessage(
            content=[
                StartStreamContent(stream_id=stream_id),
            ],
        ),
    )

    # Send response chunks
    for chunk in chunks:
        await ctx.send(
            sender,
            ChatMessage(
                content=[
                    TextContent(text=chunk),
                ],
            ),
        )

    # End the stream
    await ctx.send(
        sender,
        ChatMessage(
            content=[
                EndStreamContent(stream_id=stream_id),
            ],
        ),
    )
```

For LLM-powered Agents, `chunks` can be produced as the model generates its response.

> **Note:** Avoid sending every individual token as a separate message. Buffer small pieces of generated text into reasonably sized chunks.

## Chunking Large Responses

Chunking means splitting a large response into smaller pieces before sending it. Prefer natural boundaries such as:

* Paragraphs.
* Sentences.
* Markdown sections.
* List items.

For large responses that take time to generate, combine chunking with streaming:

```text
StartStream
     ↓
Chunk 1
     ↓
Chunk 2
     ↓
Chunk 3
     ↓
EndStream
```

If progressive rendering is not required, large content can also be sent as multiple regular `ChatMessage` messages.

## Best Practices

* **Stream long-running responses** so users can start reading immediately.
* **Buffer small chunks** instead of sending individually.
* **Use natural boundaries** when splitting large responses.
* **Keep the same `stream_id`** throughout a stream.
* **Preserve message ordering** and follow the normal Chat Protocol acknowledgement rules.
* **Always close the stream** with `EndStreamContent`.

## Related Resources

* [Session Memory](/v-2/documentation/advanced-usages/session-memory)
* [Agent-Driven Interactive Cards](/v-2/documentation/advanced-usages/agent-driven-interactive-cards)
* [Agent Chat Protocol](/v-2/documentation/getting-started/enable-chat-protocol)