Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 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
2 changes: 1 addition & 1 deletion packages/ai-semantic-conventions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
},
"dependencies": {
"@opentelemetry/api": "^1.9.0",
"@opentelemetry/semantic-conventions": "^1.36.0"
"@opentelemetry/semantic-conventions": "^1.38.0"
},
"homepage": "https://github.com/traceloop/openllmetry-js/tree/main/packages/ai-semantic-conventions",
"gitHead": "ef1e70d6037f7b5c061056ef2be16e3f55f02ed5"
Expand Down
64 changes: 48 additions & 16 deletions packages/ai-semantic-conventions/src/SemanticAttributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,65 @@
*/

import {
ATTR_GEN_AI_AGENT_NAME,
ATTR_GEN_AI_COMPLETION,
ATTR_GEN_AI_CONVERSATION_ID,
ATTR_GEN_AI_INPUT_MESSAGES,
ATTR_GEN_AI_OPERATION_NAME,
ATTR_GEN_AI_OUTPUT_MESSAGES,
ATTR_GEN_AI_PROMPT,
ATTR_GEN_AI_PROVIDER_NAME,
ATTR_GEN_AI_REQUEST_MAX_TOKENS,
ATTR_GEN_AI_REQUEST_MODEL,
ATTR_GEN_AI_REQUEST_TEMPERATURE,
ATTR_GEN_AI_REQUEST_TOP_P,
ATTR_GEN_AI_RESPONSE_FINISH_REASONS,
ATTR_GEN_AI_RESPONSE_ID,
ATTR_GEN_AI_RESPONSE_MODEL,
ATTR_GEN_AI_SYSTEM,
ATTR_GEN_AI_TOOL_CALL_ARGUMENTS,
ATTR_GEN_AI_TOOL_CALL_ID,
ATTR_GEN_AI_TOOL_CALL_RESULT,
ATTR_GEN_AI_TOOL_NAME,
ATTR_GEN_AI_USAGE_COMPLETION_TOKENS,
ATTR_GEN_AI_USAGE_INPUT_TOKENS,
ATTR_GEN_AI_USAGE_OUTPUT_TOKENS,
ATTR_GEN_AI_USAGE_PROMPT_TOKENS,
// @ts-expect-error - Using exports path that TypeScript doesn't recognize but works at runtime
} from "@opentelemetry/semantic-conventions/incubating";

