Skip to content

Commit fae5c83

Browse files
committed
doc: add inline comments for token count approximation
1 parent 3a57302 commit fae5c83

File tree

1 file changed

+12
-0
lines changed
  • aws-distro-opentelemetry-node-autoinstrumentation/src/patches/aws/services

1 file changed

+12
-0
lines changed

aws-distro-opentelemetry-node-autoinstrumentation/src/patches/aws/services/bedrock.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,9 @@ export class BedrockRuntimeServiceExtension implements ServiceExtension {
265265
}
266266
} else if (modelId.includes('mistral.mistral')) {
267267
if (requestBody.prompt !== undefined) {
268+
// NOTE: We approximate the token count since this value is not directly available in the body
269+
// According to Bedrock docs they use (total_chars / 6) to approximate token count for pricing.
270+
// https://docs.aws.amazon.com/bedrock/latest/userguide/model-customization-prepare.html
268271
spanAttributes[AwsSpanProcessingUtil.GEN_AI_USAGE_INPUT_TOKENS] = Math.ceil(requestBody.prompt.length / 6);
269272
}
270273
if (requestBody.max_tokens !== undefined) {
@@ -326,11 +329,17 @@ export class BedrockRuntimeServiceExtension implements ServiceExtension {
326329
}
327330
} else if (currentModelId.includes('cohere.command')) {
328331
if (responseBody.prompt !== undefined) {
332+
// NOTE: We approximate the token count since this value is not directly available in the body
333+
// According to Bedrock docs they use (total_chars / 6) to approximate token count for pricing.
334+
// https://docs.aws.amazon.com/bedrock/latest/userguide/model-customization-prepare.html
329335
span.setAttribute(AwsSpanProcessingUtil.GEN_AI_USAGE_INPUT_TOKENS, Math.ceil(responseBody.prompt.length / 6));
330336
}
331337
if (responseBody.generations?.[0]?.text !== undefined) {
332338
span.setAttribute(
333339
AwsSpanProcessingUtil.GEN_AI_USAGE_OUTPUT_TOKENS,
340+
// NOTE: We approximate the token count since this value is not directly available in the body
341+
// According to Bedrock docs they use (total_chars / 6) to approximate token count for pricing.
342+
// https://docs.aws.amazon.com/bedrock/latest/userguide/model-customization-prepare.html
334343
Math.ceil(responseBody.generations[0].text.length / 6)
335344
);
336345
}
@@ -355,6 +364,9 @@ export class BedrockRuntimeServiceExtension implements ServiceExtension {
355364
if (responseBody.outputs?.[0]?.text !== undefined) {
356365
span.setAttribute(
357366
AwsSpanProcessingUtil.GEN_AI_USAGE_OUTPUT_TOKENS,
367+
// NOTE: We approximate the token count since this value is not directly available in the body
368+
// According to Bedrock docs they use (total_chars / 6) to approximate token count for pricing.
369+
// https://docs.aws.amazon.com/bedrock/latest/userguide/model-customization-prepare.html
358370
Math.ceil(responseBody.outputs[0].text.length / 6)
359371
);
360372
}

0 commit comments

Comments
 (0)