关于 aws-strands
aws-strands 是使用 Strands Agents SDK 编写 Agent 的模型无关框架技能。内容涵盖工具、多 Agent 编排、对话管理和流式输出,默认模型提供方为 AWS Bedrock。
主要功能
- 01配置模型提供方
- 02自定义及预置工具
- 03Swarm 与 Graph 编排
- 04滑动窗口对话管理
- 05异步流式输出
适用场景
- 01用自定义可调用函数构建 Agent
- 02在专门 Agent 之间委派任务
- 03构建确定性的多 Agent 流程
- 04异步流式输出 Agent 响应
安装
复制下面的命令,在终端中运行。
npx skills add https:/ / github.com/ hoodini/ ai-agents-skills --skill aws-strands安装后在 Claude Code 中输入/aws-strands确认已加载。
详细介绍
Strands Agents SDK
用于编写 Agent 逻辑(即“脑”的开源框架)。与模型无关,默认使用 AWS Bedrock。可在任何环境运行(笔记本电脑、容器、Lambda、EC2)。
与另一项 AWS 技能的关系: Strands 是框架(决定 Agent 的行为)。要在 AWS 上托管和部署 Strands Agent,请使用 aws-harness(AgentCore 运行时)。两者可以组合使用:使用 Strands 编写,然后通过 AgentCore 发布。你也可以完全不部署到 AWS,直接运行 Strands。
安装
pip install strands-agents strands-agents-tools
(另有 TypeScript SDK,详见文档。以下示例使用 Python。)
快速入门
from strands import Agent
agent = Agent() # defaults to Bedrock, Claude 4 Sonnet
print(agent("What is the capital of France?"))
agent(...) 返回一个 AgentResult。str(result) 可获取文本;result.message 是结构化字典(role + content)。
模型配置
Bedrock 是默认提供方,无需传入模型参数——Strands 会选择适用于所在区域的 Claude 4 Sonnet。要覆盖默认设置,请传入模型 ID 字符串或 BedrockModel 提供方:
from strands import Agent
from strands.models import BedrockModel
# Simple: a Bedrock model id (copy the exact id from the Bedrock model catalog)
agent = Agent(model="<your-bedrock-model-id>")
# Full control:
agent = Agent(model=BedrockModel(
model_id="<your-bedrock-model-id>",
temperature=0.3,
region_name="us-west-2",
))
Strands 与模型无关——可通过各自的提供方类使用其他提供方(直接调用 Anthropic、OpenAI 等);详见模型提供方文档。
自定义工具
@tool 装饰器可将函数转换为模型可调用的工具。模型会读取文档字符串(第一段作为说明,Args: 用作文档参数说明):
from strands import Agent, tool
@tool
def word_count(text: str) -> str:
"""Count the number of words in a piece of text.
Args:
text: The text to analyze.
"""
return f"{len(text.split())} words"
agent = Agent(tools=[word_count])
遇到可恢复的失败时,返回字符串("Error: ... ask the user to rephrase"),而不是抛出异常——模型会读取返回值并尝试恢复。
预构建工具
from strands_tools import calculator # from the strands-agents-tools package
agent = Agent(tools=[calculator])
多 Agent 模式
有三种经过验证的模式。首先尝试 agents-as-tools(最简单的委派方式):将 Agent 封装在 @tool 中。
from strands import Agent, tool
researcher = Agent(system_prompt="You research topics thoroughly.")
@tool
def research(query: str) -> str:
"""Delegate a research question to the research specialist."""
return str(researcher(query)) # str(AgentResult) = the text output
coordinator = Agent(tools=[research])
coordinator("Research the history of espresso and summarize it.")
如需结构化编排,请使用 Swarm(Agent 动态地相互移交任务)或 Graph(确定性 DAG,其中一个节点的输出会传递给下一个节点):
from strands.multiagent import Swarm, GraphBuilder
# Swarm - dynamic handoffs
swarm = Swarm([researcher, writer, editor])
swarm("Draft and polish an article about espresso.")
# Graph - deterministic pipeline
builder = GraphBuilder()
builder.add_node(researcher, "research")
builder.add_node(writer, "write")
builder.add_edge("research", "write") # research output -> writer input
graph = builder.build()
graph("Write an article about espresso.")
完整的 Graph/Swarm API 请参阅多 Agent 文档。
对话管理(上下文窗口)
Strands 会为你管理对话窗口(这不是长期记忆)。默认采用滑动窗口:
from strands import Agent
from strands.agent.conversation_manager import SlidingWindowConversationManager
agent = Agent(conversation_manager=SlidingWindowConversationManager(window_size=20))
如需持久的跨会话记忆,请使用 AgentCore Memory(参见 aws-harness)或 strands-agents-tools 中的记忆工具。
流式处理
async for event in agent.stream_async("Explain quantum computing"):
print(event)
在 AWS 上部署
Strands 可在任何环境运行。如需在 AWS 上将 Strands Agent 部署为无服务器端点,并使用托管记忆、身份和可观测性功能,请使用 AgentCore 部署框架:aws-harness。
资源
- Strands 文档:https://strandsagents.com/
- Python 快速入门:https://strandsagents.com/docs/user-guide/quickstart/python/
- 多 Agent 模式:https://strandsagents.com/docs/user-guide/concepts/multi-agent/multi-agent-patterns/
- 模型提供方:https://strandsagents.com/docs/user-guide/concepts/model-providers/
- GitHub:https://github.com/strands-agents/sdk-python