文章目录
阅读模式
已读 0%

面向长时间运行智能体(agent)的有效框架(harness)

Effective harnesses for long-running agents

阅读原文
段落导航
原文译文
原文

As AI agents become more capable, developers are increasingly asking them to take on complex tasks requiring work that spans hours, or even days.

译文

随着 AI 智能体能力越来越强,开发者越来越多地要求它们承担需要持续数小时甚至数天工作的复杂任务。

原文

However, getting agents to make consistent progress across multiple context windows remains an open problem.

译文

然而,如何让智能体在多个上下文窗口(context window)之间持续取得进展,仍然是一个未解决的问题。

原文

The core challenge of long-running agents is that they must work in discrete sessions, and each new session begins with no memory of what came before.

译文

长时间运行的智能体的核心挑战在于,它们必须在离散的会话中工作,而每个新会话开始时都不记得之前发生了什么。

原文

Imagine a software project staffed by engineers working in shifts, where each new engineer arrives with no memory of what happened on the previous shift.

译文

想象一个软件项目,由轮班工作的工程师组成,每位新来的工程师都不知道上一班发生了什么。

原文

Because context windows are limited, and because most complex projects cannot be completed within a single window, agents need a way to bridge the gap between coding sessions.

译文

由于上下文窗口有限,而且大多数复杂项目无法在单个窗口内完成,智能体需要一种方式来弥合编码会话之间的鸿沟。

原文

We developed a two-fold solution to enable the Claude Agent SDK to work effectively across many context windows: an initializer agent that sets up the environment on the first run, and a coding agent that is tasked with making incremental progress in every session, while leaving clear artifacts for the next session.

译文

我们开发了一个双管齐下的解决方案,使 Claude Agent SDK 能够在多个上下文窗口之间有效工作:一个初始化智能体,它在首次运行时搭建环境;以及一个编码智能体,它的任务是在每个会话中取得增量进展,同时为下一个会话留下清晰的产物。

原文

You can find code examples in the accompanying quickstart.

译文

你可以在随附的 quickstart 中找到代码示例。

原文

The Claude Agent SDK is a powerful, general-purpose agent harness adept at coding, as well as other tasks that require the model to use tools to gather context, plan, and execute.

译文

Claude Agent SDK 是一个强大、通用的智能体框架,擅长编码,也擅长其他需要模型使用工具来收集上下文、规划和执行的任务。

原文

It has context management capabilities such as compaction, which enables an agent to work on a task without exhausting the context window.

译文

它具有诸如压缩(compaction)之类的上下文管理能力,这使智能体能够在不耗尽上下文窗口的情况下处理任务。

原文

Theoretically, given this setup, it should be possible for an agent to continue to do useful work for an arbitrarily long time.

译文

理论上,在这种设置下,智能体应该能够持续进行有用的工作任意长的时间。

原文

However, compaction isn’t sufficient.

译文

然而,压缩还不够。

原文

Out of the box, even a frontier coding model like Opus 4.5 running on the Claude Agent SDK in a loop across multiple context windows will fall short of building a production-quality web app if it’s only given a high-level prompt, such as “build a clone of claude.ai.”

译文

开箱即用的情况下,即使是像 Opus 4.5 这样的前沿编码模型,在 Claude Agent SDK 上以循环方式跨多个上下文窗口运行,如果只给它一个高层级的提示词(prompt),例如“构建一个 claude.ai 的克隆”,也无法构建出生产质量的 Web 应用。

原文

Claude’s failures manifested in two patterns.

译文

Claude 的失败表现为两种模式。

原文

First, the agent tended to try to do too much at once—essentially to attempt to one-shot the app.

译文

首先,智能体倾向于试图一次做太多事情——本质上就是试图一次性完成(one-shot)这个应用。

原文

Often, this led to the model running out of context in the middle of its implementation, leaving the next session to start with a feature half-implemented and undocumented.

译文

这常常导致模型在实现过程中耗尽上下文,让下一个会话开始时面对一个只实现了一半且没有文档的功能。

原文

The agent would then have to guess at what had happened, and spend substantial time trying to get the basic app working again.

译文

然后智能体不得不猜测发生了什么,并花费大量时间试图让基础应用重新工作。

