countTokens method
- required CountTokensInput input,
- required String modelId,
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.
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
May throw AccessDeniedException.
May throw InternalServerException.
May throw ResourceNotFoundException.
May throw ServiceUnavailableException.
May throw ThrottlingException.
May throw ValidationException.
Parameter input :
The input for which to count tokens. The structure of this parameter
depends on whether you're counting tokens for an InvokeModel
or Converse request:
-
For
InvokeModelrequests, provide the request body in theinvokeModelfield -
For
Converserequests, provide the messages and system content in theconversefield
modelId parameter.
Parameter modelId :
The unique identifier or ARN of the foundation model to use for token
counting. Each model processes tokens differently, so the token count is
specific to the model you specify.
Implementation
Future<CountTokensResponse> countTokens({
required CountTokensInput input,
required String modelId,
}) async {
final $payload = <String, dynamic>{
'input': input,
};
final response = await _protocol.send(
payload: $payload,
method: 'POST',
requestUri: '/model/${Uri.encodeComponent(modelId)}/count-tokens',
exceptionFnMap: _exceptionFns,
);
return CountTokensResponse.fromJson(response);
}