Skip to content

Commit 18265ac

Browse files
committed
apply message truncation to Anthropic gen_ai.request.messages
1 parent c50ad1b commit 18265ac

File tree

1 file changed

+7
-6
lines changed
  • packages/core/src/utils/anthropic-ai

1 file changed

+7
-6
lines changed

packages/core/src/utils/anthropic-ai/index.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
GEN_AI_RESPONSE_TOOL_CALLS_ATTRIBUTE,
2424
GEN_AI_SYSTEM_ATTRIBUTE,
2525
} from '../ai/gen-ai-attributes';
26+
import { truncateGenAiMessages } from '../ai/messageTruncation';
2627
import { buildMethodPath, getFinalOperationName, getSpanOperation, setTokenUsageAttributes } from '../ai/utils';
2728
import { handleCallbackErrors } from '../handleCallbackErrors';
2829
import { instrumentAsyncIterableStream, instrumentMessageStream } from './streaming';
@@ -71,16 +72,16 @@ function extractRequestAttributes(args: unknown[], methodPath: string): Record<s
7172
return attributes;
7273
}
7374

74-
/**
75-
* Add private request attributes to spans.
76-
* This is only recorded if recordInputs is true.
77-
*/
7875
function addPrivateRequestAttributes(span: Span, params: Record<string, unknown>): void {
7976
if ('messages' in params) {
80-
span.setAttributes({ [GEN_AI_REQUEST_MESSAGES_ATTRIBUTE]: JSON.stringify(params.messages) });
77+
const messages = params.messages;
78+
const truncatedMessages = truncateGenAiMessages(messages as unknown[]);
79+
span.setAttributes({ [GEN_AI_REQUEST_MESSAGES_ATTRIBUTE]: JSON.stringify(truncatedMessages) });
8180
}
8281
if ('input' in params) {
83-
span.setAttributes({ [GEN_AI_REQUEST_MESSAGES_ATTRIBUTE]: JSON.stringify(params.input) });
82+
const input = params.input;
83+
const truncatedInput = truncateGenAiMessages(input as unknown[]);
84+
span.setAttributes({ [GEN_AI_REQUEST_MESSAGES_ATTRIBUTE]: JSON.stringify(truncatedInput) });
8485
}
8586
if ('prompt' in params) {
8687
span.setAttributes({ [GEN_AI_PROMPT_ATTRIBUTE]: JSON.stringify(params.prompt) });

0 commit comments

Comments
 (0)