View a markdown version of this page

組態套件入門 - Amazon Bedrock AgentCore

組態套件入門

此逐步解說會將您從零移至您的代理程式在執行時間讀取的版本控制組態套件。您將建立套件、更新套件以產生新版本、讀取組態,以及檢視版本歷史記錄。

開始之前

請確認您已完成以下項目:

  • 具有部署至 AgentCore 執行期之代理程式的 AgentCore CLI 專案 (或遵循下列附錄建立具有組態套件支援的專案)

  • AWS 具有 bedrock-agentcore和 許可的 登入資料 bedrock-agentcore-control(請參閱先決條件)

boto3 範例中使用下列常數。將它們取代為您自己的值:

REGION = "us-west-2" BUNDLE_ID = "myAgentConfig-a1b2c3d4e5" # from create response COMPONENT_ARN = "arn:aws:bedrock-agentcore:us-west-2:123456789012:runtime/MyAgent-abc123"

步驟 1:建立組態套件

使用代理程式的初始組態建立套件。套件存放由元件 ARN 鍵入的鍵值對 (系統提示、模型 ID、溫度、工具描述)。

範例
AgentCore CLI
agentcore add config-bundle \ --name myAgentConfig \ --components '{"arn:aws:bedrock-agentcore:us-west-2:123456789012:runtime/MyAgent-abc123": {"configuration": {"system_prompt": "You are a helpful customer support assistant for Acme Store.", "model_id": "global.anthropic.claude-sonnet-4-5-20250929-v1:0"}}}' agentcore deploy

部署之後,CLI 會列印套件 ID 和初始版本 ID。

AWS SDK (boto3)
import boto3 import uuid client = boto3.client("bedrock-agentcore-control", region_name=REGION) response = client.create_configuration_bundle( bundleName="myAgentConfig", components={ COMPONENT_ARN: { "configuration": { "system_prompt": "You are a helpful customer support assistant for Acme Store.", "model_id": "global.anthropic.claude-sonnet-4-5-20250929-v1:0", "temperature": 0.7, } } }, description="Acme Store agent configuration", commitMessage="Initial configuration", clientToken=str(uuid.uuid4()), ) bundle_id = response["bundleId"] version_id = response["versionId"] print(f"Created bundle: {bundle_id}") print(f"Initial version: {version_id}")

步驟 2:更新套件

執行批次評估並識別代理程式的回應過於詳細後,請更新系統提示。每次更新都會建立新的不可變版本。

範例
AgentCore CLI

在 中編輯套件組態agentcore.json,然後重新部署:

# Edit the system_prompt in agentcore.json, then: agentcore deploy

CLI 會偵測現有的套件,並自動建立新的版本。

AWS SDK (boto3)
response = client.update_configuration_bundle( bundleId=bundle_id, components={ COMPONENT_ARN: { "configuration": { "system_prompt": ( "You are a helpful customer support assistant for Acme Store. " "Be concise. Confirm actions taken in one sentence." ), "model_id": "global.anthropic.claude-sonnet-4-5-20250929-v1:0", "temperature": 0.5, } } }, parentVersionIds=[version_id], commitMessage="Reduce verbosity: shorter prompt, lower temperature", clientToken=str(uuid.uuid4()), ) new_version_id = response["versionId"] print(f"New version: {new_version_id}")

步驟 3:讀取套件

擷取目前的組態,以驗證更新是否生效。

範例
AgentCore CLI
agentcore config-bundle versions --name myAgentConfig

輸出顯示具有遞交訊息的版本歷史記錄:

Version                               Branch     Created              Message
────────────────────────────────────────────────────────────────────────────────
a1b2c3d4-...                          mainline   2026-04-28 03:00     Reduce verbosity: shorter prompt, lower temperature
e5f6a7b8-...                          mainline   2026-04-28 02:30     Initial configuration
AWS SDK (boto3)
response = client.get_configuration_bundle(bundleId=bundle_id) config = response["components"][COMPONENT_ARN]["configuration"] print(f"Version: {response['versionId']}") print(f"System prompt: {config['system_prompt']}") print(f"Model: {config['model_id']}") print(f"Temperature: {config.get('temperature')}")

