@@ -245,6 +245,22 @@ export class BedrockRuntimeServiceExtension implements ServiceExtension {
245245 if ( requestBody . top_p !== undefined ) {
246246 spanAttributes [ AwsSpanProcessingUtil . GEN_AI_REQUEST_TOP_P ] = requestBody . top_p ;
247247 }
248+ } else if ( modelId . includes ( 'cohere.command-r' ) ) {
249+ if ( requestBody . max_tokens !== undefined ) {
250+ spanAttributes [ AwsSpanProcessingUtil . GEN_AI_REQUEST_MAX_TOKENS ] = requestBody . max_tokens ;
251+ }
252+ if ( requestBody . temperature !== undefined ) {
253+ spanAttributes [ AwsSpanProcessingUtil . GEN_AI_REQUEST_TEMPERATURE ] = requestBody . temperature ;
254+ }
255+ if ( requestBody . p !== undefined ) {
256+ spanAttributes [ AwsSpanProcessingUtil . GEN_AI_REQUEST_TOP_P ] = requestBody . p ;
257+ }
258+ if ( requestBody . message !== undefined ) {
259+ // NOTE: We approximate the token count since this value is not directly available in the body
260+ // According to Bedrock docs they use (total_chars / 6) to approximate token count for pricing.
261+ // https://docs.aws.amazon.com/bedrock/latest/userguide/model-customization-prepare.html
262+ spanAttributes [ AwsSpanProcessingUtil . GEN_AI_USAGE_INPUT_TOKENS ] = Math . ceil ( requestBody . message . length / 6 ) ;
263+ }
248264 } else if ( modelId . includes ( 'cohere.command' ) ) {
249265 if ( requestBody . max_tokens !== undefined ) {
250266 spanAttributes [ AwsSpanProcessingUtil . GEN_AI_REQUEST_MAX_TOKENS ] = requestBody . max_tokens ;
@@ -255,6 +271,9 @@ export class BedrockRuntimeServiceExtension implements ServiceExtension {
255271 if ( requestBody . p !== undefined ) {
256272 spanAttributes [ AwsSpanProcessingUtil . GEN_AI_REQUEST_TOP_P ] = requestBody . p ;
257273 }
274+ if ( requestBody . prompt !== undefined ) {
275+ spanAttributes [ AwsSpanProcessingUtil . GEN_AI_USAGE_INPUT_TOKENS ] = Math . ceil ( requestBody . prompt . length / 6 ) ;
276+ }
258277 } else if ( modelId . includes ( 'ai21.jamba' ) ) {
259278 if ( requestBody . max_tokens !== undefined ) {
260279 spanAttributes [ AwsSpanProcessingUtil . GEN_AI_REQUEST_MAX_TOKENS ] = requestBody . max_tokens ;
@@ -329,13 +348,18 @@ export class BedrockRuntimeServiceExtension implements ServiceExtension {
329348 if ( responseBody . stop_reason !== undefined ) {
330349 span . setAttribute ( AwsSpanProcessingUtil . GEN_AI_RESPONSE_FINISH_REASONS , [ responseBody . stop_reason ] ) ;
331350 }
332- } else if ( currentModelId . includes ( 'cohere.command' ) ) {
333- if ( responseBody . prompt !== undefined ) {
351+ } else if ( currentModelId . includes ( 'cohere.command-r' ) ) {
352+ console . log ( 'Response Body:' , responseBody ) ;
353+ if ( responseBody . text !== undefined ) {
334354 // NOTE: We approximate the token count since this value is not directly available in the body
335355 // According to Bedrock docs they use (total_chars / 6) to approximate token count for pricing.
336356 // https://docs.aws.amazon.com/bedrock/latest/userguide/model-customization-prepare.html
337- span . setAttribute ( AwsSpanProcessingUtil . GEN_AI_USAGE_INPUT_TOKENS , Math . ceil ( responseBody . prompt . length / 6 ) ) ;
357+ span . setAttribute ( AwsSpanProcessingUtil . GEN_AI_USAGE_OUTPUT_TOKENS , Math . ceil ( responseBody . text . length / 6 ) ) ;
338358 }
359+ if ( responseBody . finish_reason !== undefined ) {
360+ span . setAttribute ( AwsSpanProcessingUtil . GEN_AI_RESPONSE_FINISH_REASONS , [ responseBody . finish_reason ] ) ;
361+ }
362+ } else if ( currentModelId . includes ( 'cohere.command' ) ) {
339363 if ( responseBody . generations ?. [ 0 ] ?. text !== undefined ) {
340364 span . setAttribute (
341365 AwsSpanProcessingUtil . GEN_AI_USAGE_OUTPUT_TOKENS ,
0 commit comments