Share
Embed
Strands Agents をはじめる: Agent loop と Model providers | Kiro と学ぶ Strands Agents 01
Level 200。Strands Agents (1.64) を初めて使う Python の開発者向けに、pip install から東京のリージョンの Amazon Bedrock を呼ぶ最初の Agent、@tool のツール、AgentResult までを、本物のコードと出力で見せる回です。Agent loop の回り方と止め方 (stop_reason、cancel()、limits) と、Model providers の替え方 (BedrockModel の引数と権限、structured_output_model、ModelRouter、自作の提供元) を、Strands Agents の公式ドキュメントだけを元に、構成図、流れの図、イメージ図で説明します。例には、架空のネットの家電のお店の問い合わせ窓口のエージェントを使います。シリーズ「Kiro と学ぶ Strands Agents」の 01 (はじめる) です。
Riku INADA PRO
November 09, 2026
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
39 slides
-
Kiro と学ぶ Strands Agents 01 はじめる Strands Agents をはじめる Agent
loop と Model providers Level 200 / Strands Agents 1.64 (2026-11 時点) 2026 年 11 ⽉ 稲⽥ ⼤陸 アマゾン ウェブ サービス ジャパン © 2026, Amazon Web Services, Inc. or its affiliates. All rights reserved. 01 Kiro + Strands Agents 1 -
はじめに ⾃⼰紹介 稲⽥ ⼤陸 いなだ りく (いなりく) アマゾン ウェブ サービス
ジャパン合同会社 ソリューションアーキテクト 製造業のお客様を中⼼に⽀援 最近の取り組み AI 駆動開発ライフサイクルの推進 Kiro と AWS Amplify の情報発信 © 2026, Amazon Web Services, Inc. or its affiliates. All rights reserved. X @inada_riku Speaker Deck speakerdeck.com/inariku Zenn zenn.dev/inariku GitHub github.com/inariku 2 -
はじめに シリーズの中の位置 いまは 01 Strands Agents をはじめる、次は 02 Tools と
MCP はじめる 01–02 作り込む Strands Agents Hooks と Lifecycle 01 をはじめる 03 controls 02 Tools と MCP 04 と Plugins 次に読む 03–07 確かめる Interventions 09 Observability と Evals 09 載せて広げる 08–10 Deploy と Secure 08 for production 10 Strands harness 05 Sessions と Memory output 06 Structured と Streaming 07 Multi-agent © 2026, Amazon Web Services, Inc. or its affiliates. All rights reserved. 3 -
はじめに この資料で作るもの: 東京の Amazon Bedrock を呼ぶ Agent 1 最初の Agent
を動かす SDK を⼊れ、@tool のツールを渡す 2 Agent loop モデルとツールの往復、 ⽌まり⽅、上限 3 モデルを選んで設定する 提供元の替え⽅、BedrockModel、 ModelRouter © 2026, Amazon Web Services, Inc. or its affiliates. All rights reserved. 4 -
01 Strands Agents をはじめる 第 1 部 最初の Agent を動かす
@tool © 2026, Amazon Web Services, Inc. or its affiliates. All rights reserved. 5 -
1 最初の Agent を動かす 始め⽅は 2 つある: Strands harness と
Harness SDK したいこと 始めるもの ドキュメントの理由 決まった既定値の 完成品から始める Strands harness system prompt、tools、memory、 sessions、context management が 組み込み済み 部品を⾃分で 選んで組む Strands Harness SDK 部品とループだけがあり、何も決めていない harness で始め、 あとで細かく決める Strands harness に SDK の部品を⾜す create_harness() はふつうの Agent を 返すので、SDK の機能をそのまま使える このシリーズは Harness SDK で部品を 1 つずつ組む。Strands harness は 10 で扱う © 2026, Amazon Web Services, Inc. or its affiliates. All rights reserved. 6 -
1 最初の Agent を動かす pip install strands-agents で⼊れる 端末 (Quickstart
の命令) uv pip show の出⼒ pip install strands-agents pip install 'strands-agents[anthropic]' Name: strands-agents Version: 1.64.0 Python 3.10 以上の仮想環境で⼊れる。提供元ごとの部品は [anthropic] のような extras で⾜す © 2026, Amazon Web Services, Inc. or its affiliates. All rights reserved. 7 -
1 最初の Agent を動かす Agent() は何も渡さなくても Amazon Bedrock を使う s01_default.py
(モデルは呼ばない) from strands import Agent agent = Agent() print(type(agent.model).__name__) for k, v in agent.model.config.items(): print(k, "=", v) 出⼒ BedrockModel model_id = global.anthropic.claude-sonnet-4-6 include_tool_result_status = auto 既定は us-west-2 の Claude Sonnet 4.6。使うモデルへの アクセスを、Amazon Bedrock の コンソールで有効にしておく © 2026, Amazon Web Services, Inc. or its affiliates. All rights reserved. 8 -
1 最初の Agent を動かす この資料のコードは、agent.py の model で東京を呼ぶ agent.py の
10 ⾏⽬と 19〜22 ⾏⽬ from strands.models import BedrockModel model = BedrockModel( model_id="global.anthropic.claude-sonnet-5", region_name="ap-northeast-1", # 東京 ) ほかのファイルは from agent import model で、 この model を読み込む 認証情報の⽤意 (boto3 が探す) やること AWS CLI aws configure、または aws login (AWS CLI 2.32.0 以上) 環境変数 AWS_ACCESS_KEY_ID と AWS_SECRET_ACCESS_KEY (⼀時的なら AWS_SESSION_TOKEN も) boto3 のセッション BedrockModel(boto_session=boto3.Session(...)) で渡す リージョンは region_name が最優先。渡さないと、boto3 のセッション、AWS_REGION、us-west-2 の順 © 2026, Amazon Web Services, Inc. or its affiliates. All rights reserved. 9 -
1 最初の Agent を動かす ほかの提供元はキーを環境変数で渡す Model のクラス ⼊れる extras Quickstart
で読む環境変数 AnthropicModel strands-agents[anthropic] ANTHROPIC_API_KEY OpenAIModel strands-agents[openai] OPENAI_API_KEY GeminiModel strands-agents[gemini] GEMINI_API_KEY strands-agents[ollama] 要らない (⼿元の ollama serve) OllamaModel Quickstart の Anthropic のコード (この資料では動かしていない) from strands.models.anthropic import AnthropicModel model = AnthropicModel(model_id="claude-sonnet-5", max_tokens=4096) agent = Agent(model=model) © 2026, Amazon Web Services, Inc. or its affiliates. All rights reserved. 10 -
1 最初の Agent を動かす @tool を付けた関数がツールになる tools.py の 28〜34 ⾏⽬
(本体は 35〜38 ⾏⽬) s01_tool.py の出⼒ @tool def lookup_customer(customer_id: str) -> str: """お客さんを ID で調べる。 Tool #1: lookup_customer 登録状態は「有効」です。 end_turn Args: customer_id: お客さんの ID (例: C-1001) """ モデルは docstring と型ヒントを読んで、いつ呼ぶかと何を渡すかを決める © 2026, Amazon Web Services, Inc. or its affiliates. All rights reserved. 11 -
1 最初の Agent を動かす 付属のツールと⾃作のツールを tools に並べる s01_tools_list.py (モデルは呼ばない) from
strands import Agent from strands.vended_tools import file_editor from agent import model from tools import lookup_customer agent = Agent(model=model, tools=[lookup_customer, file_editor]) print(agent.tool_names) 出⼒ ['lookup_customer', 'file_editor'] © 2026, Amazon Web Services, Inc. or its affiliates. All rights reserved. 付属のツール (vended tools) は strands.vended_tools から読み込む 12 -
1 最初の Agent を動かす Agent は model、tools、console への出⼒をつなぐ Agent あなたのコード
agent("...") model AgentResult BedrockModel stop_reason tools lookup_customer、file_editor message metrics (traces は中) callback_handler console に流す (None で⽌める) console への出⼒を⽌めるときは callback_handler=None を渡す © 2026, Amazon Web Services, Inc. or its affiliates. All rights reserved. 13 -
1 最初の Agent を動かす 呼び出しは AgentResult を返し、会話は Agent に残る s01_result.py
の 14〜18 ⾏⽬ (agent は 11〜13 ⾏⽬で、ツール付きで作る) result = agent("C-1001 の登録の状態は︖20 字以内で") print(result.stop_reason) print(len(agent.messages), "messages") print(result.metrics.cycle_count, "cycles") print(str(result).strip()) 出⼒ end_turn 4 messages 2 cycles C-1001(⼭⽥花⼦さん)の登録状態は「有効」です。 © 2026, Amazon Web Services, Inc. or its affiliates. All rights reserved. 答えは result.message、traces は result.metrics の中。会話の履歴は agent.messages 14 -
01 Strands Agents をはじめる BedrockModel end_turn 第 2 部 Agent
loop tool_use @tool © 2026, Amazon Web Services, Inc. or its affiliates. All rights reserved. 15 -
2 Agent loop Agent loop は、ツールを求められなくなるまで回る 頼み モデルを呼ぶ user のメッセージ
BedrockModel 結果を履歴に⾜して ツールを求めた? いいえ 応答を返す end_turn はい もう⼀度呼ぶ ツールを動かす tool result を作る 会話の履歴 (1 周ごとに増え、モデルは毎回それまでの全部を⾒る) © 2026, Amazon Web Services, Inc. or its affiliates. All rights reserved. 16 -
2 Agent loop ドキュメントの例: 5 周で調べて答える 1 2 3 4
5 ファイルの⼀覧 ⼊⼝のファイル DB のモジュール 呼び出し元 12 か所 報告書を返す file listing file reader file reader code search ツールを呼ばない どの周も、モデルが⽂脈を読んで「ツールを呼ぶか、答えるか」を決める コードの脆弱性を調べる頼み。4 周はツールを呼び、5 周⽬はツールを呼ばずに報告書を 返して終わる © 2026, Amazon Web Services, Inc. or its affiliates. All rights reserved. 17 -
2 Agent loop ツールの失敗は、ループを⽌めずにモデルへ戻る ツールの要求 スキーマで検証 レジストリで探す 実⾏する tool result
失敗した error の tool result "status": "error" モデルが別の⼿を試す 例外で⽌めず、エラーの結果をモデルに返すので、モデルが別の⼿を試せる © 2026, Amazon Web Services, Inc. or its affiliates. All rights reserved. 18 -
2 Agent loop stop_reason で、ループが⽌まった理由が分かる stop_reason 意味 (ドキュメントの説明) end_turn モデルが答え終わった
(ふつうの終わり) tool_use ツールを動かして、もう⼀度モデルを呼ぶ (ループは続く) cancelled agent.cancel() で外から⽌めた limit_turns / limit_total_tokens / limit_output_tokens 上限に届いた。履歴は壊れず、上限を上げて呼び直せる max_tokens 応答が上限で切れ、MaxTokensReachedException が出る。上限を 上げて同じ Agent を呼び直せる content_filtered / guardrail_intervened 安全の仕組みが⽌めた。アプリの決まりで扱う ほかに interrupt、stop_sequence、limit_structured_output_attempts がある © 2026, Amazon Web Services, Inc. or its affiliates. All rights reserved. 19 -
2 Agent loop 呼び出し、モデル、ツールの前と後にイベントが出る 1 回の呼び出し モデルの呼び出し ツールの実⾏ BeforeInvocationEvent BeforeModelCallEvent
BeforeToolCallEvent AfterInvocationEvent AfterModelCallEvent AfterToolCallEvent Hooks でこのイベントを購読すると、ループのコードを変えずに⾒て、測って、ふるまいを変えられる (03 で扱う) © 2026, Amazon Web Services, Inc. or its affiliates. All rights reserved. 20 -
2 Agent loop agent.cancel() で、外から⽌める s01_cancel.py の 13〜17 ⾏⽬ 出⼒
agent = Agent(model=model, tools=[lookup_customer], callback_handler=None) threading.Timer(1.0, agent.cancel).start() result = agent("C-1001 と C-1002 を順に調べて") print(result.stop_reason) cancelled スレッドセーフで、何度呼んでもよい。動いていない Agent に呼ぶと、次の 呼び出しが⽌まる © 2026, Amazon Web Services, Inc. or its affiliates. All rights reserved. 21 -
2 Agent loop limits を渡さないと、何周でも回る モデルがツールを求める 限りループは続き、毎周 それまでの会話を全部 送り直す ⻑く動く
Agent には turns か total_tokens の上限を付ける (次の⾯) limits なし © 2026, Amazon Web Services, Inc. or its affiliates. All rights reserved. 22 -
2 Agent loop limits で、1 回の呼び出しに上限を付ける s01_limits.py from strands import
Agent 出⼒ limit_turns from agent import model from tools import lookup_customer agent = Agent(model=model, tools=[lookup_customer], callback_handler=None) result = agent("C-1001 と C-1002 の状態は︖", limits={"turns": 1}) print(result.stop_reason) 上限は周の頭で確かめる。前の周が求めたツールは最後まで動き、履歴は呼び直せる 形で残る © 2026, Amazon Web Services, Inc. or its affiliates. All rights reserved. 23 -
2 Agent loop 呼び出しが重なると履歴が壊れるので、既定では断る 呼び出しは会話の履歴を 書き換える。重なると、 履歴を壊しうる 既定の THROW では、
重なった呼び出しが ConcurrencyException で断られる 同じ Agent © 2026, Amazon Web Services, Inc. or its affiliates. All rights reserved. 並べて動かすなら、 仕事ごとに別の Agent を作る 24 -
01 Strands Agents をはじめる 第 3 部 モデルを選んで 設定する ©
2026, Amazon Web Services, Inc. or its affiliates. All rights reserved. ModelRouter no-such-model end_turn 25 -
3 モデルを選んで設定する streaming などの主な機能は、公式の提供元ならどれも同じ 提供元 Streaming Tool calling Structured output
Prompt caching Amazon Bedrock ◦ ◦ ◦ ◦ Anthropic ◦ ◦ ◦ ◦ OpenAI ◦ ◦ ◦ Google ◦ ◦ ◦ Ollama ◦ ◦ ◦ LiteLLM ◦ ◦ ◦ ◦ 表はドキュメントの表の⼀部。prompt caching の ◦ は、要求に cache point を書き込む提供元だけ © 2026, Amazon Web Services, Inc. or its affiliates. All rights reserved. 26 -
3 モデルを選んで設定する model に渡すインスタンスを替えるだけで、モデルが替わる s01_swap.py (モデルは呼ばない) from strands import Agent
from strands.models import BedrockModel tokyo = BedrockModel( model_id="global.anthropic.claude-sonnet-5", region_name="ap-northeast-1") for m in [None, tokyo]: agent = Agent(model=m) print(agent.model.config["model_id"]) 出⼒ global.anthropic.claude-sonnet-4-6 global.anthropic.claude-sonnet-5 © 2026, Amazon Web Services, Inc. or its affiliates. All rights reserved. 10 枚⽬の AnthropicModel などに替えても、 ほかのコードは同じ 27 -
3 モデルを選んで設定する BedrockModel で呼ぶには、2 つの権限が要る IAM ポリシー (ドキュメントの例、字下げは 2 桁)
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "bedrock:InvokeModelWithResponseStream", "bedrock:InvokeModel" ], "Resource": "*" } ] Action の 1 ⾏⽬は、 ストリーミング (既定) で呼ぶ ときに要る 2 ⾏⽬は、streaming=False で 呼ぶときに要る 本番では Resource の "*" を、 使うモデルの ARN に絞る } © 2026, Amazon Web Services, Inc. or its affiliates. All rights reserved. 28 -
3 モデルを選んで設定する BedrockModel のよく使う引数 引数 ドキュメントの説明 model_id Bedrock のモデルの ID
temperature ばらつき (⼤きいほどばらつく) max_tokens ⽣成するトークンの上限。既定はモデルの出⼒の上限 streaming ストリーミングの有無 guardrail_id かけるガードレールの ID cache_config prompt caching の設定 (例 CacheConfig(strategy="auto")) boto_session AWS の認証情報に使う boto3 のセッション additional_request_fields モデルごとの追加の引数 ほかに strict_tools と api_key がある。cache_prompt と cache_tools は⾮推奨 (cache_config を使う) © 2026, Amazon Web Services, Inc. or its affiliates. All rights reserved. 29 -
3 モデルを選んで設定する structured_output_model で、型のある結果を受け取る s01_structured.py の 12〜21 ⾏⽬ class Customer(BaseModel):
customer_id: str name: str status: str 値の間違いはモデルに返して 直させ、型にならなければ StructuredOutputException agent = Agent(model=model, tools=[lookup_customer], callback_handler=None) result = agent("C-1001 を調べて", structured_output_model=Customer) print(repr(result.structured_output)) 出⼒ Customer(customer_id='C-1001', name='⼭⽥ 花⼦', status='有効') © 2026, Amazon Web Services, Inc. or its affiliates. All rights reserved. 30 -
3 モデルを選んで設定する Bedrock を呼ぶ道は 2 つある bedrock-mantle だけのモデルや Converse API
に無い機能は、提供元の API で呼ぶ © 2026, Amazon Web Services, Inc. or its affiliates. All rights reserved. 31 -
3 モデルを選んで設定する ModelRouter は、候補と RoutingStrategy でできている ModelRouter RoutingStrategy Agent model=ModelRouter(...)
既定は FallbackStrategy 1 つ⽬ = 既定の候補 最初の候補と切り替えを決める primary_model 失敗したら 名前や説明を付けるなら model に渡す。 plugin には⼊れない © 2026, Amazon Web Services, Inc. or its affiliates. All rights reserved. 候補 = モデルか ModelRouter RoutingCandidate で包む 2 つ⽬ backup_model 32 -
3 モデルを選んで設定する 1 つ⽬のモデルが失敗したら、2 つ⽬に回す s01_router.py の 11〜18 ⾏⽬ (1
つ⽬はわざと無い model_id) broken = BedrockModel(model_id="no-such-model", region_name="ap-northeast-1") router = ModelRouter(models=[broken, model], max_switches=1) agent = Agent(model=router, callback_handler=None) result = agent("こんにちは。1 ⽂で答えて") print(result.stop_reason) print(str(result).strip()) 出⼒ end_turn こんにちは、今⽇はどのようにお⼿伝いできますか。 © 2026, Amazon Web Services, Inc. or its affiliates. All rights reserved. max_switches は 1 回の呼び出しで 候補を替える回数の上限で、同じ モデルの再試⾏は数えない 33 -
3 モデルを選んで設定する ⾃作の提供元は、Model を継承して作る Model を継承したクラス Agent loop stream() 応答を組み⽴て
update_config() ツールを動かす あなたのモデルの API 要求と応答 get_config() StreamEvent structured_output() messages、tool specs、system prompt を API の要求に変換して送り、応答を StreamEvent に変換して返す stream() は、StreamEvent を返す async のジェネレーターで書く © 2026, Amazon Web Services, Inc. or its affiliates. All rights reserved. 34 -
まとめ まとめ 1 Agent(model=..., tools=[...]) で作り、agent("...") で呼ぶ。返るのは AgentResult 2 Agent
loop はツールを求められなくなるまで回る。⽌まった理由は stop_reason、上限は limits 3 モデルは Model のインスタンスを替えるだけ。ふだんは BedrockModel、 失敗に備えるなら ModelRouter © 2026, Amazon Web Services, Inc. or its affiliates. All rights reserved. 35 -
まとめ 次に読む資料 02 Tools と MCP @tool の書き⽅、付属のツール、MCP サーバーのツールをつなぐ 03
Hooks と Lifecycle controls この回で⾒たイベントと上限を、コードで使う © 2026, Amazon Web Services, Inc. or its affiliates. All rights reserved. 36 -
付録 A1 この資料で動かしたコード ファイル 何をするか agent.py 東京の Amazon Bedrock を呼ぶ
model を作る (ほかのファイルが読み込む) s01_default.py Agent() の既定のモデルを出す (モデルは呼ばない) s01_tool.py @tool の lookup_customer を渡して呼ぶ s01_tools_list.py 付属のツールと⾃作のツールを並べる s01_result.py AgentResult と会話の履歴を⾒る s01_cancel.py agent.cancel() で⽌める s01_limits.py limits={"turns": 1} で⽌める s01_swap.py BedrockModel を替える s01_structured.py structured_output_model で型のある結果 s01_router.py ModelRouter で 2 つ⽬に回す 東京の Amazon Bedrock (global.anthropic.claude-sonnet-5) で 2026-11-10 に動かした © 2026, Amazon Web Services, Inc. or its affiliates. All rights reserved. 37 -
付録 出典 AWS、2026-11、Strands Agents のドキュメント「Strands Harness SDK」 (6 枚⽬) https://strandsagents.com/docs/user-guide/sdk/
AWS、2026-11、Strands Agents のドキュメント「Compose with the Strands Harness SDK」 (6 枚⽬) https://strandsagents.com/docs/user-guide/harness/composing-with-sdk/ AWS、2026-11、Strands Agents のドキュメント「Quickstart (Python)」 (4、7、8、10〜14、35 枚⽬) https://strandsagents.com/docs/user-guide/sdk/quickstart/python/ AWS、2026-11、Strands Agents のドキュメント「Agent loop」 (16〜24、35 枚⽬) https://strandsagents.com/docs/user-guide/sdk/agents/agent-loop/ AWS、2026-11、Strands Agents のドキュメント「State」 (14 枚⽬) https://strandsagents.com/docs/user-guide/sdk/agents/state/ AWS、2026-11、Strands Agents のドキュメント「Hook events」 (20 枚⽬) https://strandsagents.com/docs/user-guide/sdk/agents/hooks-events/ © 2026, Amazon Web Services, Inc. or its affiliates. All rights reserved. 38 -
付録 出典 (続き) AWS、2026-11、Strands Agents のドキュメント「Production lifecycle controls」 (19、21 枚⽬)
https://strandsagents.com/docs/user-guide/sdk/agents/lifecycle-controls/ AWS、2026-11、Strands Agents のドキュメント「Model providers」 (7、26、27、35 枚⽬) https://strandsagents.com/docs/user-guide/sdk/model-providers/ AWS、2026-11、Strands Agents のドキュメント「Amazon Bedrock」 (4、9、28、29、31 枚⽬) https://strandsagents.com/docs/user-guide/sdk/model-providers/amazon-bedrock/ AWS、2026-11、Strands Agents のドキュメント「Model routing」 (32、33 枚⽬) https://strandsagents.com/docs/user-guide/sdk/model-providers/model-routing/ AWS、2026-11、Strands Agents のドキュメント「Custom model provider」 (34 枚⽬) https://strandsagents.com/docs/user-guide/sdk/model-providers/custom_model_provider/ AWS、2026-11、Strands Agents のドキュメント「Return structured output」 (30 枚⽬) https://strandsagents.com/docs/user-guide/sdk/agents/structured-output/ 問い合わせ窓⼝のエージェントで動かしたコードとその出⼒ (公開していない)、strands-agents 1.64.0、 東京の Amazon Bedrock、2026-11-10 (7〜9、11、12、14、21、23、27、30、33 枚⽬) © 2026, Amazon Web Services, Inc. or its affiliates. All rights reserved. 39