View a markdown version of this page

速率限制 API 範例 - Amazon Bedrock AgentCore

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

速率限制 API 範例

下列範例示範如何使用 CLI 和 AWS Python SDK (Boto3) AWS 管理速率限制。

建立每個目標requests-per-second數限制

下列範例會建立速率限制,以控制每個目標每秒的請求。特定目標取得 100 個 RPS,所有其他目標則各自取得自己的 10 個 RPS 儲存貯體。

範例
AWS CLI
  1. 執行以下命令:

    aws bedrock-agentcore-control create-gateway-rate-limit \ --gateway-identifier my-gateway-abc1234567 \ --dimension-keys '["targetName"]' \ --description "Per-target RPS limit" \ --entries '[ { "dimensions": {"targetName": "my-high-traffic-target"}, "requests": [ { "rate": 100, "period": "second" } ] }, { "dimensions": {"targetName": "*"}, "requests": [ { "rate": 10, "period": "second" } ] } ]'
AWS Python SDK (Boto3)
  1. import boto3 client = boto3.client("bedrock-agentcore-control", region_name="us-west-2") response = client.create_gateway_rate_limit( gatewayIdentifier="my-gateway-abc1234567", dimensionKeys=["targetName"], description="Per-target RPS limit", entries=[ { "dimensions": {"targetName": "my-high-traffic-target"}, "requests": [ { "rate": 100, "period": "second", } ], }, { "dimensions": {"targetName": "*"}, "requests": [ { "rate": 10, "period": "second", } ], }, ], ) print(f"Rate Limit ID: {response['rateLimitId']}")

建立每個來電者requests-per-minute限制

下列範例會根據發起人的 JWT sub宣告建立速率限制。高級使用者可獲得 300 RPM,所有其他使用者則可獲得 60 RPM。

範例
AWS CLI
  1. 執行以下命令:

    aws bedrock-agentcore-control create-gateway-rate-limit \ --gateway-identifier my-gateway-abc1234567 \ --dimension-keys '["$.context.jwt.sub"]' \ --description "Per-caller RPM by subscription tier" \ --entries '[ { "dimensions": {"$.context.jwt.sub": "premium-user-001"}, "requests": [ { "rate": 300, "period": "minute" } ] }, { "dimensions": {"$.context.jwt.sub": "premium-user-002"}, "requests": [ { "rate": 300, "period": "minute" } ] }, { "dimensions": {"$.context.jwt.sub": "*"}, "requests": [ { "rate": 60, "period": "minute" } ] } ]'
AWS Python SDK (Boto3)
  1. import boto3 client = boto3.client("bedrock-agentcore-control", region_name="us-west-2") response = client.create_gateway_rate_limit( gatewayIdentifier="my-gateway-abc1234567", dimensionKeys=["$.context.jwt.sub"], description="Per-caller RPM by subscription tier", entries=[ { "dimensions": {"$.context.jwt.sub": "premium-user-001"}, "requests": [ { "rate": 300, "period": "minute", } ], }, { "dimensions": {"$.context.jwt.sub": "premium-user-002"}, "requests": [ { "rate": 300, "period": "minute", } ], }, { "dimensions": {"$.context.jwt.sub": "*"}, "requests": [ { "rate": 60, "period": "minute", } ], }, ], ) print(f"Rate Limit ID: {response['rateLimitId']}")

使用字符建立多維度限制

下列範例會使用三個維度索引鍵建立速率限制,以限制每分鐘的請求和字符。每個發起人都會取得自己的預算範圍,範圍限定於特定目標和模型。

範例
AWS CLI
  1. 執行以下命令:

    aws bedrock-agentcore-control create-gateway-rate-limit \ --gateway-identifier my-gateway-abc1234567 \ --dimension-keys '["targetName", "qualifiedModelId", "$.context.jwt.sub"]' \ --description "Per-caller token and request budget per target and model" \ --entries '[ { "dimensions": {"targetName": "my-inference-target", "qualifiedModelId": "anthropic.claude-3-sonnet-20240229-v1:0", "$.context.jwt.sub": "*"}, "requests": [ { "rate": 100, "period": "minute" } ], "tokens": [ { "rate": 50000, "period": "minute" } ] }, { "dimensions": {"targetName": "*", "qualifiedModelId": "*", "$.context.jwt.sub": "*"}, "requests": [ { "rate": 30, "period": "minute" } ], "tokens": [ { "rate": 10000, "period": "minute" } ] } ]'
