Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -509,8 +509,9 @@ function recordNovaAttributes(parsedChunk: ParsedChunk, span: Span): void {

function recordClaudeAttributes(parsedChunk: ParsedChunk, span: Span): void {
setClaudeStreamInputTokens(span, parsedChunk.message?.usage);
if (parsedChunk.message?.usage?.output_tokens !== undefined) {
span.setAttribute(GEN_AI_USAGE_OUTPUT_TOKENS, parsedChunk.message.usage.output_tokens);
// `message_start` carries only a placeholder output count. The final count arrives in `message_delta`.
if (parsedChunk.usage?.output_tokens !== undefined) {
span.setAttribute(GEN_AI_USAGE_OUTPUT_TOKENS, parsedChunk.usage.output_tokens);
}
if (parsedChunk.delta?.stop_reason !== undefined) {
span.setAttribute(GEN_AI_RESPONSE_FINISH_REASONS, [parsedChunk.delta.stop_reason]);
Expand Down
21 changes: 21 additions & 0 deletions packages/server-utils/test/integrations/aws-sdk-bedrock.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,27 @@ describe('BedrockRuntimeServiceExtension stream spans', () => {
expect(spanToJSON(span).end_timestamp).toBeDefined();
});

it('reads the Claude stream output tokens from message_delta', async () => {
const span = startInactiveSpan({ name: 'bedrock' });
const encoder = new TextEncoder();
const chunks = [
{ type: 'message_start', message: { role: 'assistant', usage: { input_tokens: 14, output_tokens: 1 } } },
{ type: 'message_delta', delta: { stop_reason: 'end_turn' }, usage: { output_tokens: 42 } },
];
const data = {
body: (async function* () {
for (const chunk of chunks) {
yield { chunk: { bytes: encoder.encode(JSON.stringify(chunk)) } };
}
})(),
};

respond(span, 'InvokeModelWithResponseStream', { modelId: CLAUDE_MODEL_ID }, data);
await drain(data.body);

expect(spanToJSON(span).attributes?.[GEN_AI_USAGE_OUTPUT_TOKENS]).toBe(42);
});

it('ends an InvokeModelWithResponseStream span without a modelId when the stream is drained', async () => {
const span = startInactiveSpan({ name: 'bedrock' });
const data = { body: (async function* () {})() };
Expand Down
Loading