用途
Strands Agents SDK 是一个开源框架,用于编写 AI 智能体的逻辑,即智能体的“大脑”。它模型无关,默认使用 AWS Bedrock,可运行在笔记本、容器、Lambda、EC2 等任意环境。
该框架与 AWS 托管部署的关系:Strands 是框架(定义智能体做什么);若要在 AWS 上托管和部署 Strands 智能体,可使用 AgentCore 运行时。两者可组合使用:用 Strands 编写,用 AgentCore 发布。也可以完全不进行 AWS 部署而直接运行 Strands。
安装
pip install strands-agents strands-agents-tools
另存在 TypeScript SDK,详见官方文档。以下示例均为 Python。
快速开始
from strands import Agent
agent = Agent() # 默认使用 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
# 简单方式:传入 Bedrock 模型 id(从 Bedrock 模型目录复制准确 id)
agent = Agent(model="<your-bedrock-model-id>")
# 完全控制:
agent = Agent(model=BedrockModel(
model_id="<your-bedrock-model-id>",
temperature=0.3,
region_name="us-west-2",
))
Strands 模型无关——其他提供方(Anthropic 直连、OpenAI 等)可通过各自的提供方类使用,详见模型提供方文档。
自定义工具
@tool 装饰器将函数转换为模型可调用的工具。docstring 会被模型读取(第一段为描述,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 # 来自 strands-agents-tools 包
agent = Agent(tools=[calculator])
多智能体模式
有三种已验证的模式。先从 agents-as-tools(最简单的委派)开始:将智能体包装进 @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) = 文本输出
coordinator = Agent(tools=[research])
coordinator("Research the history of espresso and summarize it.")
如需结构化编排,可使用 Swarm(智能体之间动态交接)或 Graph(确定性 DAG,一个节点的输出馈入下一个节点):
from strands.multiagent import Swarm, GraphBuilder
# Swarm - 动态交接
swarm = Swarm([researcher, writer, editor])
swarm("Draft and polish an article about espresso.")
# Graph - 确定性流水线
builder = GraphBuilder()
builder.add_node(researcher, "research")
builder.add_node(writer, "write")
builder.add_edge("research", "write") # research 输出 -> writer 输入
graph = builder.build()
graph("Write an article about espresso.")
完整的 Graph/Swarm API 见多智能体文档。
对话管理(上下文窗口)
Strands 会为你管理对话窗口(注意:这不是长期记忆)。默认是滑动窗口:
from strands import Agent
from strands.agent.conversation_manager import SlidingWindowConversationManager
agent = Agent(conversation_manager=SlidingWindowConversationManager(window_size=20))
如需持久化、跨会话记忆,可使用 AgentCore Memory 或 strands-agents-tools 中的记忆工具。
流式输出
async for event in agent.stream_async("Explain quantum computing"):
print(event)
在 AWS 上部署
Strands 可在任意环境运行。若要将 Strands 智能体作为无服务器端点部署到 AWS,并具备托管记忆、身份和可观测性,可使用 AgentCore 运行时。
资源
- Strands 文档:https://strandsagents.com/
- Python 快速开始:https://strandsagents.com/docs/user-guide/quickstart/python/
- 多智能体模式: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
适用场景与限制
适用场景:需要用 Python 编写智能体逻辑、定义工具、构建多智能体系统的开发者;希望模型无关、可本地或云端灵活运行的智能体项目。
限制:资料未提供具体收费、价格、性能指标或平台兼容性细节;长期记忆需借助 AgentCore Memory 或记忆工具,框架自带的对话窗口管理并非长期记忆。