原文

This happens even with compaction, which doesn’t always pass perfectly clear instructions to the next agent.

译文

即使有压缩,这种情况也会发生,因为压缩并不总能向下一个智能体传递完全清晰的指令。

原文

A second failure mode would often occur later in a project.

译文

第二种失败模式往往会在项目后期出现。

原文

After some features had already been built, a later agent instance would look around, see that progress had been made, and declare the job done.

译文

在已经构建了一些功能之后,后续的智能体实例会环顾四周,看到已有进展,便宣布任务完成。

原文

This decomposes the problem into two parts.

译文

这会把问题拆解成两部分。

原文

First, we need to set up an initial environment that lays the foundation for all the features that a given prompt requires, which sets up the agent to work step-by-step and feature-by-feature.

译文

首先,我们需要搭建一个初始环境,为给定提示词所要求的所有功能打下基础,让智能体能够一步一步、一个功能一个功能地推进。

原文

Second, we should prompt each agent to make incremental progress towards its goal while also leaving the environment in a clean state at the end of a session.

译文

其次,我们应该提示每个智能体朝着目标做出增量进展,同时在会话结束时让环境保持干净状态。

原文

By “clean state” we mean the kind of code that would be appropriate for merging to a main branch: there are no major bugs, the code is orderly and well-documented, and in general, a developer could easily begin work on a new feature without first having to clean up an unrelated mess.

译文

所谓“干净状态”,指的是适合合并到主分支的那种代码:没有重大 bug,代码井然有序且文档完善,总体上,开发者无需先清理无关的烂摊子,就能轻松开始开发新功能。

原文

When experimenting internally, we addressed these problems using a two-part solution:

译文

在内部实验时,我们用一套由两部分组成的方案解决了这些问题:

原文

Initializer agent: The very first agent session uses a specialized prompt that asks the model to set up the initial environment: an init.sh script, a claude-progress.txt file that keeps a log of what agents have done, and an initial git commit that shows what files were added.

译文

初始化智能体:第一个智能体会话使用一个专门的提示词,要求模型搭建初始环境:一个 init.sh 脚本、一个记录智能体所做工作的 claude-progress.txt 文件,以及一个显示新增了哪些文件的初始 git 提交。

原文

Coding agent: Every subsequent session asks the model to make incremental progress, then leave structured updates.1

译文

编码智能体:之后的每个会话都要求模型做出增量进展,然后留下结构化的更新。1

原文

The key insight here was finding a way for agents to quickly understand the state of work when starting with a fresh context window, which is accomplished with the claude-progress.txt file alongside the git history.

译文

这里的关键洞见是找到一种方法,让智能体在带着全新上下文窗口启动时能快速理解工作状态,而这通过 claude-progress.txt 文件配合 git 历史来实现。

原文

Inspiration for these practices came from knowing what effective software engineers do every day.

译文

这些做法的灵感来自了解高效软件工程师每天在做什么。

原文

In the updated Claude 4 prompting guide, we shared some best practices for multi-context window workflows, including a harness structure that uses “a different prompt for the very first context window.”

译文

在更新后的 Claude 4 提示词指南中,我们分享了一些多上下文窗口工作流的最佳实践,其中包括一种框架结构:它为第一个上下文窗口使用不同的提示词。

原文

This “different prompt” requests that the initializer agent set up the environment with all the necessary context that future coding agents will need to work effectively.

译文

这个「不同的提示词」要求初始化智能体搭建好环境,并准备好后续的编码智能体高效工作所需的全部上下文。

原文

Here, we provide a deeper dive on some of the key components of such an environment.

译文

在此,我们深入探讨这类环境的一些关键组成部分。

原文

To address the problem of the agent one-shotting an app or prematurely considering the project complete, we prompted the initializer agent to write a comprehensive file of feature requirements expanding on the user’s initial prompt.

译文

为了解决智能体一次性完成整个应用、或过早认为项目已完成的问题,我们提示初始化智能体编写一份详尽的功能需求文件,对用户的初始提示词加以扩展。

原文

In the claude.ai clone example, this meant over 200 features, such as “a user can open a new chat, type in a query, press enter, and see an AI response.”

译文

