Skip to content

Commit e5ecaa6

Browse files
author
AWS
committed
Amazon Bedrock Runtime Update: Launch CountTokens API to allow token counting
1 parent 2ca38c1 commit e5ecaa6

File tree

2 files changed

+133
-7
lines changed

2 files changed

+133
-7
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"type": "feature",
3+
"category": "Amazon Bedrock Runtime",
4+
"contributor": "",
5+
"description": "Launch CountTokens API to allow token counting"
6+
}

services/bedrockruntime/src/main/resources/codegen-resources/service-2.json

Lines changed: 127 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,25 @@
8080
],
8181
"documentation":"<p>Sends messages to the specified Amazon Bedrock model and returns the response in a stream. <code>ConverseStream</code> provides a consistent API that works with all Amazon Bedrock models that support messages. This allows you to write code once and use it with different models. Should a model have unique inference parameters, you can also pass those unique parameters to the model. </p> <p>To find out if a model supports streaming, call <a href=\"https://docs.aws.amazon.com/bedrock/latest/APIReference/API_GetFoundationModel.html\">GetFoundationModel</a> and check the <code>responseStreamingSupported</code> field in the response.</p> <note> <p>The CLI doesn't support streaming operations in Amazon Bedrock, including <code>ConverseStream</code>.</p> </note> <p>Amazon Bedrock doesn't store any text, images, or documents that you provide as content. The data is only used to generate the response.</p> <p>You can submit a prompt by including it in the <code>messages</code> field, specifying the <code>modelId</code> of a foundation model or inference profile to run inference on it, and including any other fields that are relevant to your use case.</p> <p>You can also submit a prompt from Prompt management by specifying the ARN of the prompt version and including a map of variables to values in the <code>promptVariables</code> field. You can append more messages to the prompt by using the <code>messages</code> field. If you use a prompt from Prompt management, you can't include the following fields in the request: <code>additionalModelRequestFields</code>, <code>inferenceConfig</code>, <code>system</code>, or <code>toolConfig</code>. Instead, these fields must be defined through Prompt management. For more information, see <a href=\"https://docs.aws.amazon.com/bedrock/latest/userguide/prompt-management-use.html\">Use a prompt from Prompt management</a>.</p> <p>For information about the Converse API, see <i>Use the Converse API</i> in the <i>Amazon Bedrock User Guide</i>. To use a guardrail, see <i>Use a guardrail with the Converse API</i> in the <i>Amazon Bedrock User Guide</i>. To use a tool with a model, see <i>Tool use (Function calling)</i> in the <i>Amazon Bedrock User Guide</i> </p> <p>For example code, see <i>Conversation streaming example</i> in the <i>Amazon Bedrock User Guide</i>. </p> <p>This operation requires permission for the <code>bedrock:InvokeModelWithResponseStream</code> action.</p> <important> <p>To deny all inference access to resources that you specify in the modelId field, you need to deny access to the <code>bedrock:InvokeModel</code> and <code>bedrock:InvokeModelWithResponseStream</code> actions. Doing this also denies access to the resource through the base inference actions (<a href=\"https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModel.html\">InvokeModel</a> and <a href=\"https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModelWithResponseStream.html\">InvokeModelWithResponseStream</a>). For more information see <a href=\"https://docs.aws.amazon.com/bedrock/latest/userguide/security_iam_id-based-policy-examples.html#security_iam_id-based-policy-examples-deny-inference\">Deny access for inference on specific models</a>. </p> </important> <p>For troubleshooting some of the common errors you might encounter when using the <code>ConverseStream</code> API, see <a href=\"https://docs.aws.amazon.com/bedrock/latest/userguide/troubleshooting-api-error-codes.html\">Troubleshooting Amazon Bedrock API Error Codes</a> in the Amazon Bedrock User Guide</p>"
8282
},
83+
"CountTokens":{
84+
"name":"CountTokens",
85+
"http":{
86+
"method":"POST",
87+
"requestUri":"/model/{modelId}/count-tokens",
88+
"responseCode":200
89+
},
90+
"input":{"shape":"CountTokensRequest"},
91+
"output":{"shape":"CountTokensResponse"},
92+
"errors":[
93+
{"shape":"AccessDeniedException"},
94+
{"shape":"ResourceNotFoundException"},
95+
{"shape":"ThrottlingException"},
96+
{"shape":"InternalServerException"},
97+
{"shape":"ServiceUnavailableException"},
98+
{"shape":"ValidationException"}
99+
],
100+
"documentation":"<p>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.</p> <p>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 <code>InvokeModel</code> or <code>Converse</code> request.</p> <p>You can use this operation to:</p> <ul> <li> <p>Estimate costs before sending inference requests.</p> </li> <li> <p>Optimize prompts to fit within token limits.</p> </li> <li> <p>Plan for token usage in your applications.</p> </li> </ul> <p>This operation accepts the same input formats as <code>InvokeModel</code> and <code>Converse</code>, allowing you to count tokens for both raw text inputs and structured conversation formats.</p> <p>The following operations are related to <code>CountTokens</code>:</p> <ul> <li> <p> <a href=\"https://docs.aws.amazon.com/bedrock/latest/API/API_runtime_InvokeModel.html\">InvokeModel</a> - Sends inference requests to foundation models</p> </li> <li> <p> <a href=\"https://docs.aws.amazon.com/bedrock/latest/API/API_runtime_Converse.html\">Converse</a> - Sends conversation-based inference requests to foundation models</p> </li> </ul>"
101+
},
83102
"GetAsyncInvoke":{
84103
"name":"GetAsyncInvoke",
85104
"http":{
@@ -1065,6 +1084,20 @@
10651084
},
10661085
"documentation":"<p>The trace object in a response from <a href=\"https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ConverseStream.html\">ConverseStream</a>. Currently, you can only trace guardrails.</p>"
10671086
},
1087+
"ConverseTokensRequest":{
1088+
"type":"structure",
1089+
"members":{
1090+
"messages":{
1091+
"shape":"Messages",
1092+
"documentation":"<p>An array of messages to count tokens for.</p>"
1093+
},
1094+
"system":{
1095+
"shape":"SystemContentBlocks",
1096+
"documentation":"<p>The system content blocks to count tokens for. System content provides instructions or context to the model about how it should behave or respond. The token count will include any system content provided.</p>"
1097+
}
1098+
},
1099+
"documentation":"<p>The inputs from a <code>Converse</code> API request for token counting.</p> <p>This structure mirrors the input format for the <code>Converse</code> operation, allowing you to count tokens for conversation-based inference requests.</p>"
1100+
},
10681101
"ConverseTrace":{
10691102
"type":"structure",
10701103
"members":{
@@ -1079,6 +1112,50 @@
10791112
},
10801113
"documentation":"<p>The trace object in a response from <a href=\"https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Converse.html\">Converse</a>. Currently, you can only trace guardrails.</p>"
10811114
},
1115+
"CountTokensInput":{
1116+
"type":"structure",
1117+
"members":{
1118+
"invokeModel":{
1119+
"shape":"InvokeModelTokensRequest",
1120+
"documentation":"<p>An <code>InvokeModel</code> request for which to count tokens. Use this field when you want to count tokens for a raw text input that would be sent to the <code>InvokeModel</code> operation.</p>"
1121+
},
1122+
"converse":{
1123+
"shape":"ConverseTokensRequest",
1124+
"documentation":"<p>A <code>Converse</code> request for which to count tokens. Use this field when you want to count tokens for a conversation-based input that would be sent to the <code>Converse</code> operation.</p>"
1125+
}
1126+
},
1127+
"documentation":"<p>The input value for token counting. The value should be either an <code>InvokeModel</code> or <code>Converse</code> request body. </p>",
1128+
"union":true
1129+
},
1130+
"CountTokensRequest":{
1131+
"type":"structure",
1132+
"required":[
1133+
"modelId",
1134+
"input"
1135+
],
1136+
"members":{
1137+
"modelId":{
1138+
"shape":"FoundationModelVersionIdentifier",
1139+
"documentation":"<p>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.</p>",
1140+
"location":"uri",
1141+
"locationName":"modelId"
1142+
},
1143+
"input":{
1144+
"shape":"CountTokensInput",
1145+
"documentation":"<p>The input for which to count tokens. The structure of this parameter depends on whether you're counting tokens for an <code>InvokeModel</code> or <code>Converse</code> request:</p> <ul> <li> <p>For <code>InvokeModel</code> requests, provide the request body in the <code>invokeModel</code> field</p> </li> <li> <p>For <code>Converse</code> requests, provide the messages and system content in the <code>converse</code> field</p> </li> </ul> <p>The input format must be compatible with the model specified in the <code>modelId</code> parameter.</p>"
1146+
}
1147+
}
1148+
},
1149+
"CountTokensResponse":{
1150+
"type":"structure",
1151+
"required":["inputTokens"],
1152+
"members":{
1153+
"inputTokens":{
1154+
"shape":"Integer",
1155+
"documentation":"<p>The number of tokens in the provided input according to the specified model's tokenization rules. This count represents the number of input tokens that would be processed if the same input were sent to the model in an inference request. Use this value to estimate costs and ensure your inputs stay within model token limits.</p>"
1156+
}
1157+
}
1158+
},
10821159
"Document":{
10831160
"type":"structure",
10841161
"members":{
@@ -1275,6 +1352,13 @@
12751352
"type":"blob",
12761353
"min":1
12771354
},
1355+
"FoundationModelVersionIdentifier":{
1356+
"type":"string",
1357+
"documentation":"<p>ARN or ID of a Bedrock model</p>",
1358+
"max":256,
1359+
"min":1,
1360+
"pattern":"[a-zA-Z_\\.\\-/0-9:]+"
1361+
},
12781362
"GetAsyncInvokeRequest":{
12791363
"type":"structure",
12801364
"required":["invocationArn"],
@@ -1399,13 +1483,34 @@
13991483
"GuardrailAutomatedReasoningFinding":{
14001484
"type":"structure",
14011485
"members":{
1402-
"valid":{"shape":"GuardrailAutomatedReasoningValidFinding"},
1403-
"invalid":{"shape":"GuardrailAutomatedReasoningInvalidFinding"},
1404-
"satisfiable":{"shape":"GuardrailAutomatedReasoningSatisfiableFinding"},
1405-
"impossible":{"shape":"GuardrailAutomatedReasoningImpossibleFinding"},
1406-
"translationAmbiguous":{"shape":"GuardrailAutomatedReasoningTranslationAmbiguousFinding"},
1407-
"tooComplex":{"shape":"GuardrailAutomatedReasoningTooComplexFinding"},
1408-
"noTranslations":{"shape":"GuardrailAutomatedReasoningNoTranslationsFinding"}
1486+
"valid":{
1487+
"shape":"GuardrailAutomatedReasoningValidFinding",
1488+
"documentation":"<p>Contains the result when the automated reasoning evaluation determines that the claims in the input are logically valid and definitively true based on the provided premises and policy rules.</p>"
1489+
},
1490+
"invalid":{
1491+
"shape":"GuardrailAutomatedReasoningInvalidFinding",
1492+
"documentation":"<p>Contains the result when the automated reasoning evaluation determines that the claims in the input are logically invalid and contradict the established premises or policy rules.</p>"
1493+
},
1494+
"satisfiable":{
1495+
"shape":"GuardrailAutomatedReasoningSatisfiableFinding",
1496+
"documentation":"<p>Contains the result when the automated reasoning evaluation determines that the claims in the input could be either true or false depending on additional assumptions not provided in the input context.</p>"
1497+
},
1498+
"impossible":{
1499+
"shape":"GuardrailAutomatedReasoningImpossibleFinding",
1500+
"documentation":"<p>Contains the result when the automated reasoning evaluation determines that no valid logical conclusions can be drawn due to contradictions in the premises or policy rules themselves.</p>"
1501+
},
1502+
"translationAmbiguous":{
1503+
"shape":"GuardrailAutomatedReasoningTranslationAmbiguousFinding",
1504+
"documentation":"<p>Contains the result when the automated reasoning evaluation detects that the input has multiple valid logical interpretations, requiring additional context or clarification to proceed with validation.</p>"
1505+
},
1506+
"tooComplex":{
1507+
"shape":"GuardrailAutomatedReasoningTooComplexFinding",
1508+
"documentation":"<p>Contains the result when the automated reasoning evaluation cannot process the input due to its complexity or volume exceeding the system's processing capacity for logical analysis.</p>"
1509+
},
1510+
"noTranslations":{
1511+
"shape":"GuardrailAutomatedReasoningNoTranslationsFinding",
1512+
"documentation":"<p>Contains the result when the automated reasoning evaluation cannot extract any relevant logical information from the input that can be validated against the policy rules.</p>"
1513+
}
14091514
},
14101515
"documentation":"<p>Represents a logical validation result from automated reasoning policy evaluation. The finding indicates whether claims in the input are logically valid, invalid, satisfiable, impossible, or have other logical issues.</p>",
14111516
"union":true
@@ -2647,6 +2752,10 @@
26472752
"max":1,
26482753
"min":0
26492754
},
2755+
"Integer":{
2756+
"type":"integer",
2757+
"box":true
2758+
},
26502759
"InternalServerException":{
26512760
"type":"structure",
26522761
"members":{
@@ -2748,6 +2857,17 @@
27482857
},
27492858
"payload":"body"
27502859
},
2860+
"InvokeModelTokensRequest":{
2861+
"type":"structure",
2862+
"required":["body"],
2863+
"members":{
2864+
"body":{
2865+
"shape":"Body",
2866+
"documentation":"<p>The request body to count tokens for, formatted according to the model's expected input format. To learn about the input format for different models, see <a href=\"https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters.html\">Model inference parameters and responses</a>.</p>"
2867+
}
2868+
},
2869+
"documentation":"<p>The body of an <code>InvokeModel</code> API request for token counting. This structure mirrors the input format for the <code>InvokeModel</code> operation, allowing you to count tokens for raw text inference requests.</p>"
2870+
},
27512871
"InvokeModelWithBidirectionalStreamInput":{
27522872
"type":"structure",
27532873
"members":{

0 commit comments

Comments
 (0)