Skip to content

/AWS1/IF_BDR=>COUNTTOKENS()

About CountTokens

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.

This operation accepts the same input formats as 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

Method Signature

IMPORTING

Required arguments:

iv_modelid TYPE /AWS1/BDRFOUNDATIONMODELVRSID /AWS1/BDRFOUNDATIONMODELVRSID

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.

io_input TYPE REF TO /AWS1/CL_BDRCOUNTTOKENSINPUT /AWS1/CL_BDRCOUNTTOKENSINPUT

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.

RETURNING

oo_output TYPE REF TO /aws1/cl_bdrcounttokensrsp /AWS1/CL_BDRCOUNTTOKENSRSP

Domain /AWS1/RT_ACCOUNT_ID
Primitive Type NUMC

Examples

Syntax Example

This is an example of the syntax for calling the method. It includes every possible argument and initializes every possible value. The data provided is not necessarily semantically accurate (for example the value "string" may be provided for something that is intended to be an instance ID, or in some cases two arguments may be mutually exclusive). The syntax shows the ABAP syntax for creating the various data structures.

DATA(lo_result) = lo_client->/aws1/if_bdr~counttokens(
  io_input = new /aws1/cl_bdrcounttokensinput(
    io_converse00 = new /aws1/cl_bdrconversetokensreq(
      it_messages = VALUE /aws1/cl_bdrmessage=>tt_messages(
        (
          new /aws1/cl_bdrmessage(
            it_content = VALUE /aws1/cl_bdrcontentblock=>tt_contentblocks(
              (
                new /aws1/cl_bdrcontentblock(
                  io_cachepoint = new /aws1/cl_bdrcachepointblock( |string| )
                  io_citationscontent = new /aws1/cl_bdrcitationscontblock(
                    it_citations = VALUE /aws1/cl_bdrcitation=>tt_citations(
                      (
                        new /aws1/cl_bdrcitation(
                          io_location = new /aws1/cl_bdrcitationlocation(
                            io_documentchar = new /aws1/cl_bdrdoccharlocation(
                              iv_documentindex = 123
                              iv_end = 123
                              iv_start = 123
                            )
                            io_documentchunk = new /aws1/cl_bdrdocchunklocation(
                              iv_documentindex = 123
                              iv_end = 123
                              iv_start = 123
                            )
                            io_documentpage = new /aws1/cl_bdrdocpagelocation(
                              iv_documentindex = 123
                              iv_end = 123
                              iv_start = 123
                            )
                          )
                          it_sourcecontent = VALUE /aws1/cl_bdrcitationsourcecont=>tt_citationsourcecontentlist(
                            ( new /aws1/cl_bdrcitationsourcecont( |string| ) )
                          )
                          iv_title = |string|
                        )
                      )
                    )
                    it_content = VALUE /aws1/cl_bdrcitationgenr8dcont=>tt_citationgenr8dcontentlist(
                      ( new /aws1/cl_bdrcitationgenr8dcont( |string| ) )
                    )
                  )
                  io_document = new /aws1/cl_bdrdocumentblock(
                    io_citations = new /aws1/cl_bdrcitationsconfig( ABAP_TRUE )
                    io_source = new /aws1/cl_bdrdocumentsource(
                      io_s3location = new /aws1/cl_bdrs3location(
                        iv_bucketowner = |string|
                        iv_uri = |string|
                      )
                      it_content = VALUE /aws1/cl_bdrdoccontentblock=>tt_documentcontentblocks(
                        ( new /aws1/cl_bdrdoccontentblock( |string| ) )
                      )
                      iv_bytes = '5347567362473873563239796247513D'
                      iv_text = |string|
                    )
                    iv_context = |string|
                    iv_format = |string|
                    iv_name = |string|
                  )
                  io_guardcontent = new /aws1/cl_bdrguardrailconvers00(
                    io_image = new /aws1/cl_bdrguardrailconvers03(
                      io_source = new /aws1/cl_bdrguardrailconvers04( '5347567362473873563239796247513D' )
                      iv_format = |string|
                    )
                    io_text = new /aws1/cl_bdrguardrailconvers01(
                      it_qualifiers = VALUE /aws1/cl_bdrguardrailconvers02=>tt_guardrailconversecontqual00(
                        ( new /aws1/cl_bdrguardrailconvers02( |string| ) )
                      )
                      iv_text = |string|
                    )
                  )
                  io_image = new /aws1/cl_bdrimageblock(
                    io_source = new /aws1/cl_bdrimagesource(
                      io_s3location = new /aws1/cl_bdrs3location(
                        iv_bucketowner = |string|
                        iv_uri = |string|
                      )
                      iv_bytes = '5347567362473873563239796247513D'
                    )
                    iv_format = |string|
                  )
                  io_reasoningcontent = new /aws1/cl_bdrreasoningcontblock(
                    io_reasoningtext = new /aws1/cl_bdrreasoningtextblock(
                      iv_signature = |string|
                      iv_text = |string|
                    )
                    iv_redactedcontent = '5347567362473873563239796247513D'
                  )
                  io_toolresult = new /aws1/cl_bdrtoolresultblock(
                    it_content = VALUE /aws1/cl_bdrtoolrsltcontblock=>tt_toolresultcontentblocks(
                      (
                        new /aws1/cl_bdrtoolrsltcontblock(
                          io_document = new /aws1/cl_bdrdocumentblock(
                            io_citations = new /aws1/cl_bdrcitationsconfig( ABAP_TRUE )
                            io_source = new /aws1/cl_bdrdocumentsource(
                              io_s3location = new /aws1/cl_bdrs3location(
                                iv_bucketowner = |string|
                                iv_uri = |string|
                              )
                              it_content = VALUE /aws1/cl_bdrdoccontentblock=>tt_documentcontentblocks(
                                ( new /aws1/cl_bdrdoccontentblock( |string| ) )
                              )
                              iv_bytes = '5347567362473873563239796247513D'
                              iv_text = |string|
                            )
                            iv_context = |string|
                            iv_format = |string|
                            iv_name = |string|
                          )
                          io_image = new /aws1/cl_bdrimageblock(
                            io_source = new /aws1/cl_bdrimagesource(
                              io_s3location = new /aws1/cl_bdrs3location(
                                iv_bucketowner = |string|
                                iv_uri = |string|
                              )
                              iv_bytes = '5347567362473873563239796247513D'
                            )
                            iv_format = |string|
                          )
                          io_json = /AWS1/CL_RT_DOCUMENT=>FROM_JSON_STR( |\{"foo":"this is a JSON object..."\}| )
                          io_video = new /aws1/cl_bdrvideoblock(
                            io_source = new /aws1/cl_bdrvideosource(
                              io_s3location = new /aws1/cl_bdrs3location(
                                iv_bucketowner = |string|
                                iv_uri = |string|
                              )
                              iv_bytes = '5347567362473873563239796247513D'
                            )
                            iv_format = |string|
                          )
                          iv_text = |string|
                        )
                      )
                    )
                    iv_status = |string|
                    iv_tooluseid = |string|
                  )
                  io_tooluse = new /aws1/cl_bdrtooluseblock(
                    io_input = /AWS1/CL_RT_DOCUMENT=>FROM_JSON_STR( |\{"foo":"this is a JSON object..."\}| )
                    iv_name = |string|
                    iv_tooluseid = |string|
                  )
                  io_video = new /aws1/cl_bdrvideoblock(
                    io_source = new /aws1/cl_bdrvideosource(
                      io_s3location = new /aws1/cl_bdrs3location(
                        iv_bucketowner = |string|
                        iv_uri = |string|
                      )
                      iv_bytes = '5347567362473873563239796247513D'
                    )
                    iv_format = |string|
                  )
                  iv_text = |string|
                )
              )
            )
            iv_role = |string|
          )
        )
      )
      it_system = VALUE /aws1/cl_bdrsystemcontentblock=>tt_systemcontentblocks(
        (
          new /aws1/cl_bdrsystemcontentblock(
            io_cachepoint = new /aws1/cl_bdrcachepointblock( |string| )
            io_guardcontent = new /aws1/cl_bdrguardrailconvers00(
              io_image = new /aws1/cl_bdrguardrailconvers03(
                io_source = new /aws1/cl_bdrguardrailconvers04( '5347567362473873563239796247513D' )
                iv_format = |string|
              )
              io_text = new /aws1/cl_bdrguardrailconvers01(
                it_qualifiers = VALUE /aws1/cl_bdrguardrailconvers02=>tt_guardrailconversecontqual00(
                  ( new /aws1/cl_bdrguardrailconvers02( |string| ) )
                )
                iv_text = |string|
              )
            )
            iv_text = |string|
          )
        )
      )
    )
    io_invokemodel00 = new /aws1/cl_bdrinvkmodeltokensreq( '5347567362473873563239796247513D' )
  )
  iv_modelid = |string|
).

This is an example of reading all possible response values

lo_result = lo_result.
IF lo_result IS NOT INITIAL.
  lv_integer = lo_result->get_inputtokens( ).
ENDIF.