在 claude.ai 克隆示例中,这意味着 200 多项功能,例如「用户可以打开一个新对话、输入查询、按回车,然后看到 AI 的回复」。

原文

These features were all initially marked as “failing” so that later coding agents would have a clear outline of what full functionality looked like.

译文

这些功能最初全部标记为「failing」,这样后续的编码智能体就能清楚了解完整功能应当是什么样子。

代码
{
    "category": "functional",
    "description": "New chat button creates a fresh conversation",
    "steps": [
      "Navigate to main interface",
      "Click the 'New Chat' button",
      "Verify a new conversation is created",
      "Check that chat area shows welcome state",
      "Verify conversation appears in sidebar"
    ],
    "passes": false
  }
原文

We prompt coding agents to edit this file only by changing the status of a passes field, and we use strongly-worded instructions like “It is unacceptable to remove or edit tests because this could lead to missing or buggy functionality.”

译文

我们提示编码智能体只能通过修改 passes 字段的状态来编辑这个文件,并使用措辞强硬的指令,例如「删除或修改测试是不可接受的,因为这可能导致功能缺失或出现 bug」。

原文

After some experimentation, we landed on using JSON for this, as the model is less likely to inappropriately change or overwrite JSON files compared to Markdown files.

译文

经过一番实验,我们最终选择用 JSON 来做这件事,因为与 Markdown 文件相比,模型不太可能不恰当地修改或覆盖 JSON 文件。

原文

Given this initial environment scaffolding, the next iteration of the coding agent was then asked to work on only one feature at a time.

译文

有了这套初始环境脚手架,下一轮编码智能体被要求一次只处理一个功能。

原文

This incremental approach turned out to be critical to addressing the agent’s tendency to do too much at once.

译文

事实证明,这种增量式方法对于纠正智能体一次想做太多事的倾向至关重要。

原文

Once working incrementally, it’s still essential that the model leaves the environment in a clean state after making a code change.

译文

即便采用增量方式工作,模型在做出代码更改后仍必须让环境保持干净状态。

原文

In our experiments, we found that the best way to elicit this behavior was to ask the model to commit its progress to git with descriptive commit messages and to write summaries of its progress in a progress file.

译文

在我们的实验中,我们发现促成这种行为的最佳方式是让模型用描述性的提交信息把进度提交到 git,并在进度文件中写下进度摘要。

原文

This allowed the model to use git to revert bad code changes and recover working states of the code base.

译文

这让模型能够借助 git 回滚糟糕的代码更改,并恢复代码库的可工作状态。

原文

These approaches also increased efficiency, as they eliminated the need for an agent to have to guess at what had happened and spend its time trying to get the basic app working again.

译文

这些做法还提升了效率,因为智能体不再需要猜测发生了什么,也不必把时间花在让基础应用重新跑起来上。

原文

One final major failure mode that we observed was Claude’s tendency to mark a feature as complete without proper testing.

译文

我们观察到的最后一个主要失效模式是 Claude 倾向于在没有充分测试的情况下就把功能标记为完成。

原文

Absent explicit prompting, Claude tended to make code changes, and even do testing with unit tests or curl commands against a development server, but would fail recognize that the feature didn’t work end-to-end.

译文

在没有明确提示的情况下,Claude 往往会修改代码,甚至用单元测试或针对开发服务器的 curl 命令做测试,但会未能意识到该功能无法端到端正常工作。

原文

In the case of building a web app, Claude mostly did well at verifying features end-to-end once explicitly prompted to use browser automation tools and do all testing as a human user would.

译文

在构建 Web 应用的情况下,一旦被明确提示使用浏览器自动化工具并像真实用户那样完成所有测试,Claude 大多能很好地端到端验证功能。

 Screenshots taken by Claude through the Puppeteer MCP server as it tested the claude.ai clone.
原文

Screenshots taken by Claude through the Puppeteer MCP server as it tested the claude.ai clone.

译文

Claude 在测试 claude.ai 克隆版时通过 Puppeteer MCP server 截取的屏幕截图。

原文

Providing Claude with these kinds of testing tools dramatically improved performance, as the agent was able to identify and fix bugs that weren’t obvious from the code alone.

译文

