借助消息线程加快长时间运行的智能体
原题Make long-running agents faster with message threads
推荐理由Claude 消息线程通过增量发送会话历史降低长任务首词延迟,适合 Agent 开发者核对接入方式、短期缓存期限与 token 计费保持不变的关系。
读前速览
消息线程是 Messages API 的一项新功能,通过只发送新消息而非完整对话历史,降低长时间运行的智能体会话延迟;对于包含 500k 到 1M 个 token 的会话,TTFT 中位数可下降约 35%。
消息线程可降低长时间运行的智能体会话延迟
每轮只发送新消息,而非完整对话历史,对于包含 500k 到 1M 个 token 的会话,首 token 时间中位数可减少约 35%。
基于线程的对话管理会在记忆闲置后过期
每轮请求之间,线程会保存在加密内存中;闲置 5 minutes 后会自动过期,此时客户端必须重新发送完整对话以启动新线程。
该功能已在 Claude Platform 的多种部署方式中开放
消息线程已在原生 Claude Platform、AWS 上的 Claude Platform、Microsoft Foundry 中的 Claude,以及 Google Cloud 上的 Claude Models(由 Anthropic 托管)开放;适用标准 token 费率和现有速率限制。
完整摘要
消息线程现已在 Claude Platform 开放公测,可让长时间运行的智能体只发送增量对话内容,而无需在每轮请求中重新发送完整历史记录,从而降低延迟。该功能的工作方式是:首次请求包含完整对话以启动线程,后续请求则只发送新消息和上一次响应的 ID。线程会保存在加密内存中,并在闲置 5 minutes 后过期。对于 Claude Code 用户,包含 500k 到 1M 个 token 的会话,其首 token 时间中位数可减少约 35%。这项功能尤其适用于长时间运行的编程智能体、包含屏幕截图的计算机使用智能体,以及文档或图像密集型工作流。该功能已在 Claude Platform 的多种部署方式中开放,包括 AWS、Microsoft Foundry 和 Google Cloud(由 Anthropic 托管);适用标准 token 费率和现有速率限制。
目录 · 2 节
消息线程的工作原理How message threads works
一个编码代理在一个会话中可能会发出数百次请求,而每次请求都会重新发送所有先前的消息和工具结果。对话一旦达到数十万个 token,重新发送全部内容就会增加 Claude 开始响应前的等待时间。借助消息线程,代理只需发送上轮之后新增的内容,从而缩短首 token 延迟(TTFT)。A coding agent might make hundreds of requests in one session, and each request resends every earlier message and tool result. Once a conversation reaches hundreds of thousands of tokens, resending all of it adds time before Claude starts responding. With message threads, agents send only what’s new since the last turn, which reduces time to first token (TTFT).
对于大多数 Claude Code 用户,消息线程默认处于启用状态,包含 50 万到 100 万个 token 的请求,其 TTFT 中位数下降约 35%。With message threads on by default for most Claude Code users, requests with 500k to 1M tokens see median TTFT drop by about 35%.
要使用消息线程,请在 Messages API 请求中添加 thread 参数。第一个请求包含完整对话,并启动一个线程。之后的每个请求只包含新消息和上一个响应的 ID。在各轮对话之间,我们会将每个线程保存在内存中,并对其加密,不会写入磁盘。线程在最后一次请求 5 分钟后过期,此时客户端会重新发送完整对话以启动新线程。使用消息线程后,每个请求会更小,但 Claude 在每一轮仍会读取完整对话,因此 token 用量保持不变。To use message threads, add the thread parameter to Messages API requests. The first request includes the full conversation and starts a thread. Each request after that includes only the new messages and the ID of the previous response. Between turns, we hold each thread in memory, where it’s encrypted and not written to disk. A thread expires 5 minutes after its last request and your client then resends the full conversation to start a new one. While each request is smaller with message threads, Claude still reads the full conversation on every turn, so token usage stays the same.
消息线程最适合延迟至关重要的活动会话,包括:Message threads is best for active sessions where latency matters, including:
- 长时间运行的编码代理,其每次工具调用结果或文件编辑都会增加对话内容。Long-running coding agents, where each tool result or file edit adds to the conversation.
- 计算机操作代理,截图会保留在线程中,无需每轮都重新上传。Computer use agents, where screenshots stay with the thread instead of being re-uploaded on every turn.
- 文档和图像密集型工作流,在这类工作流中,请求会被拆分得更小。Document and image-heavy workflows, where requests are made smaller.
智能体携带的上下文越多,消息线程越能缩短首个 token 的生成时间。The more context an agent carries, the more message threads can reduce time to first token.
入门Getting started
消息线程现已在原生 Claude Platform、AWS 上的 Claude Platform、Microsoft Foundry 中的 Claude,以及 Google Cloud 上由 Anthropic 托管的 Claude Models 中开放公测。适用标准 Claude 令牌费率和现有速率限制。请参阅文档以开始使用。Message threads is available now in public beta on the native Claude Platform, and on Claude Platform on AWS, Claude in Microsoft Foundry, and on Claude Models on Google Cloud (Anthropic managed). Standard Claude token rates and existing rate limits apply. See the documentation to get started.