Skip to content

Tokens processed in Amazon Bedrock

0

Hi, I'm working with an agent in amazon bedrock using sonnet and I make this flow: Me: "Hi, I need a function in python to call a lambda" Model: "Hi, you need to do this......" Orchestation trace: "inputTokens": 300, "outputTokens": 200

Then I continue with the conversation: Me: "Thank you, now I need to connect Api Gateway in my front, how I can connect the API?" Model: "Okey, you need to do this....." Orchestation trace: "inputTokens": 600, "outputTokens": 230

Then I continue with the conversation: Me: "Thank you, now I need to connect Api Gateway in my front, how I can connect the API?" Model: "Okey, you need to do this....." Orchestation trace: "inputTokens": 900, "outputTokens": 300

Then I continue with the conversation: Me: "Thank you, now I need to connect a DynamoDB to my back, how I can do this?" Model: "Okey, you need to do this....." Orchestation trace: "inputTokens": 1200, "outputTokens": 400

My question is: How the input tokens is processed by bedrock? Like the first prompt processed 300 of input, the the second prompt processed 600, the third 900 and so. I need to know if the last agent processed the 1200 tokens of input or only processed the 300 last tokes (1200-900)

2 Answers
0

IMO It's potentially misleading for the re:Post Agent to say conversation tokens are "typically cumulative", but it's correct that in this case your total tokens processed should be the sum of all the reported numbers.

Agents in Amazon Bedrock support session state which is used to persist information over multiple turns of conversation and remember current context when generating a reply. This contributes to the increasing inputTokens count you see in each turn, but you can refer to the linked doc for options to customize the session management.

In plain Converse or InvokeModel calls, you'd need to manage this session state on the client side: Storing the previous conversation history and passing it in each time, which makes it a bit clearer how & why the inputTokens counts would accumulate each time.

In either case, the inputTokens and outputTokens returned in each API response should be the counts processed for that particular request... So to monitor usage for billing tracking purposes, you'd add them all up i.e. 300+600+900+1200 input tokens, 200+230+300+400 output tokens.

AWS
EXPERT

answered 2 years ago

EXPERT

reviewed 2 years ago

  • When using session state, does it reduces the billing for the sum of tokens? Or is it only to create "memory" of the conversation but it still sends the full token count to the model (and the corresponding billing for those tokens happens)?

    What about prompt cache, would it be a good approach to reduce token billing in this case?

0

In Amazon Bedrock, the input tokens for each interaction in a conversation are typically cumulative. This means that the model maintains context from previous interactions within the same session.

Based on the information provided in your example:

  1. The first prompt processed 300 input tokens.
  2. The second prompt processed 600 input tokens, which likely includes the previous 300 tokens plus the new input.
  3. The third prompt processed 900 input tokens, again including all previous interactions.
  4. The final prompt processed 1200 input tokens, which would include all previous interactions plus the new input.

This cumulative approach allows the model to maintain context throughout the conversation, enabling it to provide more coherent and contextually relevant responses. The model is not just processing the last 300 tokens, but rather the entire conversation history up to that point.

It's important to note that there are usually token limits for models, and if the cumulative input exceeds this limit, older parts of the conversation may be truncated to fit within the model's context window. The exact behavior can depend on the specific model and configuration being used in Amazon Bedrock.

When working with agents in Amazon Bedrock, it's good practice to be aware of these token counts and manage your conversation length accordingly to ensure you stay within the model's capabilities and avoid unexpected truncation of important context.

Sources
Build generative AI applications on Amazon Bedrock with the AWS SDK for Python (Boto3) | AWS Machine Learning Blog

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.