为 Claude 提供这类测试工具大幅提升了表现,因为智能体能够识别并修复仅靠代码不易发现的 bug。

原文

Some issues remain, like limitations to Claude’s vision and to browser automation tools making it difficult to identify every kind of bug.

译文

仍有一些问题存在,比如 Claude 的视觉能力以及浏览器自动化工具的限制,使得难以识别每一种 bug。

原文

For example, Claude can’t see browser-native alert modals through the Puppeteer MCP, and features relying on these modals tended to be buggier as a result.

译文

例如,Claude 无法通过 Puppeteer MCP 看到浏览器原生的警告模态框,因此依赖这些模态框的功能往往更容易出现 bug。

原文

With all of the above in place, every coding agent is prompted to run through a series of steps to get its bearings, some quite basic but still helpful:

译文

在以上所有配置就位后,每个编码智能体都会被提示执行一系列步骤来熟悉情况,其中有些相当基础但仍然有帮助:

原文

Run pwd to see the directory you’re working in.

译文

运行 pwd 查看你当前工作的目录。

原文

You’ll only be able to edit files in this directory.

译文

你只能编辑此目录中的文件。

原文

Read the git logs and progress files to get up to speed on what was recently worked on.

译文

阅读 git 日志和进度文件,以快速了解最近在做什么。

原文

Read the features list file and choose the highest-priority feature that’s not yet done to work on.

译文

阅读功能列表文件,并选择尚未完成的最高优先级功能来处理。

原文

This approach saves Claude some tokens in every session since it doesn’t have to figure out how to test the code.

译文

这种方法在每次会话中为 Claude 节省一些 token,因为它不必弄清楚如何测试代码。

原文

It also helps to ask the initializer agent to write an init.sh script that can run the development server, and then run through a basic end-to-end test before implementing a new feature.

译文

让初始化智能体编写一个可以运行开发服务器的 init.sh 脚本,然后在实现新功能之前运行一遍基本的端到端测试,也会有所帮助。

原文

In the case of the claude.ai clone, this meant that the agent always started the local development server and used the Puppeteer MCP to start a new chat, send a message, and receive a response.

译文

在 claude.ai 克隆项目的情况下,这意味着智能体总是启动本地开发服务器,并使用 Puppeteer MCP 来开始新聊天、发送消息并接收响应。

原文

This ensured that Claude could quickly identify if the app had been left in a broken state, and immediately fix any existing bugs.

译文

这确保了 Claude 能快速识别应用是否已被留在损坏状态,并立即修复任何现有 bug。

原文

If the agent had instead started implementing a new feature, it would likely make the problem worse.

译文

如果智能体转而开始实现新功能,它很可能会让问题变得更糟。

原文

Given all this, a typical session starts off with the following assistant messages:

译文

鉴于这一切,一次典型会话会以以下 assistant 消息开始:

代码
[Assistant] I'll start by getting my bearings and understanding the current state of the project.
[Tool Use] <bash - pwd>
[Tool Use] <read - claude-progress.txt>
[Tool Use] <read - feature_list.json>
[Assistant] Let me check the git log to see recent work.
[Tool Use] <bash - git log --oneline -20>
[Assistant] Now let me check if there's an init.sh script to restart the servers.
<Starts the development server>
[Assistant] Excellent! Now let me navigate to the application and verify that some fundamental features are still working.
<Tests basic functionality>
[Assistant] Based on my verification testing, I can see that the fundamental functionality is working well. The core chat features, theme switching, conversation loading, and error handling are all functioning correctly. Now let me review the tests.json file more comprehensively to understand what needs to be implemented next.
<Starts work on a new feature>
原文

Problem

译文

问题

原文

Initializer Agent Behavior

译文

初始化智能体行为

原文

Coding Agent Behavior

译文

编码智能体行为

原文

Claude declares victory on the entire project too early.

译文

Claude 过早地宣布整个项目取得胜利。

原文

Set up a feature list file: based on the input spec, set up a structured JSON file with a list of end-to-end feature descriptions.

译文

设置一个功能列表文件:根据输入规格(spec),设置一个结构化的 JSON 文件,其中包含端到端功能描述的列表。

原文

Read the feature list file at the beginning of a session.