export const SpanAttributes = {
LLM_SYSTEM: "gen_ai.system",
LLM_REQUEST_MODEL: "gen_ai.request.model",
LLM_REQUEST_MAX_TOKENS: "gen_ai.request.max_tokens",
LLM_REQUEST_TEMPERATURE: "gen_ai.request.temperature",
LLM_REQUEST_TOP_P: "gen_ai.request.top_p",
LLM_PROMPTS: "gen_ai.prompt",
LLM_COMPLETIONS: "gen_ai.completion",
LLM_INPUT_MESSAGES: "gen_ai.input.messages",
LLM_OUTPUT_MESSAGES: "gen_ai.output.messages",
LLM_RESPONSE_MODEL: "gen_ai.response.model",
LLM_USAGE_PROMPT_TOKENS: "gen_ai.usage.prompt_tokens",
LLM_USAGE_COMPLETION_TOKENS: "gen_ai.usage.completion_tokens",
LLM_USAGE_INPUT_TOKENS: ATTR_GEN_AI_USAGE_INPUT_TOKENS,
LLM_USAGE_OUTPUT_TOKENS: ATTR_GEN_AI_USAGE_OUTPUT_TOKENS,
ATTR_GEN_AI_SYSTEM,
ATTR_GEN_AI_REQUEST_MODEL,
ATTR_GEN_AI_REQUEST_MAX_TOKENS,
ATTR_GEN_AI_REQUEST_TEMPERATURE,
ATTR_GEN_AI_REQUEST_TOP_P,
ATTR_GEN_AI_PROMPT,
ATTR_GEN_AI_COMPLETION,
ATTR_GEN_AI_INPUT_MESSAGES,
ATTR_GEN_AI_OUTPUT_MESSAGES,
ATTR_GEN_AI_RESPONSE_MODEL,
ATTR_GEN_AI_USAGE_PROMPT_TOKENS,
ATTR_GEN_AI_USAGE_COMPLETION_TOKENS,
ATTR_GEN_AI_USAGE_INPUT_TOKENS,
ATTR_GEN_AI_USAGE_OUTPUT_TOKENS,
ATTR_GEN_AI_OPERATION_NAME,
ATTR_GEN_AI_PROVIDER_NAME,
ATTR_GEN_AI_TOOL_NAME,
ATTR_GEN_AI_TOOL_CALL_ID,
ATTR_GEN_AI_TOOL_CALL_ARGUMENTS,
ATTR_GEN_AI_TOOL_CALL_RESULT,
ATTR_GEN_AI_RESPONSE_ID,
ATTR_GEN_AI_RESPONSE_FINISH_REASONS,
ATTR_GEN_AI_CONVERSATION_ID,
ATTR_GEN_AI_AGENT_NAME,

// Attributes not yet in @opentelemetry/semantic-conventions
Comment thread
nina-kollman marked this conversation as resolved.
GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS:
"gen_ai.usage.cache_creation_input_tokens",
GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS: "gen_ai.usage.cache_read_input_tokens",
GEN_AI_USAGE_REASONING_TOKENS: "gen_ai.usage.reasoning_tokens",

GEN_AI_AGENT_NAME: "gen_ai.agent.name",

// LLM
LLM_REQUEST_TYPE: "llm.request.type",
LLM_USAGE_TOTAL_TOKENS: "llm.usage.total_tokens",
Expand Down
52 changes: 30 additions & 22 deletions packages/instrumentation-anthropic/src/instrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,14 +204,15 @@ export class AnthropicInstrumentation extends InstrumentationBase {
};
}): Span {
const attributes: Attributes = {
[SpanAttributes.LLM_SYSTEM]: "Anthropic",
[SpanAttributes.ATTR_GEN_AI_SYSTEM]: "Anthropic",
[SpanAttributes.LLM_REQUEST_TYPE]: type,
};

try {
attributes[SpanAttributes.LLM_REQUEST_MODEL] = params.model;
attributes[SpanAttributes.LLM_REQUEST_TEMPERATURE] = params.temperature;
attributes[SpanAttributes.LLM_REQUEST_TOP_P] = params.top_p;
attributes[SpanAttributes.ATTR_GEN_AI_REQUEST_MODEL] = params.model;
attributes[SpanAttributes.ATTR_GEN_AI_REQUEST_TEMPERATURE] =
params.temperature;
attributes[SpanAttributes.ATTR_GEN_AI_REQUEST_TOP_P] = params.top_p;
attributes[SpanAttributes.LLM_TOP_K] = params.top_k;

// Handle thinking parameters (for beta messages)
Expand All @@ -223,10 +224,11 @@ export class AnthropicInstrumentation extends InstrumentationBase {
}

if (type === "completion") {
attributes[SpanAttributes.LLM_REQUEST_MAX_TOKENS] =
attributes[SpanAttributes.ATTR_GEN_AI_REQUEST_MAX_TOKENS] =
params.max_tokens_to_sample;
} else {
attributes[SpanAttributes.LLM_REQUEST_MAX_TOKENS] = params.max_tokens;
attributes[SpanAttributes.ATTR_GEN_AI_REQUEST_MAX_TOKENS] =
params.max_tokens;
}

if (
Expand All @@ -244,8 +246,9 @@ export class AnthropicInstrumentation extends InstrumentationBase {

// If a system prompt is provided, it should always be first
if ("system" in params && params.system !== undefined) {
attributes[`${SpanAttributes.LLM_PROMPTS}.0.role`] = "system";
attributes[`${SpanAttributes.LLM_PROMPTS}.0.content`] =
attributes[`${SpanAttributes.ATTR_GEN_AI_PROMPT}.0.role`] =
"system";
attributes[`${SpanAttributes.ATTR_GEN_AI_PROMPT}.0.content`] =
typeof params.system === "string"
? params.system
: JSON.stringify(params.system);
Expand All @@ -254,21 +257,23 @@ export class AnthropicInstrumentation extends InstrumentationBase {

params.messages.forEach((message, index) => {
const currentIndex = index + promptIndex;
attributes[`${SpanAttributes.LLM_PROMPTS}.${currentIndex}.role`] =
message.role;
attributes[
`${SpanAttributes.ATTR_GEN_AI_PROMPT}.${currentIndex}.role`
] = message.role;
if (typeof message.content === "string") {
attributes[
`${SpanAttributes.LLM_PROMPTS}.${currentIndex}.content`
`${SpanAttributes.ATTR_GEN_AI_PROMPT}.${currentIndex}.content`
] = (message.content as string) || "";
} else {
attributes[
`${SpanAttributes.LLM_PROMPTS}.${currentIndex}.content`
`${SpanAttributes.ATTR_GEN_AI_PROMPT}.${currentIndex}.content`
] = JSON.stringify(message.content);
}
});
} else {
attributes[`${SpanAttributes.LLM_PROMPTS}.0.role`] = "user";
attributes[`${SpanAttributes.LLM_PROMPTS}.0.content`] = params.prompt;
attributes[`${SpanAttributes.ATTR_GEN_AI_PROMPT}.0.role`] = "user";
attributes[`${SpanAttributes.ATTR_GEN_AI_PROMPT}.0.content`] =
params.prompt;
}
}
} catch (e) {
Expand Down Expand Up @@ -477,46 +482,49 @@ export class AnthropicInstrumentation extends InstrumentationBase {
result: Completion;
}) {
try {
span.setAttribute(SpanAttributes.LLM_RESPONSE_MODEL, result.model);
span.setAttribute(
SpanAttributes.ATTR_GEN_AI_RESPONSE_MODEL,
result.model,
);
if (type === "chat" && result.usage) {
span.setAttribute(
SpanAttributes.LLM_USAGE_TOTAL_TOKENS,
result.usage?.input_tokens + result.usage?.output_tokens,
);
span.setAttribute(
SpanAttributes.LLM_USAGE_COMPLETION_TOKENS,
SpanAttributes.ATTR_GEN_AI_USAGE_COMPLETION_TOKENS,
result.usage?.output_tokens,
);
span.setAttribute(
SpanAttributes.LLM_USAGE_PROMPT_TOKENS,
SpanAttributes.ATTR_GEN_AI_USAGE_PROMPT_TOKENS,
result.usage?.input_tokens,
);
}

if (result.stop_reason) {
span.setAttribute(
`${SpanAttributes.LLM_COMPLETIONS}.0.finish_reason`,
`${SpanAttributes.ATTR_GEN_AI_COMPLETION}.0.finish_reason`,
result.stop_reason,
);
}

if (this._shouldSendPrompts()) {
if (type === "chat") {
span.setAttribute(
`${SpanAttributes.LLM_COMPLETIONS}.0.role`,
`${SpanAttributes.ATTR_GEN_AI_COMPLETION}.0.role`,
"assistant",
);
span.setAttribute(
`${SpanAttributes.LLM_COMPLETIONS}.0.content`,
`${SpanAttributes.ATTR_GEN_AI_COMPLETION}.0.content`,
JSON.stringify(result.content),
);
} else {
span.setAttribute(
`${SpanAttributes.LLM_COMPLETIONS}.0.role`,
`${SpanAttributes.ATTR_GEN_AI_COMPLETION}.0.role`,
"assistant",
);
span.setAttribute(
`${SpanAttributes.LLM_COMPLETIONS}.0.content`,
`${SpanAttributes.ATTR_GEN_AI_COMPLETION}.0.content`,
result.completion,
);
}
Expand Down
Loading