BedrockRuntimeClient.count_tokens
rboto-bedrock-runtime==0.4.0 · Service index · Types
Model summary (excerpt): 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...
Install and imports
from rboto import BedrockRuntimeClient
from rboto_bedrock_runtime.types import (
CountTokensInput,
CountTokensOutput,
)
Signature
Arguments without defaults are required. None omits an optional request argument. Static types do not validate resource existence, permissions, lengths, ranges, or conditional service requirements.
Arguments
| Argument | Required | Model notes (excerpt) |
|---|---|---|
model_id |
Yes | 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. |
input |
Yes | 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 InvokeModel requests, provide the request body in the invokeModel field For Converse requests, provide the messages and system content in the converse field The input format must be compatible with the model specified in the modelId parameter. |
Output
CountTokensOutput.
Read-only native attributes, accessed as response.field. Collection getters materialize Python values; to_dict() recursively materializes a mapping. Streams are consumed once.
| Field | Type | Presence | References |
|---|---|---|---|
input_tokens |
int |
Always present | — |
| None means a value can be absent. Output mappings include every declared key; optional input keys may be omitted. Unknown output enum/union variants are not automatically valid inputs.
Related types
CountTokensInput, CountTokensOutput
Follow these links for nested inputs and outputs; recursive types link back to the same type page.
Pagination and streaming
No generated paginator for this operation.
No output stream.
Errors
Import from rboto_bedrock_runtime.exceptions:
AccessDeniedError, InternalServerError, ResourceNotFoundError, ServiceUnavailableError, ThrottlingError, ValidationError.
Other service failures use the service base error; credential, transport and timeout failures use shared errors.