Skip to content

Commit d4be724

Browse files
Dwij1704dot-agi
andauthored
Update Anthropic instrumentation (#918)
* Update Anthropic instrumentation * Refactor EventHandlerWrapper to streamline event forwarding and remove unused EventHandler class. Update original_handler type to Any for flexibility. * Implement content processing and message simplification in Anthropic instrumentation. Introduce helper functions to handle various content types and extract attributes for telemetry. Update `get_message_request_attributes` to streamline message handling and support complex content structures. * Removed unused utility functions from Anthropic instrumentation. * cleanup * Update `extract_tool_definitions` and `get_tool_attributes` functions to replace old attribute keys with new `MessageAttributes` constants for correct indexing. * remove deprecated message simplification logic from `get_message_request_attributes` * added unit tests * fix notebook for incorrect message append * put content in markdown instead of code --------- Co-authored-by: Pratyush Shukla <[email protected]>
1 parent 507a386 commit d4be724

30 files changed

+2441
-1430
lines changed

agentops/instrumentation/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def get_instance(self) -> BaseInstrumentor:
5858
provider_import_name="openai",
5959
),
6060
InstrumentorLoader(
61-
module_name="opentelemetry.instrumentation.anthropic",
61+
module_name="agentops.instrumentation.anthropic",
6262
class_name="AnthropicInstrumentor",
6363
provider_import_name="anthropic",
6464
),
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""Anthropic API instrumentation.
2+
3+
This module provides instrumentation for the Anthropic API,
4+
including chat completions, streaming, and event handling.
5+
"""
6+
7+
import logging
8+
from typing import Collection
9+
10+
def get_version() -> str:
11+
"""Get the version of the Anthropic SDK, or 'unknown' if not found
12+
13+
Attempts to retrieve the installed version of the Anthropic SDK using importlib.metadata.
14+
Falls back to 'unknown' if the version cannot be determined.
15+
16+
Returns:
17+
The version string of the Anthropic SDK or 'unknown'
18+
"""
19+
try:
20+
from importlib.metadata import version
21+
return version("anthropic")
22+
except ImportError:
23+
logger.debug("Could not find Anthropic SDK version")
24+
return "unknown"
25+
26+
LIBRARY_NAME = "anthropic"
27+
LIBRARY_VERSION: str = get_version()
28+
29+
logger = logging.getLogger(__name__)
30+
31+
# Import after defining constants to avoid circular imports
32+
from agentops.instrumentation.anthropic.instrumentor import AnthropicInstrumentor # noqa: E402
33+
34+
__all__ = [
35+
"LIBRARY_NAME",
36+
"LIBRARY_VERSION",
37+
"AnthropicInstrumentor",
38+
]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""Attribute extraction for Anthropic API instrumentation."""
2+
3+
from agentops.instrumentation.anthropic.attributes.common import get_common_instrumentation_attributes
4+
from agentops.instrumentation.anthropic.attributes.message import get_message_attributes, get_completion_attributes
5+
from agentops.instrumentation.anthropic.attributes.tools import (
6+
extract_tool_definitions,
7+
extract_tool_use_blocks,
8+
extract_tool_results,
9+
get_tool_attributes
10+
)
11+
12+
__all__ = [
13+
"get_common_instrumentation_attributes",
14+
"get_message_attributes",
15+
"get_completion_attributes",
16+
"extract_tool_definitions",
17+
"extract_tool_use_blocks",
18+
"extract_tool_results",
19+
"get_tool_attributes",
20+
]
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
"""Common attribute extraction for Anthropic instrumentation."""
2+
3+
from typing import Dict, Any
4+
5+
from agentops.logging import logger
6+
from agentops.semconv import InstrumentationAttributes, SpanAttributes
7+
from agentops.instrumentation.common.attributes import AttributeMap, get_common_attributes
8+
from agentops.instrumentation.anthropic import LIBRARY_NAME, LIBRARY_VERSION
9+
10+
def get_common_instrumentation_attributes() -> AttributeMap:
11+
"""Get common instrumentation attributes for the Anthropic instrumentation.
12+
13+
This combines the generic AgentOps attributes with Anthropic specific library attributes.
14+
15+
Returns:
16+
Dictionary of common instrumentation attributes
17+
"""
18+
attributes = get_common_attributes()
19+
attributes.update({
20+
InstrumentationAttributes.LIBRARY_NAME: LIBRARY_NAME,
21+
InstrumentationAttributes.LIBRARY_VERSION: LIBRARY_VERSION,
22+
})
23+
return attributes
24+
25+
26+
def extract_request_attributes(kwargs: Dict[str, Any]) -> AttributeMap:
27+
"""Extract all request attributes from kwargs.
28+
29+
This consolidated function extracts all relevant attributes from the request
30+
kwargs, including model, system prompt, messages, max_tokens, temperature,
31+
and other parameters. It replaces the individual extraction functions with
32+
a single comprehensive approach.
33+
34+
Args:
35+
kwargs: Request keyword arguments
36+
37+
Returns:
38+
Dictionary of extracted request attributes
39+
"""
40+
attributes = {}
41+
42+
# Extract model
43+
if 'model' in kwargs:
44+
attributes[SpanAttributes.LLM_REQUEST_MODEL] = kwargs["model"]
45+
46+
# Extract max_tokens
47+
if 'max_tokens' in kwargs:
48+
attributes[SpanAttributes.LLM_REQUEST_MAX_TOKENS] = kwargs["max_tokens"]
49+
50+
# Extract temperature
51+
if 'temperature' in kwargs:
52+
attributes[SpanAttributes.LLM_REQUEST_TEMPERATURE] = kwargs["temperature"]
53+
54+
# Extract top_p
55+
if "top_p" in kwargs:
56+
attributes[SpanAttributes.LLM_REQUEST_TOP_P] = kwargs["top_p"]
57+
58+
# Extract streaming
59+
if "stream" in kwargs:
60+
attributes[SpanAttributes.LLM_REQUEST_STREAMING] = kwargs["stream"]
61+
62+
return attributes

0 commit comments

Comments
 (0)