AWS Python SDK (Boto3)
  1. import boto3 client = boto3.client("bedrock-agentcore-control", region_name="us-west-2") response = client.create_gateway_rate_limit( gatewayIdentifier="my-gateway-abc1234567", dimensionKeys=["targetName", "qualifiedModelId", "$.context.jwt.sub"], description="Per-caller token and request budget per target and model", entries=[ { "dimensions": {"targetName": "my-inference-target", "qualifiedModelId": "anthropic.claude-3-sonnet-20240229-v1:0", "$.context.jwt.sub": "*"}, "requests": [ { "rate": 100, "period": "minute", } ], "tokens": [ { "rate": 50000, "period": "minute", } ], }, { "dimensions": {"targetName": "*", "qualifiedModelId": "*", "$.context.jwt.sub": "*"}, "requests": [ { "rate": 30, "period": "minute", } ], "tokens": [ { "rate": 10000, "period": "minute", } ], }, ], ) print(f"Rate Limit ID: {response['rateLimitId']}")

建立連線速率限制

下列範例會為每個模型建立連線速率限制。這會限制每個模型的並行處理中請求數量,這有助於保護並行容量有限的目標。

範例
AWS CLI
  1. 執行以下命令:

    aws bedrock-agentcore-control create-gateway-rate-limit \ --gateway-identifier my-gateway-abc1234567 \ --dimension-keys '["qualifiedModelId"]' \ --description "Connection rate limit per model" \ --entries '[ { "dimensions": {"qualifiedModelId": "anthropic.claude-3-sonnet-20240229-v1:0"}, "connections": [ { "rate": 50, "period": "second" } ] }, { "dimensions": {"qualifiedModelId": "*"}, "connections": [ { "rate": 20, "period": "second" } ] } ]'
AWS Python SDK (Boto3)
  1. import boto3 client = boto3.client("bedrock-agentcore-control", region_name="us-west-2") response = client.create_gateway_rate_limit( gatewayIdentifier="my-gateway-abc1234567", dimensionKeys=["qualifiedModelId"], description="Connection rate limit per model", entries=[ { "dimensions": {"qualifiedModelId": "anthropic.claude-3-sonnet-20240229-v1:0"}, "connections": [ { "rate": 50, "period": "second", } ], }, { "dimensions": {"qualifiedModelId": "*"}, "connections": [ { "rate": 20, "period": "second", } ], }, ], ) print(f"Rate Limit ID: {response['rateLimitId']}")

封鎖速率為零的來電者

下列範例透過將特定呼叫者率設定為 0 來封鎖特定呼叫者。一旦變更傳播 (最多 30 秒),這會拒絕來自封鎖發起人的所有請求。

範例
AWS CLI
  1. 執行以下命令:

    aws bedrock-agentcore-control create-gateway-rate-limit \ --gateway-identifier my-gateway-abc1234567 \ --dimension-keys '["$.context.jwt.sub"]' \ --description "Block abusive caller" \ --entries '[ { "dimensions": {"$.context.jwt.sub": "blocked-user-789"}, "requests": [ { "rate": 0, "period": "second" } ] }, { "dimensions": {"$.context.jwt.sub": "*"}, "requests": [ { "rate": 100, "period": "second" } ] } ]'
AWS Python SDK (Boto3)
  1. import boto3 client = boto3.client("bedrock-agentcore-control", region_name="us-west-2") response = client.create_gateway_rate_limit( gatewayIdentifier="my-gateway-abc1234567", dimensionKeys=["$.context.jwt.sub"], description="Block abusive caller", entries=[ { "dimensions": {"$.context.jwt.sub": "blocked-user-789"}, "requests": [ { "rate": 0, "period": "second", } ], }, { "dimensions": {"$.context.jwt.sub": "*"}, "requests": [ { "rate": 100, "period": "second", } ], }, ], ) print(f"Rate Limit ID: {response['rateLimitId']}")

取得速率限制

下列範例會擷取特定速率限制的詳細資訊。

範例
AWS CLI
  1. 執行以下命令:

    aws bedrock-agentcore-control get-gateway-rate-limit \ --gateway-identifier my-gateway-abc1234567 \ --rate-limit-id rl-abc123def456
AWS Python SDK (Boto3)
  1. import boto3 client = boto3.client("bedrock-agentcore-control", region_name="us-west-2") response = client.get_gateway_rate_limit( gatewayIdentifier="my-gateway-abc1234567", rateLimitId="rl-abc123def456", ) print(f"Status: {response['status']}") print(f"Dimension Keys: {response['dimensionKeys']}") print(f"Entries: {len(response['entries'])}")

更新速率限制

下列範例會更新現有速率限制的項目,以增加特定目標的速率。

