Count Tokens
| bedrockruntime_count_tokens | R Documentation |
Returns the token count for a given inference request¶
Description¶
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 invoke_model 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 invoke_model and
converse, allowing you to count tokens for both raw text inputs and
structured conversation formats.
The following operations are related to count_tokens:
-
invoke_model- Sends inference requests to foundation models -
converse- Sends conversation-based inference requests to foundation models
Usage¶
bedrockruntime_count_tokens(modelId, input)
Arguments¶
modelId |
[required] 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 |
[required] The input for which to count tokens. The structure of
this parameter depends on whether you're counting tokens for an
The input format must be compatible with the model specified in the
|
Value¶
A list with the following syntax:
list(
inputTokens = 123
)
Request syntax¶
svc$count_tokens(
modelId = "string",
input = list(
invokeModel = list(
body = raw
),
converse = list(
messages = list(
list(
role = "user"|"assistant",
content = list(
list(
text = "string",
image = list(
format = "png"|"jpeg"|"gif"|"webp",
source = list(
bytes = raw,
s3Location = list(
uri = "string",
bucketOwner = "string"
)
),
error = list(
message = "string"
)
),
document = list(
format = "pdf"|"csv"|"doc"|"docx"|"xls"|"xlsx"|"html"|"txt"|"md",
name = "string",
source = list(
bytes = raw,
s3Location = list(
uri = "string",
bucketOwner = "string"
),
text = "string",
content = list(
list(
text = "string"
)
)
),
context = "string",
citations = list(
enabled = TRUE|FALSE
)
),
video = list(
format = "mkv"|"mov"|"mp4"|"webm"|"flv"|"mpeg"|"mpg"|"wmv"|"three_gp",
source = list(
bytes = raw,
s3Location = list(
uri = "string",
bucketOwner = "string"
)
)
),
audio = list(
format = "mp3"|"opus"|"wav"|"aac"|"flac"|"mp4"|"ogg"|"mkv"|"mka"|"x-aac"|"m4a"|"mpeg"|"mpga"|"pcm"|"webm",
source = list(
bytes = raw,
s3Location = list(
uri = "string",
bucketOwner = "string"
)
),
error = list(
message = "string"
)
),
toolUse = list(
toolUseId = "string",
name = "string",
input = list(),
type = "server_tool_use"
),
toolResult = list(
toolUseId = "string",
content = list(
list(
json = list(),
text = "string",
image = list(
format = "png"|"jpeg"|"gif"|"webp",
source = list(
bytes = raw,
s3Location = list(
uri = "string",
bucketOwner = "string"
)
),
error = list(
message = "string"
)
),
document = list(
format = "pdf"|"csv"|"doc"|"docx"|"xls"|"xlsx"|"html"|"txt"|"md",
name = "string",
source = list(
bytes = raw,
s3Location = list(
uri = "string",
bucketOwner = "string"
),
text = "string",
content = list(
list(
text = "string"
)
)
),
context = "string",
citations = list(
enabled = TRUE|FALSE
)
),
video = list(
format = "mkv"|"mov"|"mp4"|"webm"|"flv"|"mpeg"|"mpg"|"wmv"|"three_gp",
source = list(
bytes = raw,
s3Location = list(
uri = "string",
bucketOwner = "string"
)
)
),
searchResult = list(
source = "string",
title = "string",
content = list(
list(
text = "string"
)
),
citations = list(
enabled = TRUE|FALSE
)
)
)
),
status = "success"|"error",
type = "string"
),
guardContent = list(
text = list(
text = "string",
qualifiers = list(
"grounding_source"|"query"|"guard_content"
)
),
image = list(
format = "png"|"jpeg",
source = list(
bytes = raw
)
)
),
cachePoint = list(
type = "default",
ttl = "5m"|"1h"
),
reasoningContent = list(
reasoningText = list(
text = "string",
signature = "string"
),
redactedContent = raw
),
citationsContent = list(
content = list(
list(
text = "string"
)
),
citations = list(
list(
title = "string",
source = "string",
sourceContent = list(
list(
text = "string"
)
),
location = list(
web = list(
url = "string",
domain = "string"
),
documentChar = list(
documentIndex = 123,
start = 123,
end = 123
),
documentPage = list(
documentIndex = 123,
start = 123,
end = 123
),
documentChunk = list(
documentIndex = 123,
start = 123,
end = 123
),
searchResultLocation = list(
searchResultIndex = 123,
start = 123,
end = 123
)
)
)
)
),
searchResult = list(
source = "string",
title = "string",
content = list(
list(
text = "string"
)
),
citations = list(
enabled = TRUE|FALSE
)
)
)
)
)
),
system = list(
list(
text = "string",
guardContent = list(
text = list(
text = "string",
qualifiers = list(
"grounding_source"|"query"|"guard_content"
)
),
image = list(
format = "png"|"jpeg",
source = list(
bytes = raw
)
)
),
cachePoint = list(
type = "default",
ttl = "5m"|"1h"
)
)
),
toolConfig = list(
tools = list(
list(
toolSpec = list(
name = "string",
description = "string",
inputSchema = list(
json = list()
),
strict = TRUE|FALSE
),
systemTool = list(
name = "string"
),
cachePoint = list(
type = "default",
ttl = "5m"|"1h"
)
)
),
toolChoice = list(
auto = list(),
any = list(),
tool = list(
name = "string"
)
)
),
additionalModelRequestFields = list()
)
)
)