@@ -11,6 +11,17 @@ defmodule AWS.BedrockRuntime do
1111
1212 @ typedoc """
1313
14+ ## Example:
15+
16+ count_tokens_response() :: %{
17+ "inputTokens" => [integer()]
18+ }
19+
20+ """
21+ @ type count_tokens_response ( ) :: % { ( String . t ( ) | atom ( ) ) => any ( ) }
22+
23+ @ typedoc """
24+
1425 ## Example:
1526
1627 guardrail_automated_reasoning_rule() :: %{
@@ -176,6 +187,18 @@ defmodule AWS.BedrockRuntime do
176187
177188 @ typedoc """
178189
190+ ## Example:
191+
192+ converse_tokens_request() :: %{
193+ "messages" => list(message()),
194+ "system" => list(list())
195+ }
196+
197+ """
198+ @ type converse_tokens_request ( ) :: % { ( String . t ( ) | atom ( ) ) => any ( ) }
199+
200+ @ typedoc """
201+
179202 ## Example:
180203
181204 any_tool_choice() :: %{}
@@ -283,6 +306,17 @@ defmodule AWS.BedrockRuntime do
283306
284307 @ typedoc """
285308
309+ ## Example:
310+
311+ count_tokens_request() :: %{
312+ required("input") => list()
313+ }
314+
315+ """
316+ @ type count_tokens_request ( ) :: % { ( String . t ( ) | atom ( ) ) => any ( ) }
317+
318+ @ typedoc """
319+
286320 ## Example:
287321
288322 citations_content_block() :: %{
@@ -1329,6 +1363,17 @@ defmodule AWS.BedrockRuntime do
13291363
13301364 @ typedoc """
13311365
1366+ ## Example:
1367+
1368+ invoke_model_tokens_request() :: %{
1369+ "body" => binary()
1370+ }
1371+
1372+ """
1373+ @ type invoke_model_tokens_request ( ) :: % { ( String . t ( ) | atom ( ) ) => any ( ) }
1374+
1375+ @ typedoc """
1376+
13321377 ## Example:
13331378
13341379 guardrail_pii_entity_filter() :: %{
@@ -1495,6 +1540,14 @@ defmodule AWS.BedrockRuntime do
14951540 | resource_not_found_exception ( )
14961541 | model_not_ready_exception ( )
14971542
1543+ @ type count_tokens_errors ( ) ::
1544+ throttling_exception ( )
1545+ | validation_exception ( )
1546+ | access_denied_exception ( )
1547+ | internal_server_exception ( )
1548+ | service_unavailable_exception ( )
1549+ | resource_not_found_exception ( )
1550+
14981551 @ type get_async_invoke_errors ( ) ::
14991552 throttling_exception ( )
15001553 | validation_exception ( )
@@ -1771,6 +1824,65 @@ defmodule AWS.BedrockRuntime do
17711824 )
17721825 end
17731826
1827+ @ doc """
1828+ Returns the token count for a given inference request.
1829+
1830+ This operation helps you estimate token usage before sending requests to
1831+ foundation models by returning the token count that would be used if the same
1832+ input were sent to the model in an inference request.
1833+
1834+ Token counting is model-specific because different models use different
1835+ tokenization strategies. The token count returned by this operation will match
1836+ the token count that would be charged if the same input were sent to the model
1837+ in an `InvokeModel` or `Converse` request.
1838+
1839+ You can use this operation to:
1840+
1841+ * Estimate costs before sending inference requests.
1842+
1843+ * Optimize prompts to fit within token limits.
1844+
1845+ * Plan for token usage in your applications.
1846+
1847+ This operation accepts the same input formats as `InvokeModel` and `Converse`,
1848+ allowing you to count tokens for both raw text inputs and structured
1849+ conversation formats.
1850+
1851+ The following operations are related to `CountTokens`:
1852+
1853+ *
1854+ [InvokeModel](https://docs.aws.amazon.com/bedrock/latest/API/API_runtime_InvokeModel.html) - Sends inference requests to foundation models
1855+
1856+ *
1857+ [Converse](https://docs.aws.amazon.com/bedrock/latest/API/API_runtime_Converse.html)
1858+ - Sends conversation-based inference requests to foundation models
1859+ """
1860+ @ spec count_tokens ( map ( ) , String . t ( ) | atom ( ) , count_tokens_request ( ) , list ( ) ) ::
1861+ { :ok , count_tokens_response ( ) , any ( ) }
1862+ | { :error , { :unexpected_response , any ( ) } }
1863+ | { :error , term ( ) }
1864+ | { :error , count_tokens_errors ( ) }
1865+ def count_tokens ( % Client { } = client , model_id , input , options \\ [ ] ) do
1866+ url_path = "/model/#{ AWS.Util . encode_uri ( model_id ) } /count-tokens"
1867+ headers = [ ]
1868+ custom_headers = [ ]
1869+ query_params = [ ]
1870+
1871+ meta = metadata ( )
1872+
1873+ Request . request_rest (
1874+ client ,
1875+ meta ,
1876+ :post ,
1877+ url_path ,
1878+ query_params ,
1879+ custom_headers ++ headers ,
1880+ input ,
1881+ options ,
1882+ 200
1883+ )
1884+ end
1885+
17741886 @ doc """
17751887 Retrieve information about an asynchronous invocation.
17761888 """
0 commit comments