Skip to content

Commit 55e294e

Browse files
nirgaclaude
andcommitted
refactor: use official Anthropic SDK types instead of custom types
- Replace custom ThinkingParams with official BetaThinkingConfigParam - Use BetaMessageCreateParamsNonStreaming from @anthropic-ai/sdk - Remove redundant custom type definitions from types.ts - Maintain same functionality with better type safety and consistency This follows best practices by using the official SDK types rather than recreating them, ensuring compatibility and reducing maintenance overhead. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent e45441b commit 55e294e

File tree

2 files changed

+10
-17
lines changed

2 files changed

+10
-17
lines changed

packages/instrumentation-anthropic/src/instrumentation.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import {
3131
CONTEXT_KEY_ALLOW_TRACE_CONTENT,
3232
SpanAttributes,
3333
} from "@traceloop/ai-semantic-conventions";
34-
import { AnthropicInstrumentationConfig, MessageCreateParamsWithThinking } from "./types";
34+
import { AnthropicInstrumentationConfig } from "./types";
3535
import { version } from "../package.json";
3636
import type * as anthropic from "@anthropic-ai/sdk";
3737
import type {
@@ -45,6 +45,9 @@ import type {
4545
Message,
4646
MessageStreamEvent,
4747
} from "@anthropic-ai/sdk/resources/messages";
48+
import type {
49+
MessageCreateParamsNonStreaming as BetaMessageCreateParamsNonStreaming,
50+
} from "@anthropic-ai/sdk/resources/beta/messages";
4851
import type { Stream } from "@anthropic-ai/sdk/streaming";
4952
import type { APIPromise, BaseAnthropic } from "@anthropic-ai/sdk";
5053

@@ -213,12 +216,11 @@ export class AnthropicInstrumentation extends InstrumentationBase {
213216
attributes[SpanAttributes.LLM_REQUEST_TOP_P] = params.top_p;
214217
attributes[SpanAttributes.LLM_TOP_K] = params.top_k;
215218

216-
// Handle thinking parameters
217-
const paramsWithThinking = params as MessageCreateParamsWithThinking;
218-
if (paramsWithThinking.thinking) {
219-
const thinking = paramsWithThinking.thinking;
220-
attributes["llm.request.thinking.type"] = thinking.type;
221-
attributes["llm.request.thinking.budget_tokens"] = thinking.budget_tokens;
219+
// Handle thinking parameters (for beta messages)
220+
const betaParams = params as BetaMessageCreateParamsNonStreaming;
221+
if (betaParams.thinking && betaParams.thinking.type === "enabled") {
222+
attributes["llm.request.thinking.type"] = betaParams.thinking.type;
223+
attributes["llm.request.thinking.budget_tokens"] = betaParams.thinking.budget_tokens;
222224
}
223225

224226
if (type === "completion") {

packages/instrumentation-anthropic/src/types.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,7 @@ export interface AnthropicInstrumentationConfig extends InstrumentationConfig {
88
traceContent?: boolean;
99

1010
/**
11-
* A custom logger to log any exceptions that happen during span created.
11+
* A custom logger to log any exceptions that happen during span creation.
1212
*/
1313
exceptionLogger?: (e: Error) => void;
1414
}
15-
16-
export interface ThinkingParams {
17-
type: "enabled";
18-
budget_tokens: number;
19-
}
20-
21-
export interface MessageCreateParamsWithThinking {
22-
thinking?: ThinkingParams;
23-
}

0 commit comments

Comments
 (0)