第一天,三位本无交集的英雄踏上了书写同一段传说的旅程。来自开发者阵营的 TheWitcherish、来自 Agent 阵营的 Amagash,以及来自构建者阵营的 Tagazok。
而在这一切之上,一位 Orchestrator Agent 俯瞰着这个世界。
本系列分三部分讲述这段冒险:
- 本文:创建你的第一个 Agent,为它配备一个 Vended 工具,然后亲手写一个工具。
- 第 2 部分:通过 Strands Agents Harness SDK 探索 MCP(Model Context Protocol)。
- 第 3 部分:RAG(检索增强生成,Agent 从查到的文档中作答)与 A2A(Agent2Agent,一种让 Agent 与其他 Agent 对话的协议),由一位 Orchestrator Agent 统领多个专用 Agent。
读完第 1 部分,你的 Agent 能讲故事、能在真实的规则 API 中查询法术,还能用你亲手赋予的能力掷出真实的骰子。而且每一项都能在日志中找到证据。
开始上手
要完成英雄们的任务,你需要 Python、uv、一个终端,以及可访问 Amazon Bedrock 的 AWS 凭证。
三位英雄共享同一段冒险,因此你用一个 SDK 编写他们任务中每一步的代码:Strands Agents,一个面向 Python 和 TypeScript 的 AI Agent 开源 SDK。
代码位于 aws-samples/sample-once-upon-agentic-ai。main 分支包含带 # TODO 标记的骨架代码。
git clone https://github.com/aws-samples/sample-once-upon-agentic-ai.git ~/sample-once-upon-agentic-ai
cd ~/sample-once-upon-agentic-ai
uv sync
export AWS_PROFILE=YOUR_AWS_PROFILE AWS_REGION=YOUR_AWS_REGION
还没有配置 AWS profile?Strands Agents 的 Amazon Bedrock 模型提供方指南介绍了如何设置 AWS 凭证(用 aws configure、环境变量、boto3 session 或 aws login),并列出了 Agent 调用模型所需的 IAM 权限。Strands 会自动读取这些凭证。
为什么把 AI Agent 叫作……Agent?
因为 Agent 确实有能动性(agency),所以我们才叫它 Agent。这两个词都源自拉丁语 agere,意为“行动”。模型本身只会作答;Agent 则会决定下一步做什么,并用你给它的工具付诸行动。
Agent 由三部分组成:一个模型、一份带指令的系统提示词,以及它可以使用的工具。你发一条消息,模型决定是否调用工具。它读取结果,然后作答或再调用另一个工具。这个循环就是 Agent 循环。Strands 替你运行这个循环,负责模型调用、工具执行和对话历史,你只需编写指令和工具。
下面看看每个部分为我们的主角提供了什么:
| 我们的主角需要…… | 也就是…… | Strands 提供…… |
|---|---|---|
| 思考和决策 | 一个能读懂局势并选择行动的大脑 | Amazon Bedrock 上的模型(默认模型提供方) |
| 知道自己是谁 | 一条常驻指令 | 系统提示词 |
| 超越言语去行动 | 触达模型之外的能力 | 工具:strands.vended_tools 中的现成工具,或用 @tool 自定义的工具
|
第 1 步给模型配上系统提示词,第 2、3 步给它配上工具。
第 1 步:Agent 需要的不止一行代码吗?
“我们的主角刚刚抵达这片疆域。没有地图,没有武器,连一个咒语都不会。”你的第一个 Agent 登场时也是如此:只有一副嗓音,手中空无一物。
用调试日志、系统提示词和一次调用把它召唤出来:
from strands import Agent
import logging
logging.getLogger("strands").setLevel(logging.DEBUG)
logging.basicConfig(
format="%(levelname)s | %(name)s | %(message)s",
handlers=[logging.StreamHandler()]
)
agent = Agent(
system_prompt="You are a game master for a fantasy role-playing game"
)
response = agent("Hi, I am an adventurer ready for adventure!")
Agent(system_prompt=...) 为模型每一轮提供指令。这里它让 Agent 扮演奇幻角色扮演游戏的叙事者。不传 model 参数时,Strands 默认使用 Amazon Bedrock 上的 Claude Sonnet 4.6。Bedrock 是一项无服务器 AWS 服务,通过其 API 为你运行多家供应商的模型,因此换用其他模型只需改一个 model ID,不必重写 Agent。
cd ~/sample-once-upon-agentic-ai/1_strands_basics
uv run python simple_agent.py
调试日志展示了SDK的执行过程:
DEBUG | strands.models.bedrock | config=<{'model_id': 'global.anthropic.claude-sonnet-4-6', ...}> | initializing
DEBUG | strands.tools.registry | tool_count=<0> | tools configured
DEBUG | strands.models.bedrock | request=<{'modelId': 'global.anthropic.claude-sonnet-4-6', 'messages': [...], 'system': [...]}>
DEBUG | strands.event_loop._retry | stop_reason=<end_turn> | model call succeeded, ...
一次请求,没有工具,stop_reason=<end_turn>:模型一轮就完成了。Agent 开场了一个场景:
火把的光摇曳着,你踏进那间陈旧的木制酒馆。麦酒和木柴烟的气味弥漫在空气中。一个头发花白的酒保从房间另一头打量着你。
接着酒保补了一句:“如今荆棘谷可难得见到新面孔了。”一句话就召唤出一间酒馆,还给小镇起了名字。没有地图,没有武器:tool_count=<0>。保持 DEBUG 开启。这个 Agent 听起来像是懂规则,但它手头没有规则书可查。
第 2 步:Agent 已经会火球术了吗?
“要征服什么,先得把自己武装起来。”
我们的英雄们走进一家号称应有尽有的商店。其中一位饿了。“不行,我们这儿不卖汉堡,”店主回答,“不过还有不少别的东西,应该会让你感兴趣。”
英雄们本以为只有几件小玩意儿,结果发现了一整座宝库。
工具就是一个描述给模型看的函数,由模型决定是否调用。现成的工具放在 strands.vended_tools 里。这里的 http_request 用来调用 Web API:
from strands.vended_tools import http_request
agent = Agent(tools=[http_request], system_prompt=...)
系统提示词指向 Open5e——一个开放的奇幻RPG规则API,然后提问:“火球术有什么效果,能造成多少伤害?”复制第1步的日志代码块,然后运行:
cd ~/sample-once-upon-agentic-ai/2_vended_tools
uv run python agent_with_vended_tools.py
下面是 SDK 执行过程的调用链:
DEBUG | strands.tools.registry | tool_count=<1> | tools configured
DEBUG | strands.event_loop._retry | stop_reason=<tool_use> | model call succeeded, ...
DEBUG | strands.tools.executors._executor | tool_use=<{..., 'name': 'http_request', 'input': {'method': 'GET', 'url': 'https://api.open5e.com/v2/spells/srd_fireball/'}}> | streaming
Tool #1: http_request
第一轮以 stop_reason=<tool_use> 结束。模型拼出 URL,Strands 执行请求,返回结果开头是 {"status": 200, "status_text": "OK", ...}。直到这时,Agent 才给出回答:
Casting Time: Action
Range: 150 feet
...
Failed save: The target takes 8d6 fire damage.
API 返回的 JSON 里写着 "range": 150 和 "casting_time": "action",回复中这两个值原样重现。你没有写任何选工具的代码:工具是模型自己选的,日志可以作证。但商店里卖的,终究是别人做好的工具。
第 3 步:函数本身就是工具吗?
“行囊满满,心情轻快,英雄们继续上路。”
其中两人大步走在前面。落在后面的 TheWitcherish 翻着刚到手的那本魔法书。
突然,他大喊一声“等等!”,宣布自己获得了一种非凡能力。能力是:“现在 13 点 37 分。”队伍毫无反应。接着他冒出个更好的主意:“你们可以自己写能力!”怎么写?“只要创建一个函数,再把它声明为工具就行。”
把函数变成工具,靠的是一个装饰器,也就是函数上方以 @ 开头的那一行。在 Strands 里,这个装饰器是 @tool。自定义工具文档说明了 Strands 如何读取函数的 docstring 和类型提示,据此构建展示给模型的工具规格。
我们的自定义工具是一个掷骰器,这里做了精简:
from strands import Agent, tool
@tool
def roll_dice(faces: int = 6) -> int:
"""Roll one die with a given number of faces and return the result.
...
"""
import random
return random.randint(1, faces)
dice_master = Agent(tools=[roll_dice], system_prompt=...)
系统提示词把 Agent 设定为“幸运女神”,每项属性掷四颗骰子并取最高的三颗。Agent 构建完成后,日志会显示新工具已注册:
DEBUG | strands.tools.registry | tool_name=<roll_dice>, tool_type=<function>, is_dynamic=<False> | registering tool
DEBUG | strands.tools.registry | tool_count=<1> | tools configured
agent built, tools: ['roll_dice']
该方案会打印出 roll_dice.tool_spec,也就是发送给模型的规范:
{'name': 'roll_dice', 'description': 'Roll one die with a given number of faces and return the result.\n\n...', 'inputSchema': {'json': {'properties': {'faces': {'default': 6, ..., 'type': 'integer'}}, 'type': 'object'}}}
docstring 变成了 description,类型提示变成了 'type': 'integer',默认值变成了 'default': 6。模型读的是这份规格说明,而不是你的代码,所以写清楚 docstring 能帮它选对工具。
该掷骰子了!
运行本章代码:
cd ~/sample-once-upon-agentic-ai/3_custom_tools
uv run python agent_with_dice_roll_tool.py
追踪记录展示了 SDK 执行的每一步:
DEBUG | strands.event_loop._retry | stop_reason=<tool_use> | model call succeeded, ...
DEBUG | strands.tools.executors._executor | tool_use=<{..., 'name': 'roll_dice', 'input': {'faces': 6}}> | streaming
...
Tool #1: roll_dice
...
Tool #16: roll_dice
DEBUG | strands.event_loop._retry | stop_reason=<end_turn> | model call succeeded, ...
幸运女神要求一回合掷出全部16颗骰子。Strands 把工具跑了16次,返回结果,第二次模型调用给出了答案:
力量
掷骰:3, 1, 1, 3 -> 弃掉1(其中一颗)-> 3 + 1 + 3 = 7
...
魅力
掷骰:2, 6, 5, 3 -> 弃掉2 -> 6 + 5 + 3 = 14
完整日志里的16条工具结果包含了上述每一个数字,分组方式与幸运女神所用的一致:3, 1, 1, 3;1, 2, 4, 3;3, 2, 1, 2;以及2, 6, 5, 3。
日志就是记录。当我向第1步那个无工具的Agent提出火球问题时,它同样回答了8d6,所以单看回复无法判断工具是否运行过。tool_count、stop_reason以及每一行Tool #才能说明问题。
Tagazok决定自己写一个用@tool装饰的自定义工具:def fire_sword():
这真是太AWSome了!
跟随冒险者们的脚步
在第2部分中,我们的英雄将寻求自身力量之外的帮助。我们为他们配备一个MCP服务器,因为另一只更强大、更威猛的怪物正在等着他们!
在那之前,先给你的Agent养成三个习惯:
- 从系统提示词开始。先定好Agent的角色和规则,再添加工具。
-
为模型写docstring。在模型读到
tool_spec之前,你自己先读一遍。 -
让模型自己选。如果你发现自己正在写
if语句来挑选工具,那这条规则就该放进系统提示词里。
附言:他们的冒险还出了一本漫画书。如果你想要一本,Once Upon Agentic AI with Strands Agents页面列出了北美、欧洲和亚太地区的购买渠道。
本博文中的漫画插图由 Holy Bird 基于 AWS 工作坊内容创作并制作。
版权所有。


