Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.
Esempi di API con limiti di velocità
Gli esempi seguenti mostrano come gestire i limiti di velocità utilizzando la AWS CLI e l'SDK AWS Python (Boto3).
Crea un limite di richieste al secondo per destinazione
L'esempio seguente crea un limite di frequenza che controlla le richieste al secondo in base al target. Un target specifico riceve 100 RPS e tutti gli altri target ricevono ciascuno il proprio bucket da 10 RPS.
Esempio
- AWS CLI
-
-
Esegui il comando seguente:
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)
-
-
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']}")
Crea un limite di richieste al minuto per chiamante
L'esempio seguente crea un limite di velocità basato sull'attestazione JWT del chiamante. sub Gli utenti Premium ottengono 300 RPM e tutti gli altri utenti ottengono 60 RPM.
Esempio
- AWS CLI
-
-
Esegui il comando seguente:
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)
-
-
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']}")
Crea un limite multidimensionale con i token
L'esempio seguente crea un limite di velocità con chiavi tridimensionali che limita sia le richieste che i token al minuto. Ogni chiamante assegna il proprio budget a un obiettivo e un modello specifici.
Esempio
- AWS CLI
-
-
Esegui il comando seguente:
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)
-
-
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']}")
Crea un limite di velocità di connessione
L'esempio seguente crea un limite di velocità di connessione per modello. Questo limita il numero di richieste simultanee in corso a ciascun modello, il che è utile per proteggere le destinazioni con una capacità di concorrenza limitata.
Esempio
- AWS CLI
-
-
Esegui il comando seguente:
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)
-
-
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']}")
Blocca un chiamante con tariffa zero
L'esempio seguente blocca un chiamante specifico impostando la sua frequenza su 0. Ciò nega tutte le richieste del chiamante bloccato una volta che la modifica si è propagata (fino a 30 secondi).
Esempio
- AWS CLI
-
-
Esegui il comando seguente:
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)
-
-
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']}")
Stabilisci un limite di velocità
L'esempio seguente recupera i dettagli di un limite di tariffa specifico.
Esempio
- AWS CLI
-
-
Esegui il comando seguente:
aws bedrock-agentcore-control get-gateway-rate-limit \
--gateway-identifier my-gateway-abc1234567 \
--rate-limit-id rl-abc123def456
- AWS Python SDK (Boto3)
-
-
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'])}")
Aggiornare un limite di velocità
L'esempio seguente aggiorna le voci di un limite di tariffa esistente per aumentare il tasso per un obiettivo specifico.
Il dimensionKeys campo è immutabile dopo la creazione. Per modificare le chiavi di dimensione, elimina il limite di velocità e creane uno nuovo.
Esempio
- AWS CLI
-
-
Esegui il comando seguente:
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)
-
-
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']}")
Elenca i limiti di velocità
L'esempio seguente elenca tutti i limiti di velocità per un gateway.
Esempio
- AWS CLI
-
-
Esegui il comando seguente:
aws bedrock-agentcore-control list-gateway-rate-limits \
--gateway-identifier my-gateway-abc1234567
- AWS Python SDK (Boto3)
-
-
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']}"
)
I risultati potrebbero essere suddivisi in pagine. Usa il nextToken valore della risposta per recuperare pagine aggiuntive o usa l'impaginatore Boto3 come mostrato sopra.
Eliminare un limite di velocità
L'esempio seguente elimina un limite di velocità da un gateway.
Esempio
- AWS CLI
-
-
Esegui il comando seguente:
aws bedrock-agentcore-control delete-gateway-rate-limit \
--gateway-identifier my-gateway-abc1234567 \
--rate-limit-id rl-abc123def456
- AWS Python SDK (Boto3)
-
-
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']}")
Limiti di velocità di invio in batch
L'esempio seguente crea o aggiorna più limiti di velocità in una singola chiamata. Il batch put utilizza la semantica upsert: se esiste un limite di velocità con le stesse chiavi di dimensione, viene aggiornato; in caso contrario, viene creato un nuovo limite di velocità.
Il batch put è idempotente. Puoi riprovare con sicurezza le chiamate non riuscite senza creare limiti di frequenza duplicati.
Esempio
- AWS CLI
-
-
Esegui il comando seguente:
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)
-
-
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']}")