# offload_messages_to_backend

> **Function** in `deepagents_cli`

📖 [View in docs](https://reference.langchain.com/python/deepagents-cli/offload/offload_messages_to_backend)

Write messages to backend storage before offloading.

Appends messages as a timestamped markdown section to the conversation
history file, matching the `SummarizationMiddleware` offload pattern.

Filters out prior summary messages using the middleware's
`_filter_summary_messages` to avoid storing summaries-of-summaries.

## Signature

```python
offload_messages_to_backend(
    messages: list[Any],
    middleware: SummarizationMiddleware,
    *,
    thread_id: str,
    backend: BackendProtocol,
) -> str | None
```

## Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `messages` | `list[Any]` | Yes | Messages to offload. |
| `middleware` | `SummarizationMiddleware` | Yes | `SummarizationMiddleware` instance for filtering. |
| `thread_id` | `str` | Yes | Thread identifier used to derive the storage path. |
| `backend` | `BackendProtocol` | Yes | Backend to persist conversation history to. |

## Returns

`str | None`

File path where history was stored, `""` (empty string) if there were no
non-summary messages to offload (not an error), or `None` if the
write failed.

---

[View source on GitHub](https://github.com/langchain-ai/deepagents/blob/829909166606f8a9d9571b00da725845bad08da7/libs/cli/deepagents_cli/offload.py#L122)