介绍 Strands Agent SOPs – AI 代理的自然语言工作流 | AWS 开源博客
现代人工智能可以编写代码、创作交响乐并解决复杂的推理问题。那么,为什么让它可靠地完成你想要的任务仍然如此困难?
构建能够持续执行复杂任务的可靠AI代理仍然充满挑战。虽然现代语言模型在推理和解决问题方面表现出色,但将这种能力转化为可预测的工作流通常需要大量的状态机来编排代理行为。代码定义的工作流可以最大程度地控制代理的行为,但更改系统需要编写数百行代码以适应新的行为。这代表了开发代理的控制-灵活性谱系中的一个极端。
在该谱系的另一端,模型驱动代理完全摒弃了代码定义的行为,转而进行自我编排。我们用代理的自主决策来替代成千上万行定义工作流的代码,让它自行决定解决问题的路径。模型驱动代理能够适应意料之外的输入——其非确定性特性使其能够灵活应对任何情况。然而,这种灵活性是有代价的:很难以一种可靠的方式引导代理,使其始终如一地实现目标。
有没有一种方法可以兼得两者之长呢?
这就是 Agent SOPs 的用武之地。它是一种用于以自然语言定义AI代理工作流的标准化Markdown格式,在灵活性和控制性之间充当强大的中间地带。这些自然语言指令集将复杂流程转化为可重用、可共享的工作流,适用于不同的AI系统和团队。通过将结构化指导与使AI代理强大的灵活性相结合,Agent SOPs使团队能够将经过验证的工作流编码成可重用模板,并在任何需要智能自动化的地方一致地应用它们。这一概念源自亚马逊内部构建者社区的创新,随着团队发现其在引导代理行为方面的潜力,已在公司内部迅速传播。
从内部创新到开源
在亚马逊,我们拥有一个由数万构建者组成的充满活力的社区,他们积极尝试并创新AI在日常工作中的应用。这个社区涵盖AWS服务团队、亚马逊零售运营、物流和研究团队,他们围绕着AI驱动的开发工具,通过快速实验和知识共享的文化紧密联系在一起。
当内部首次出现AI编码助手时,我们的构建者们立即开始挑战可能性的边界。团队开始利用它们来自动化从代码审查、文档生成到事件响应和系统监控等一切任务。但随着采用规模扩大,我们遇到了多个问题:
- 行为不一致 可能是最令人头疼的挑战。同一个代理在开发过程中表现出色,但在处理真实场景时却产生了截然不同的结果。在没有结构化指导的情况下,代理在工具使用、任务优先级排序和输出格式方面做出了不同的决策,导致不可预测的结果,削弱了信心并令用户感到沮丧。
- 提示工程复杂性 构成了另一个采用障碍。为复杂工作流程设计有效的提示需要领域知识和语言模型行为方面的深厚专业知识。团队花了数周时间来完善复杂任务的提示,结果却发现他们的指令无法很好地迁移到不同的模型或用例中。开发人员变得不愿更新提示,因为他们无法预测更改会如何影响输出,导致在修改前需要进行漫长的人工评估。
当意识到我们需要找到一种处于“确定性-非确定性”甜点区的方案时,突破随之而来:既要足够结构化以确保一致的结果,又要足够灵活以利用AI代理的价值智能。这一洞见促使我们的社区开发了Agent SOPs,这是一种用于定义AI代理工作流的标准化Markdown格式,平衡了可靠性与适应性。
我们的社区迅速采纳了这一概念,因为它解决了每个团队都面临的实际问题。团队发现,无需任何提示工程专业知识,他们可以在几分钟内生成初始的自动化流程,创建出立即有用、易于理解且易于分享的工作流。这种结构化方法极大地减少了提示开发的试错过程,同时保留了使AI自动化有价值的智能。团队可以更有信心地对SOP的特定行为进行迭代,而不会破坏现有功能,使改进更快、更可预测。
如今,亚马逊的团队在各种不同的用例中使用了数千个SOP,从代码审查和文档生成到事件响应和系统监控。一致的格式使得在不同团队之间分享成功的方法变得容易,同时可以根据不同的上下文和需求进行调整。这个最初作为解决提示工程问题的简单方案,已成为在我们整个组织中普及AI专业知识的有力方式。
现在,我们将Agent SOPs作为开源项目发布,因为我们相信这种方法可以使更广泛的AI开发社区受益。通过分享这个概念和实际例子,我们希望能加速可靠AI工作流程在整个行业的采用。
什么是 Agent SOPs?
Agent SOPs 使用具有关键特性的标准化 Markdown 格式,以促使 AI 代理产生可重复且可理解的行为:
- 具有 RFC 2119 约束的结构化步骤 – 工作流的每一步都使用 RFC 2119 关键词,如 MUST、SHOULD 和 MAY,在无需刚性脚本的情况下提供对代理行为的精确控制,确保可靠执行,同时保留代理的推理能力。
- 参数化输入 – SOPs 接受参数来为不同项目、团队或需求定制行为,而不是硬编码特定值。这将单次使用的提示转变为灵活的模板,可以在广泛范围内应用,同时保持一致性。
- 通过 AI 辅助实现轻松创作 – 团队可以在几分钟内创建新的 SOP。编码代理可以阅读 SOP 格式规范,并根据自然语言描述生成新的工作流程,使创建过程对任何人开放,无论其提示工程专业知识如何。
- 进度跟踪和可恢复性 – Agent SOPs 可以指示代理在工作时记录其进度,便于了解当前情况,并在出现问题时恢复工作。这种透明度对于调试提示和建立开发者对 AI 自动化的信任至关重要。
这种格式简单但高度通用,适用于任何接受自然语言指令的地方。像 Strands 这样的代理框架可以将 SOPs 作为系统提示嵌入,像 Kiro 和 Cursor 这样的开发工具可以使用它们来构建结构化工作流程,而像 Claude 和 GPT-4 这样的 AI 模型可以直接执行它们。虽然不同模型在精确遵循指令的能力上可能有所不同,但标准化格式为工作流自动化提供了一致的基础。
实践演练:运行 Codebase Summary SOP
让我们通过运行 codebase-summary SOP 来实际了解 Agent SOPs 的工作原理。在此示例中,我们将使用 Kiro CLI,但您可以在任何支持模型上下文协议服务器的 AI 代理中尝试此演练。
首先,安装 strands-agents-sops Python 包:
1
|
pip install strands-agents-sops
|
接下来,更新您的 MCP 服务器配置以包含 strands-agents-sops MCP 服务器:
1
2
3
4
5
6
7
8
|
{
"mcpServers" : {
"strands-agents-sops" : {
"command" : "strands-agents-sops",
"args" : []
}
}
}
|
此 MCP 服务器将 SOPs 作为 MCP 提示提供,可供您的 AI 助手调用。在 Kiro CLI 中,我们可以通过 /prompts 命令确认预打包的 Agent SOPs 现已可用:
1
2
3
4
5
6
7
8
9
10
11
|
[strands-agents-sop] > /prompts
Usage: You can use a prompt by typing '@<prompt name> [...args]'
Prompt Description Arguments (* = required)
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
strands-agents-sop (MCP):
- code-assist This sop guides the implementation of... user_input
- code-task-generator This sop generates structured code ta... user_input
- codebase-summary This sop analyzes a codebase and gene... user_input
- pdd This sop guides you through the proce... user_input
|
接下来,我们调用 codebase-summary SOP:
Kiro CLI 读取 SOP 并请求必需和可选参数:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
> I'll help you run the codebase summary SOP. Let me gather the required parameters first.
Please provide the following parameters for the codebase analysis:
Required Parameters:
• **codebase_path**: Path to the codebase to analyze (default: current directory)
Optional Parameters:
• **output_dir**: Directory where documentation will be stored (default: ".sop/summary")
• **consolidate**: Whether to create a consolidated documentation file (default: false)
• **consolidate_prompt**: Description of how to structure the consolidated content
• **check_consistency**: Whether to check for inconsistencies across documents (default: true)
• **check_completeness**: Whether to identify areas lacking sufficient detail (default: true)
• **update_mode**: Whether to update existing documentation based on recent commits (default: false)
You can provide these parameters in any of these ways:
• Direct input in your response
• File path containing the configuration
• Just specify what you want to change from defaults
What parameters would you like to use for this codebase analysis?
|
我们以自然语言提供请求的参数。在此示例中,我们将在 strands-agents-sop python 源代码上运行此 SOP:
1
2
|
[strands-agents-sop] > codebase_path: ~/github/agent-sop/python
consolidate: true
|
然后,代理遵循 SOP 的结构化步骤,从设置目录结构开始。您可以看到代理验证了代码库路径存在,因为 SOP 约束规定代理在继续之前 必须 验证路径存在:
1
2
3
4
5
6
7
|
## Step 1: Setup and Directory Structure
...
> ✅ Validated codebase path exists and is accessible
✅ Created directory .summary/
✅ Full analysis will be performed (not in update mode)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
## Step 6: Summary and Next Steps
## Summary
✅ Documentation generation complete!
### What Was Accomplished
• **Analyzed** Python package with 4 core modules and 5 SOP files
• **Generated** comprehensive documentation ecosystem in .summary/ directory:
• index.md: Knowledge base index with AI assistant instructions
• codebase_info.md: Project overview and structure
• architecture.md: System design and patterns
• components.md: Module responsibilities and functionality
• interfaces.md: APIs, CLI, and integration points
• data_models.md: SOP format and data structures
• workflows.md: Build process and operational procedures
• dependencies.md: External requirements and usage
• review_notes.md: Quality assessment and recommendations
• **Identified** system as modular CLI tool with MCP server capabilities
### Key Findings
• **Architecture**: Clean modular design with dynamic SOP loading
• **Integration**: Multiple interfaces (Python imports, MCP protocol, CLI, Anthropic skills)
• **Content Management**: File-based SOP storage with build-time synchronization
• **Dependencies**: Minimal external requirements (only MCP library)
• **Extensibility**: Easy to add new SOPs without code changes
<summary continues...>
|
使用此 SOP,通过调用单个提示,我们的代理能够在无需额外人工交互的情况下生成全面的代码库文档。SOP 的约束确保了结构的一致性和完整性,而代理的智能则使内容适应特定的代码库特征。
SOP 链:构建完整的开发工作流程
SOP 链通过将专注的 SOP 连接成智能序列,使代理能够执行复杂的多阶段工作流程。初始的 SOP 存储库包含一个完整的开发工作流程链,展示了 SOPs 如何协同工作,带您从理解现有代码库到实现新功能:
codebase-summary 分析代码库并生成全面的文档,帮助人类和 AI 代理理解系统架构、组件和工作流程。此 SOP 通常作为基础,为后续自动化提供上下文。
pdd 实现了“提示驱动开发”方法,将用户从一个粗略的想法带出,进行系统研究、需求澄清、解决方案设计和实施计划。在理解了代码库之后使用此 SOP 来处理复杂的功能规划。
code-task-generator 将高级需求分解为可操作的开发任务,展示了 SOPs 如何处理需要理解上下文并对范围和优先级做出明智决定的创造性和分析性工作。可将其作为独立工具使用,或与 pdd SOP 的输出结合使用。
code-assist 实现了一个测试驱动开发工作流,引导代理通过结构化的探索、规划、编码和提交阶段来完成功能实现。此 SOP 展示了如何在自动执行和人工监督之间取得平衡。
SOP 链通过工件交接进行工作:codebase-summary 创建 pdd 在设计功能时引用的文档文件,pdd 生成 code-task-generator 用于创建任务列表的规范,而 code-assist 则在完全了解先前建立的架构和要求的情况下实施这些任务。每个 SOP 都会将其发现浓缩成专注的输出,这些输出成为下一步的简化输入。
这种链式方法实现了远超单个代理处理能力的工作流程。无需让一个代理承担整个开发周期——这会导致其失去焦点并偏离预期行为——您可以将复杂问题分解为可管理的部分。在开发 Agent SOPs 时,这种模块化方法允许您独立构建和测试每个组件,然后将它们链接成复杂的工作流程。顺序结构消除了冗余工作(无需重复的代码库分析或信息收集),同时保持了使代理具有价值的智能适应性,最终实现了在整体方法中难以企及的自动化规模。
Agent SOP 集成
Agent SOPs 不绑定到特定的代理产品,可在多种环境中使用:
Strands 代理
Agent SOPs 可以用作您的 Strands 代理的系统提示。只需安装 PyPI 包,然后将 SOP 作为代理的系统提示导入。以下是一个 code-assist CLI 代理的示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
from strands import Agent
from strands_tools import editor, shell
from strands_agents_sops import code_assist
agent = Agent(
system_prompt=code_assist,
tools=[editor, shell]
)
agent("Start code-assist sop")
while(True):
agent(input("\nInput: "))
|
执行此 Python 脚本后,您可能会看到类似这样的输出:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
I'll help you implement a code task using the test-driven development workflow. Let me gather the required parameters to get started.
Please provide the following information:
**Required:**
- **task_description**: What would you like to implement? (You can provide a detailed specification, requirements, acceptance criteria, or even a rough idea that we'll refine together. You can also provide a file path or URL if the description is in a document)
**Optional:**
- **additional_context**: Any supplementary information that would help with understanding the implementation context (can also be a file path or URL)
- **documentation_dir**: Directory for planning documents (default: ".sop/planning")
- **repo_root**: Root directory of the repository (default: current working directory)
- **task_name**: A short, descriptive name for the task (I'll generate one if not provided)
- **mode**: Interaction mode:
- "interactive" (default): I'll collaborate with you and ask for confirmation at each step
- "auto": I'll execute autonomously after setup with no further interaction required
Please provide these details so we can begin the implementation process.
|
基于前面的 codebase-summary 示例,您可以创建一个自动保持文档最新的 Strands 代理:
1
2
3
4
5
6
7
8
9
10
11
|
from strands import Agent
from strands_tools import editor, shell, git
from strands_agents_sops import codebase_summary
doc_agent = Agent(
system_prompt=codebase_summary,
tools=[editor, shell, git]
)
# Run in update mode to refresh docs based on recent commits
doc_agent("Run codebase-summary with update_mode: true")
|
这实现了无需人工干预的持续文档维护,自动更新代码库摘要以反映新的更改。
Claude Skills
Agent SOPs 与 Claude 的 Skills 系统完全兼容,允许您教授 Claude 可以跨对话和项目重用的专业工作流程。
每个 Agent SOP 都可以自动转换为 Anthropic 的 Skills 格式:
1
2
3
4
5
|
# Generate Skills format from SOPs
strands-agents-sops skills
# Or specify custom output directory
strands-agents-sops skills --output-dir my-skills
|
这将创建单独的技能目录:
1
2
3
4
5
6
7
8
9
|
skills/
├── code-assist/
│ └── SKILL.md
├── codebase-summary/
│ └── SKILL.md
├── code-task-generator/
│ └── SKILL.md
└── pdd/
└── SKILL.md
|
然后,您可以按照 Anthropic 的文档开始将这些 SOPs 用作技能。
其他产品集成
Agent SOPs 可在各种 AI 开发环境中运行。虽然不同的 LLM 在遵循指令的能力上可能有所不同,但标准化格式可在不同平台上提供一致的结果。以下是团队使用 Agent SOPs 的其他方式:
- Kiro IDE 引导文件和钩子 – SOPs 可以集成到 Kiro IDE 的工作流系统中,为特定的开发任务提供结构化指导。
- Claude Code 和 Cursor 中的自定义命令 – SOPs 可以成为自定义命令的理想选择,用于调用常见开发工作流的特定 SOP。
- Python 模块 – 通过程序化访问,可以集成到自定义应用程序和更大的自动化系统中。
创作 Agent SOPs
Agent SOPs 最强大的方面之一是通过对话式创作轻松创建它们。我们提供了一个 Agent SOP 格式规则,引导 AI 模型捕获您的自然工作流描述,并将其转换为标准化格式。让我们通过创作一个用于处理会议记录的简单 SOP 来演示一下。
根据您使用的 AI 应用程序,您可以将 Agent SOP 格式安装为引导文件或规则。然而,出于本演练的目的,我们让 Kiro CLI 直接读取 Agent SOP 规则:
1
2
3
4
|
[strands-agents-sop] > Run `strands-agents-sops rule`
...
> The command executed successfully and displayed the Agent SOP format specification.
...
|
现在 Kiro CLI 已经理解了 Agent SOP 格式,我们只需用自然语言向我们的 AI 助手描述我们想要什么:
1
|
[strands-agents-sop] > I want to create an Agent SOP that takes meeting notes and generates action items, decisions made, and follow-up tasks with assigned owners and deadlines.
|
拥有 SOP 格式规范访问权限的 AI 助手在几秒钟内生成了一个初版 Agent SOP。它会创建适当的参数,将工作流程构造成逻辑步骤,并添加 RFC 2119 约束以使 SOP 可靠且可重复。
由于它是用自然语言编写的,Agent SOP 易于阅读。我们可以在单独的会话中尝试运行它,然后返回与我们的代理交流,以迭代改进该 SOP。例如,我们可以改进这个 SOP,使其使用特定的文件夹结构,在会议记录文件夹和跟进文件夹中记录行动项,并将做出的决定添加到项目特定的决策日志中。
同样的方法适用于您想要自动化的任何工作流程。只需用自然语言描述过程,然后让 AI 辅助来处理 SOP 创建的技术细节。注意,如果您希望您的 SOP 使用特定的 MCP 工具,在创作 SOP 时需要加载这些 MCP 服务器。
结论
Agent SOPs 解决了限制 AI 自动化采用的根本矛盾:可靠性与灵活性之间的张力。通过将结构化指导与智能推理相结合,它们使团队能够快速捕获专业知识,并在不同的 AI 系统和环境中一致地应用。让 Agent SOPs 在亚马逊获得成功的半确定性甜点,现在已可供全球客户使用,以构建更可靠、能力更强的 AI 系统。
准备好开始了吗?
- 访问 Agent SOPs GitHub 仓库获取完整文档和示例
- 安装包:
pip install strands-agents-sops
- 启动 MCP 服务器:
strands-agents-sops
- 在您的下一个项目上尝试
codebase-summary SOP
- 将 SOPs 链接在一起以构建复杂的开发工作流程
我们很期待看到客户如何使用 Agent SOPs 解决他们独特的自动化挑战,以及更广泛的社区将涌现出哪些创新工作流程。