내용으로 건너뛰기

Amazon Bedrock에서 모델을 호출할 때 토큰 수를 확인하려면 어떻게 해야 합니까?

3분 분량
0

Amazon Bedrock에서 모델을 호출할 때 입력 및 출력 토큰의 수를 확인하고 싶습니다.

해결 방법

참고: AWS Command Line Interface(AWS CLI) 명령을 실행할 때 오류가 발생하면 AWS CLI의 오류 해결을 참조하십시오. 또한 최신 AWS CLI 버전을 사용하고 있는지 확인하십시오.

토큰 수를 확인하려면 다음 방법 중 하나를 사용하십시오. 

Amazon Bedrock 플레이그라운드 사용

모델을 호출할 때 플레이그라운드를 사용하여 입력 및 출력 토큰을 확인하려면 플레이그라운드를 사용하여 콘솔에서 응답 생성을 참조하십시오.

모델 호출 로그 확인

사전 요구 사항: 로그를 전송할 대상을 만들어야 합니다. Amazon Simple Storage Service(Amazon S3)를 사용하여 S3 버킷을 설정하거나 Amazon CloudWatch Logs를 사용하여 로그 그룹을 설정할 수 있습니다. S3 버킷을 사용하는 경우 Amazon S3 대상 설정의 2단계에서 정책 설명이 포함된 버킷 정책을 버킷에 추가하십시오. 로그 그룹을 사용하는 경우 CloudWatch Logs 대상 설정의 2단계에서 신뢰 관계 및 정책 설명을 포함하는 AWS Identity and Access Management(IAM) 역할을 만드십시오.

로그를 확인하려면 다음 단계를 완료하십시오.

  1. CloudWatch 콘솔 또는 API를 사용하여 모든 모델 호출에 대한 로깅을 활성화합니다.
  2. 로그에서 각 모델 호출의 inputTokensoutputTokens 수를 확인합니다.

로그 예시:

{
    "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
    }
}

Converse 작업의 응답 확인

AWS CLI를 사용하여 Converse API 작업에서 응답을 받을 수 있습니다.

converse 명령과 다음 bedrock-runtime 명령을 함께 실행합니다.

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

참고: 위 명령은 amazon.nova-pro-v1:0 모델의 토큰 수를 가져옵니다. your-region을 AWS 리전으로, model-id를 모델 ID로, text를 사용자 고유의 메시지로 바꾸십시오.

응답 예시:

{
    "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
    }
}