View a markdown version of this page

Amazon Bedrock AgentCore 入門 - Amazon Bedrock AgentCore

Amazon Bedrock AgentCore 入門

此快速入門可讓您使用 AgentCore CLI,在幾分鐘內從零到執行中的代理程式。您將安裝 CLI、堆疊專案、在本機測試 AWS、部署至 並叫用您的代理程式。

在 AgentCore 上建置代理程式的兩種方式,相同的 CLI:

  • 受管繫帶。您在組態檔案中宣告代理程式 (模型、提示、工具、記憶體),AgentCore 會為您執行迴圈。無架構,無協同運作程式碼。當您想要從想法到執行中代理程式的最快路由時,路徑良好。進一步了解。

  • 程式碼型代理程式。您可以使用已知的架構 (Strands、LangGraph、Google ADK 或 OpenAI 代理程式) 在 Python 中編寫代理程式迴圈,並將其部署到 AgentCore 執行期。完全控制協同運作邏輯。

此頁面會逐步解說程式碼型流程。如需 繫帶,請參閱什麼是 AgentCore 繫帶

註冊 AWS 帳戶

註冊 AWS 帳戶

若要開始使用 AWS,您需要 AWS 帳戶。如需建立 AWS 帳戶的資訊,請參閱《AWS 帳戶管理參考指南》中的 帳戶入門 AWS

先決條件

  • Node.js 20 或更新版本。AgentCore CLI 以 npm 套件的形式分佈。使用 檢查 node --version。如有需要,請從 nodejs.org 安裝 。

  • npm。隨附於 Node.js。

  • 已設定登入資料的 AWS 帳戶。透過 AWS CLI、環境變數或 AWS 設定檔設定 。請參閱設定 AWS CLI

  • Python 3.10 或更新版本 (適用於代理程式程式碼)。使用 檢查 python3 --version

  • IAM 許可。您的身分需要許可才能進行 AgentCore API 呼叫,並擔任部署期間使用的 CDK 引導角色。請參閱 AgentCore CLI IAM 許可

步驟 1:安裝 AgentCore CLI

npm install -g @aws/agentcore

驗證:

agentcore --version

若要稍後更新,請重新執行安裝命令或 agentcore update。來源和問題:GitHub 上的 agentcore-cli

步驟 2:建立您的專案

agentcore create

互動式精靈會先詢問您要建置的內容:

  • Harness - 受管組態型代理程式迴圈。不需要架構或協同運作程式碼。請參閱什麼是 AgentCore 繫帶

  • 代理程式 - 使用您選擇的架構、部署到 AgentCore 執行期的程式碼型代理程式。

  • 略過 - 建立不含代理程式的專案結構。稍後使用 新增一個agentcore add

如果您選擇 代理程式,精靈會繼續:

  • 架構 - Strands Agents (建議)、LangChain/LangGraph、Google Agent Development Kit 或 OpenAI Agents SDK

  • 模型提供者 - Amazon Bedrock、Anthropic、OpenAI 或 Gemini

  • 記憶體 - 無、短期或長期和短期

  • 組建類型 - CodeZip (預設) 或 Container

您也可以直接傳遞旗標來建立程式碼型代理程式:

agentcore create \ --name MyAgent \ --framework Strands \ --model-provider Bedrock \ --memory none \ --build CodeZip

專案結構

agentcore create 會產生:

MyAgent/
├── agentcore/
│   ├── agentcore.json      # Project and resource configuration
│   ├── aws-targets.json    # Deployment target (account and region)
│   └── cdk/                # CDK infrastructure (auto-managed)
└── app/
    └── MyAgent/            # Your agent code
        ├── main.py         # Agent entrypoint
        ├── pyproject.toml  # Python dependencies
        └── ...

金鑰檔案:

  • agentcore/agentcore.json - 主要組態。定義您的代理程式、記憶體存放區、閘道、登入資料和其他資源。由 agentcore add和 管理agentcore remove

  • app/ - 您的代理程式程式碼。每個客服人員都會使用進入點和 取得自己的子目錄pyproject.toml

  • agentcore/aws-targets.json - 部署的帳戶 AWS 和區域。

步驟 3:在本機測試

cd MyAgent agentcore dev

agentcore dev 會建立 Python 虛擬環境、安裝相依性、啟動具有熱重新載入的本機伺服器,並在瀏覽器中開啟代理程式檢查程式,讓您可以與代理程式聊天、檢查追蹤和瀏覽專案資源。程式碼變更會自動取得。

有用的旗標:

  • --no-browser - 使用終端機型 TUI 而非瀏覽器檢查器。

  • --no-traces - 停用將追蹤寫入 agentcore/.cli/traces

  • --logs - 處於非互動式模式的結尾伺服器日誌。

  • --port <N> - 固定開發連接埠 (預設值為 HTTP 8080、MCP 8000、A2A 9000;忙碌時自動遞增)。

步驟 4:部署您的代理程式

agentcore deploy

部署:

  1. 將您的程式碼封裝成 zip 成品 (如果 ,則建置容器--build Container)

  2. 在幕後使用 AWS CDK 來合成和佈建資源

  3. 為您的代理程式建立 AgentCore 執行期端點

  4. 設定 CloudWatch 記錄和可觀測性

第一次部署需要幾分鐘的時間,而 CDK 會引導您的帳戶。後續部署速度更快。

預覽在不部署的情況下變更的內容:

agentcore deploy --dry-run

檢查狀態:

agentcore status

步驟 5:叫用您部署的代理程式

agentcore invoke --prompt "Hello, what can you do?"

如果您的代理程式已設定付款,請在調用時提供付款內容:

agentcore invoke \ --prompt "Access https://example-x402-merchant.com/paid-api" \ --payment-instrument-id <INSTRUMENT_ID> \ --auto-session \ --payment-user-id user@example.com

這就是迴圈。在 上反覆運算app/MyAgent/main.py、使用 進行測試agentcore dev、使用 部署agentcore deploy、使用 叫用 agentcore invoke

將功能新增至您的專案

agentcore add 管理 中的資源agentcore.json。在沒有互動式功能表引數的情況下執行它,或直接將資源設為目標。

agentcore add memory # Store conversation context agentcore add agent # Add a second agent to the same project agentcore add gateway # Connect external APIs/tools through Gateway agentcore add credential # Add an API key for a non-Bedrock provider agentcore add evaluator # Quality evaluation agentcore add payment-manager # Payments: create a payment manager agentcore add payment-connector # Payments: link a payment provider

每個新增命令會堆疊組態,並提示必要值。新增後,執行 agentcore deploy以佈建。

深入探討您可以連接的功能:

檢視日誌和追蹤

# Stream recent logs agentcore logs # Filter agentcore logs --since 30m --level error agentcore logs --query "timeout" # List recent traces agentcore traces list # Get a specific trace agentcore traces get <trace-id>

清除

agentcore remove all agentcore deploy

remove all 會重設組態。追蹤會deploy偵測空白狀態,並縮減您帳戶中的資源。

後續步驟