步驟 4:比較版本

區分兩個版本,以查看確切的變更:

範例
AgentCore CLI
agentcore config-bundle diff --name myAgentConfig --from <version-1> --to <version-2>
AWS SDK (boto3)
# Fetch both versions v1 = client.get_configuration_bundle_version(bundleId=bundle_id, versionId=version_id) v2 = client.get_configuration_bundle_version(bundleId=bundle_id, versionId=new_version_id) # Compare v1_config = v1["components"][COMPONENT_ARN]["configuration"] v2_config = v2["components"][COMPONENT_ARN]["configuration"] for key in set(list(v1_config.keys()) + list(v2_config.keys())): if v1_config.get(key) != v2_config.get(key): print(f"{key}:") print(f" before: {v1_config.get(key)}") print(f" after: {v2_config.get(key)}")

後續步驟

  • 執行批次評估,以測量組態變更的影響。請參閱批次評估

  • 使用建議讓服務自動產生最佳化提示。請參閱建議

  • 設定 A/B 測試,將兩個套件版本與即時流量進行比較。請參閱 A/B 測試

  • 分支實驗的組態,而不會影響主線。請參閱更新組態套件

附錄:具有組態套件整合的代理程式程式碼

下列代理程式程式碼會在執行時間使用BeforeModelCallEvent勾點模式讀取作用中的組態套件。代理程式會在模組層級建立一次,掛接會在每個模型呼叫之前動態更新系統提示。組態套件變更會立即生效,而不會重新啟動。

  • SDK 需求:需要 1.8 bedrock-agentcore-sdk-python版或更新版本。上的 get_config_bundle()方法可從此版本開始BedrockAgentCoreContext使用。

將此儲存為客服人員的 main.py

"""Agent with Configuration Bundle integration — hooks pattern.""" from strands import Agent, tool from strands.models.bedrock import BedrockModel from strands.hooks.events import BeforeModelCallEvent from bedrock_agentcore.runtime import BedrockAgentCoreApp, BedrockAgentCoreContext app = BedrockAgentCoreApp() DEFAULT_MODEL_ID = "global.anthropic.claude-sonnet-4-5-20250929-v1:0" DEFAULT_SYSTEM_PROMPT = ( "You are a helpful customer support assistant for Acme Store. " "Help customers with their orders, returns, and shipping questions." ) @tool def lookup_order(order_id: str) -> str: """Look up an order by ID.""" orders = { "ORD-1001": {"status": "delivered", "item": "Blue T-Shirt (L)", "total": "$29.99"}, "ORD-1002": {"status": "in_transit", "item": "Running Shoes (10)", "total": "$89.99"}, "ORD-1003": {"status": "delayed", "item": "Wireless Headphones", "days_late": 5, "total": "$59.99"}, } return str(orders.get(order_id, {"error": f"Order {order_id} not found"})) @tool def check_shipping_status(order_id: str) -> str: """Check detailed shipping status for an order.""" statuses = { "ORD-1002": "Package is with carrier, on schedule for April 5.", "ORD-1003": "Package delayed 5 days. Policy: 3+ days late qualifies for 15% discount.", } return statuses.get(order_id, f"No active shipment found for {order_id}.") TOOLS = [lookup_order, check_shipping_status] def dynamic_config_hook(event: BeforeModelCallEvent): """Read config bundle and apply system prompt before every model call.""" config_bundle = BedrockAgentCoreContext.get_config_bundle() event.agent.system_prompt = config_bundle.get("system_prompt", DEFAULT_SYSTEM_PROMPT) agent = Agent( model=BedrockModel(model_id=DEFAULT_MODEL_ID), tools=TOOLS, system_prompt=DEFAULT_SYSTEM_PROMPT, ) agent.hooks.add_callback(BeforeModelCallEvent, dynamic_config_hook) @app.entrypoint def invoke(payload, context): result = agent(payload.get("prompt", "Hello")) return {"response": str(result)} if __name__ == "__main__": app.run()