注意

dimensionKeys 欄位在建立後是不可變的。若要變更維度索引鍵,請刪除速率限制並建立新的限制。

範例
AWS CLI
  1. 執行以下命令:

    aws bedrock-agentcore-control update-gateway-rate-limit \ --gateway-identifier my-gateway-abc1234567 \ --rate-limit-id rl-abc123def456 \ --description "Updated per-target RPS limit" \ --entries '[ { "dimensions": {"targetName": "my-high-traffic-target"}, "requests": [ { "rate": 200, "period": "second" } ] }, { "dimensions": {"targetName": "*"}, "requests": [ { "rate": 20, "period": "second" } ] } ]'
AWS Python SDK (Boto3)
  1. import boto3 client = boto3.client("bedrock-agentcore-control", region_name="us-west-2") response = client.update_gateway_rate_limit( gatewayIdentifier="my-gateway-abc1234567", rateLimitId="rl-abc123def456", description="Updated per-target RPS limit", entries=[ { "dimensions": {"targetName": "my-high-traffic-target"}, "requests": [ { "rate": 200, "period": "second", } ], }, { "dimensions": {"targetName": "*"}, "requests": [ { "rate": 20, "period": "second", } ], }, ], ) print(f"Status: {response['status']}")

列出速率限制

下列範例列出閘道的所有速率限制。

範例
AWS CLI
  1. 執行以下命令:

    aws bedrock-agentcore-control list-gateway-rate-limits \ --gateway-identifier my-gateway-abc1234567
AWS Python SDK (Boto3)
  1. import boto3 client = boto3.client("bedrock-agentcore-control", region_name="us-west-2") paginator = client.get_paginator("list_gateway_rate_limits") for page in paginator.paginate(gatewayIdentifier="my-gateway-abc1234567"): for rate_limit in page["rateLimits"]: print( f"ID: {rate_limit['rateLimitId']}, " f"Status: {rate_limit['status']}, " f"Dimensions: {rate_limit['dimensionKeys']}" )
注意

結果可能會分頁。使用回應中的 nextToken值來擷取其他頁面,或使用 Boto3 分頁器,如上所示。

刪除速率限制

下列範例會從閘道刪除速率限制。

範例
AWS CLI
  1. 執行以下命令:

    aws bedrock-agentcore-control delete-gateway-rate-limit \ --gateway-identifier my-gateway-abc1234567 \ --rate-limit-id rl-abc123def456
AWS Python SDK (Boto3)
  1. import boto3 client = boto3.client("bedrock-agentcore-control", region_name="us-west-2") response = client.delete_gateway_rate_limit( gatewayIdentifier="my-gateway-abc1234567", rateLimitId="rl-abc123def456", ) print(f"Status: {response['status']}")

批次放置速率限制

下列範例會在單一呼叫中建立或更新多個速率限制。批次放置會使用 upsert 語意 - 如果存在具有相同維度索引鍵的速率限制,則會更新;否則會建立新的速率限制。

注意

批次放置是等冪的。您可以安全地重試失敗的呼叫,而無需建立重複的速率限制。

範例
AWS CLI
  1. 執行以下命令:

    aws bedrock-agentcore-control batch-put-gateway-rate-limits \ --gateway-identifier my-gateway-abc1234567 \ --rate-limits '[ { "dimensionKeys": ["targetName"], "description": "Per-target RPS", "entries": [ { "dimensions": {"targetName": "*"}, "requests": [{"rate": 50, "period": "second"}] } ] }, { "dimensionKeys": ["$.context.jwt.sub"], "description": "Per-caller RPM", "entries": [ { "dimensions": {"$.context.jwt.sub": "*"}, "requests": [{"rate": 120, "period": "minute"}] } ] } ]'
AWS Python SDK (Boto3)
  1. import boto3 client = boto3.client("bedrock-agentcore-control", region_name="us-west-2") response = client.batch_put_gateway_rate_limits( gatewayIdentifier="my-gateway-abc1234567", rateLimits=[ { "dimensionKeys": ["targetName"], "description": "Per-target RPS", "entries": [ { "dimensions": {"targetName": "*"}, "requests": [ {"rate": 50, "period": "second"} ], } ], }, { "dimensionKeys": ["$.context.jwt.sub"], "description": "Per-caller RPM", "entries": [ { "dimensions": {"$.context.jwt.sub": "*"}, "requests": [ {"rate": 120, "period": "minute"} ], } ], }, ], ) for result in response["rateLimits"]: print(f"ID: {result['rateLimitId']}, Status: {result['status']}")