How do I check the number of tokens when I invoke a model in Amazon Bedrock?

3 minutos de lectura
0

I want to check the number of input and output tokens when I invoke a model in Amazon Bedrock.

Resolution

Note: If you receive errors when you run AWS Command Line Interface (AWS CLI) commands, then see Troubleshooting errors for the AWS CLI. Also, make sure that you're using the most recent AWS CLI version.

To check the token count, use one of the following methods. 

Use an Amazon Bedrock playground

To use a playground to check the input and output tokens when you invoke the model, see Generate responses in the console using playgrounds.

Check the model invocation logs

Prerequisite: You must create a destination to deliver your logs to. You can use Amazon Simple Storage Service (Amazon S3) to set up an S3 bucket, or use Amazon CloudWatch Logs to set up a log group. If you use an S3 bucket then, add a bucket policy to the bucket that includes the policy statement from step 2 in Set up an Amazon S3 destination. If you use a log group, then create an AWS Identity and Access Management (IAM) role that includes the trust relationship and policy statement from step 2 in Set up a CloudWatch Logs destination.

To check the logs, complete the following steps:

  1. Use the CloudWatch console or API to activate logging for all model invocations.
  2. In the logs, view the number of inputTokens and outputTokens for each model invocation.

Example log:

{
    "schemaType": "ModelInvocationLog",
    "schemaVersion": "1.0",
    "timestamp": "2025-03-31T21:34:03Z",
    "accountId": "123456789",
    "identity": {
        "arn": "arn:aws:sts::123456789:role/MyRole"
    },
    "region": "us-east-1",
    "requestId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx",
    "operation": "ConverseStream",
    "modelId": "amazon.nova-pro-v1:0",
    "input": {
        "inputContentType": "application/json",
        "inputBodyJson": {
            "messages": [
                {
                    "role": "user",
                    "content": [
                        {
                            "text": "Good morning."
                        }
                    ]
                }
            ],
            "inferenceConfig": {
                "maxTokens": 512,
                "temperature": 0.7,
                "topP": 0.9,
                "stopSequences": []
            },
            "additionalModelRequestFields": {}
        },
        "inputTokenCount": 3,
        "cacheReadInputTokenCount": 0,
        "cacheWriteInputTokenCount": 0
    },
    "output": {
        "outputContentType": "application/json",
        "outputBodyJson": {
            "output": {
                "message": {
                    "role": "assistant",
                    "content": [
                        {
                            "text": "Good morning! How can I assist you today? Whether you have questions, need information, or just want to chat, feel free to ask."
                        }
                    ]
                }
            },
            "stopReason": "end_turn",
            "metrics": {
                "latencyMs": 454
            },
            "usage": {
                "inputTokens": 3,
                "outputTokens": 29,
                "totalTokens": 32
            }
        },
        "outputTokenCount": 29
    }
}

Check the response from the Converse operation

Use the AWS CLI to get a response from the Converse API operation.

Run the following bedrock-runtime command with the converse command:

aws bedrock-runtime converse \
    --region your-region\
    --model-id amazon.nova-pro-v1:0 \
    --messages '{"role": "user", "content": [{"text": "Hello"}]}'

Note: The preceding command gets the token count for the amazon.nova-pro-v1:0 model. Replace your-region with your AWS Region, model-id with your model ID and text with your own message.

Example response:

{
    "output": {
        "message": {
            "role": "assistant",
            "content": [
                {
                    "text": "Hello! It's nice to have you here. I'm here to help with whatever you might need. Whether you have a question, need assistance with a topic, or just want to chat, feel free to ask. What can I assist you with today?"
                }
            ]
        }
    },
    "stopReason": "end_turn",
    "usage": {
        "inputTokens": 1,
        "outputTokens": 54,
        "totalTokens": 55
    },
    "metrics": {
        "latencyMs": 852
    }
}
OFICIAL DE AWS
OFICIAL DE AWSActualizada hace 7 días