When a generative AI request takes longer than expected, a single timer cannot show whether the model or your application spent the time. Amazon Bedrock’s Converse API returns token usage and a service-reported latency. Record those alongside a client-side timer to compare requests before changing the prompt or the model.
Choose an inference target
Use Python in a virtual environment with a current Boto3 SDK. Configure your AWS credentials using the normal SDK credential chain, such as an IAM Identity Center profile for local development or an IAM role for a workload. The caller needs bedrock:InvokeModel on the applicable inference resources. Select a model that supports Converse in your Region and complete any model-access prerequisites.
Set BEDROCK_REGION and BEDROCK_MODEL_ID in your shell. Use the exact foundation-model or inference-profile ID shown in Bedrock for your target. Models that require an inference profile need that profile’s ID or ARN. This example sends its own messages and inference settings, so use an inference target instead of a Prompt Management prompt ARN.
Log usage, stop reason, and two timings
Save the following as converse_metrics.py. Install Boto3 with python -m pip install boto3 in the virtual environment, then run python converse_metrics.py. The example sends a synthetic prompt and prints request metadata without writing the prompt or the generated text to logs.
import json
import os
from time import perf_counter
import boto3
client = boto3.client(
"bedrock-runtime",
region_name=os.environ["BEDROCK_REGION"],
)
model_id = os.environ["BEDROCK_MODEL_ID"]
message = {
"role": "user",
"content": [{
"text": "In two bullet points, explain why S3 versioning helps recovery."
}],
}
started = perf_counter()
result = client.converse(
modelId=model_id,
messages=[message],
inferenceConfig={"maxTokens": 160},
)
client_ms = round((perf_counter() - started) * 1000)
usage = result["usage"]
print(json.dumps({
"model_id": model_id,
"request_id": result["ResponseMetadata"]["RequestId"],
"input_tokens": usage["inputTokens"],
"output_tokens": usage["outputTokens"],
"stop_reason": result["stopReason"],
"bedrock_latency_ms": result["metrics"]["latencyMs"],
"client_latency_ms": client_ms,
}))
The client timer measures the full SDK call. It includes network round trips and any SDK retry delay. The Bedrock value is the service-reported latency. A gap is worth investigating, but the difference alone does not identify the source of the delay.
Read the results before adjusting the prompt
- Input tokens rise. Check whether the application sends extra conversation history, retrieved documents, or tool definitions.
- The stop reason is
max_tokens. Inspect whether the answer was cut short. Tighten the requested output or increase the generation limit if a longer response is necessary. - Client latency rises more than Bedrock latency. Check transport errors, throttling, and SDK retries before concluding the model is slower.
Verify that the script emits a request ID, token counts, and both timings. Compare a small set of the same synthetic prompts across candidate configurations. Keep the target, prompt, and generation settings fixed when comparing latency across runs. Review output quality in a separate test, since metrics alone cannot tell whether a response is useful.
Handle errors and keep the test small
For access errors, check the principal’s inference permissions, any account or organization policy, and model access in the chosen Region. For validation errors, check that the target supports Converse, that its invocation mode matches the ID you supplied, and that the inference settings are supported. For throttling, use bounded SDK retries and measure the failure rate alongside latency.
Use a small input and limited output for experiments. maxTokens limits generation, so it does not limit input tokens or set a monetary budget. This example does not provision persistent resources. Individual inference calls can still incur charges based on the model and service tier. Consult Bedrock pricing before scaling the test.
See the Converse request and response reference, the Converse user guide, and model access requirements.






Leave a Reply