View a markdown version of this page

レート制限 API の例 - Amazon Bedrock AgentCore

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

レート制限 API の例

次の例は、 CLI と AWS Python SDK (Boto3) AWS を使用してレート制限を管理する方法を示しています。

1 requests-per-secondの制限を作成する

次の例では、ターゲットごとに 1 秒あたりのリクエストを制御するレート制限を作成します。特定のターゲットは 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']}")

発信者ごとの 1 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']}")

トークンを使用してマルチディメンション制限を作成する

次の例では、1 分あたりのリクエストとトークンの両方を制限する 3 つのディメンションキーを使用してレート制限を作成します。各発信者は、特定のターゲットとモデルを対象とする独自の予算を取得します。

例
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']}")

バッチ配置レート制限

次の の例では、1 回の呼び出しで複数のレート制限を作成または更新します。バッチ配置ではアップサートセマンティクスを使用します。同じディメンションキーを持つレート制限が存在する場合は更新されます。それ以外の場合は、新しいレート制限が作成されます。

注記

バッチ配置はべき等です。重複レート制限を作成せずに、失敗した呼び出しを安全に再試行できます。

例
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']}")