AWS services or capabilities described in AWS Documentation may vary by region/location. Click Getting Started with Amazon AWS to see specific differences applicable to the China (Beijing) Region.
Returns the token count for a given inference request. This operation helps you estimate token usage before sending requests to foundation models by returning the token count that would be used if the same input were sent to the model in an inference request.
Token counting is model-specific because different models use different tokenization
strategies. The token count returned by this operation will match the token count
that would be charged if the same input were sent to the model in an InvokeModel
or Converse
request.
You can use this operation to:
Estimate costs before sending inference requests.
Optimize prompts to fit within token limits.
Plan for token usage in your applications.
This operation accepts the same input formats as InvokeModel
and Converse
,
allowing you to count tokens for both raw text inputs and structured conversation
formats.
The following operations are related to CountTokens
:
InvokeModel - Sends inference requests to foundation models
Converse - Sends conversation-based inference requests to foundation models
For .NET Core this operation is only available in asynchronous form. Please refer to CountTokensAsync.
Namespace: Amazon.BedrockRuntime
Assembly: AWSSDK.BedrockRuntime.dll
Version: 3.x.y.z
public virtual CountTokensResponse CountTokens( CountTokensRequest request )
Container for the necessary parameters to execute the CountTokens service method.
Exception | Condition |
---|---|
AccessDeniedException | The request is denied because you do not have sufficient permissions to perform the requested action. For troubleshooting this error, see AccessDeniedException in the Amazon Bedrock User Guide |
InternalServerException | An internal server error occurred. For troubleshooting this error, see InternalFailure in the Amazon Bedrock User Guide |
ResourceNotFoundException | The specified resource ARN was not found. For troubleshooting this error, see ResourceNotFound in the Amazon Bedrock User Guide |
ServiceUnavailableException | The service isn't currently available. For troubleshooting this error, see ServiceUnavailable in the Amazon Bedrock User Guide |
ThrottlingException | Your request was denied due to exceeding the account quotas for Amazon Bedrock. For troubleshooting this error, see ThrottlingException in the Amazon Bedrock User Guide |
ValidationException | The input fails to satisfy the constraints specified by Amazon Bedrock. For troubleshooting this error, see ValidationError in the Amazon Bedrock User Guide |
.NET Framework:
Supported in: 4.7.2 and newer