译文

在会话开始时读取功能列表文件。

原文

Choose a single feature to start working on.

译文

选择一个功能开始开发。

原文

Claude leaves the environment in a state with bugs or undocumented progress.

译文

Claude 会让环境处于带有 bug 或未记录进展的状态。

原文

An initial git repo and progress notes file is written.

译文

会写入一个初始 git repo 和进展记录文件。

原文

Start the session by reading the progress notes file and git commit logs, and run a basic test on the development server to catch any undocumented bugs.

译文

通过阅读进展记录文件和 git commit 日志来开始会话,并在开发服务器上运行基本测试,以捕获任何未记录的 bug。

原文

End the session by writing a git commit and progress update.

译文

通过写入 git commit 和进展更新来结束会话。

原文

Claude marks features as done prematurely.

译文

Claude 过早地将功能标记为完成。

原文

Set up a feature list file.

译文

设置一个功能列表文件。

原文

Self-verify all features.

译文

自行验证所有功能。

原文

Only mark features as “passing” after careful testing.

译文

只有在仔细测试后才将功能标记为“passing”。

原文

Claude has to spend time figuring out how to run the app.

译文

Claude 不得不花时间弄清楚如何运行该应用。

原文

Write an init.sh script that can run the development server.

译文

编写一个 init.sh 脚本,使其能运行开发服务器。

原文

Start the session by reading init.sh.

译文

通过读取 init.sh 来开始会话。

原文

This research demonstrates one possible set of solutions in a long-running agent harness to enable the model to make incremental progress across many context windows.

译文

这项研究展示了在长时间运行的智能体框架中一组可能的解决方案,使模型能够在多个上下文窗口之间取得渐进式进展。

原文

However, there remain open questions.

译文

然而,仍存在一些未解决的问题。

原文

Most notably, it’s still unclear whether a single, general-purpose coding agent performs best across contexts, or if better performance can be achieved through a multi-agent architecture.

译文

最值得注意的是,目前仍不清楚是单个通用编码智能体在不同上下文中表现最佳,还是通过多智能体架构可以获得更好的表现。

原文

It seems reasonable that specialized agents like a testing agent, a quality assurance agent, or a code cleanup agent, could do an even better job at sub-tasks across the software development lifecycle.

译文

看起来合理的是,像测试智能体、质量保证智能体或代码清理智能体这样的专用智能体,可以在软件开发生命周期的各个子任务上做得更好。

原文

Additionally, this demo is optimized for full-stack web app development.

译文

此外,这个演示针对全栈 Web 应用开发进行了优化。

原文

A future direction is to generalize these findings to other fields.

译文

未来的一个方向是将这些发现推广到其他领域。

原文

It’s likely that some or all of these lessons can be applied to the types of long-running agentic tasks required in, for example, scientific research or financial modeling.

译文

其中一些或全部经验很可能可以应用于例如科学研究或金融建模中所需的那类长时间运行的智能体任务。

原文

Written by Justin Young.

译文

作者:Justin Young。

原文

Special thanks to David Hershey, Prithvi Rajasakeran, Jeremy Hadfield, Naia Bouscal, Michael Tingley, Jesse Mu, Jake Eaton, Marius Buleandara, Maggie Vo, Pedram Navid, Nadine Yasser, and Alex Notov for their contributions.

译文

特别感谢 David Hershey、Prithvi Rajasakeran、Jeremy Hadfield、Naia Bouscal、Michael Tingley、Jesse Mu、Jake Eaton、Marius Buleandara、Maggie Vo、Pedram Navid、Nadine Yasser 和 Alex Notov 的贡献。

原文

This work reflects the collective efforts of several teams across Anthropic who made it possible for Claude to safely do long-horizon autonomous software engineering, especially the code RL & Claude Code teams.

译文

这项工作体现了 Anthropic 内部多个团队的共同努力,正是他们让 Claude 能够安全地完成长时程自主软件工程(long-horizon autonomous software engineering),尤其是 code RL 和 Claude Code 团队。

原文

Interested candidates who would like to contribute are welcome to apply at anthropic.com/careers.

译文

有兴趣作出贡献的候选人,欢迎在 anthropic.com/careers 申请。