diff --git a/ag2/config/anthropic/anthropic_client.py b/ag2/config/anthropic/anthropic_client.py index ad0b172475a1..297281b57117 100644 --- a/ag2/config/anthropic/anthropic_client.py +++ b/ag2/config/anthropic/anthropic_client.py @@ -9,7 +9,7 @@ from typing import Any, Literal, TypedDict import httpx2 -from anthropic import NOT_GIVEN, AsyncAnthropic +from anthropic import NOT_GIVEN, AsyncAnthropic, OpenTelemetryClientOption from anthropic.types import ( ContainerUploadBlock, Message, @@ -105,6 +105,7 @@ def __init__( prompt_caching: bool = True, cache_diagnostics: bool = False, extra_body: dict[str, Any] | None = None, + open_telemetry: OpenTelemetryClientOption = None, ) -> None: self._client = AsyncAnthropic( api_key=api_key, @@ -113,6 +114,7 @@ def __init__( max_retries=max_retries, default_headers=default_headers, http_client=http_client, + open_telemetry=open_telemetry, ) self._create_options = {k: v for k, v in (create_options or {}).items() if k != "stream"} diff --git a/ag2/config/anthropic/config.py b/ag2/config/anthropic/config.py index ebb676c21a9d..1bea0a09c460 100644 --- a/ag2/config/anthropic/config.py +++ b/ag2/config/anthropic/config.py @@ -6,6 +6,7 @@ from typing import TYPE_CHECKING, Any, TypedDict, overload import httpx2 +from anthropic import OpenTelemetryClientOption from anthropic.types import ModelParam from typing_extensions import Unpack, deprecated @@ -33,6 +34,7 @@ class AnthropicConfigOverrides(TypedDict, total=False): cache_diagnostics: bool extra_body: dict[str, Any] | None speed: Speed | None + open_telemetry: OpenTelemetryClientOption class _DeprecatedConfigOverrides(AnthropicConfigOverrides, total=False): @@ -66,6 +68,11 @@ class AnthropicConfig(ModelConfig): extra_body: dict[str, Any] | None = None speed: Speed | None = None """Inference speed; some models require a fast-mode beta header for either value.""" + open_telemetry: OpenTelemetryClientOption = None + """The SDK's own OpenTelemetry tracing of API calls: `False` disables its spans, a dict configures them. + + `None` leaves the option unset, so the SDK reads the `ANTHROPIC_OPEN_TELEMETRY*` environment variables. + """ if TYPE_CHECKING: # The generated `__init__` carries the deprecated sampling parameters; these overloads @@ -89,6 +96,7 @@ def __init__( cache_diagnostics: bool = False, extra_body: dict[str, Any] | None = None, speed: Speed | None = None, + open_telemetry: OpenTelemetryClientOption = None, ) -> None: ... @overload @@ -114,6 +122,7 @@ def __init__( cache_diagnostics: bool = False, extra_body: dict[str, Any] | None = None, speed: Speed | None = None, + open_telemetry: OpenTelemetryClientOption = None, ) -> None: ... def __init__(self, *args: Any, **kwargs: Any) -> None: ... @@ -161,6 +170,7 @@ def create(self) -> AnthropicClient: prompt_caching=self.prompt_caching, cache_diagnostics=self.cache_diagnostics, extra_body=sampling_extra_body(sampling, self.extra_body), + open_telemetry=self.open_telemetry, ) def create_files_client(self) -> AnthropicFilesClient: diff --git a/ag2/config/anthropic/files.py b/ag2/config/anthropic/files.py index c0da859ecfbf..45021e6e860e 100644 --- a/ag2/config/anthropic/files.py +++ b/ag2/config/anthropic/files.py @@ -27,6 +27,7 @@ def __init__(self, config: "AnthropicConfig") -> None: max_retries=config.max_retries, default_headers=config.default_headers, http_client=config.http_client, + open_telemetry=config.open_telemetry, ) async def upload(self, data: bytes, filename: str, purpose: str | None = None) -> UploadedFile: diff --git a/test/config/anthropic/test_anthropic_config.py b/test/config/anthropic/test_anthropic_config.py index af453911b4ca..84ba630b4424 100644 --- a/test/config/anthropic/test_anthropic_config.py +++ b/test/config/anthropic/test_anthropic_config.py @@ -136,3 +136,38 @@ def test_send_sampling_merges_and_extra_body_wins() -> None: result = _compat._send_sampling({"temperature": 0.2, "top_k": 5}, {"top_k": 9}) assert result == {"temperature": 0.2, "top_k": 9} + + +def test_open_telemetry_defaults_to_none() -> None: + config = AnthropicConfig(model="claude-sonnet-5") + + assert config.open_telemetry is None + + +@pytest.mark.parametrize("open_telemetry", [None, False, {"traces": {"content_mode": "content"}}]) +def test_open_telemetry_passed_to_sdk_clients(open_telemetry: Any, monkeypatch: pytest.MonkeyPatch) -> None: + from ag2.config.anthropic import anthropic_client, files + + calls: list[dict[str, Any]] = [] + + def fake_async_anthropic(**kwargs: Any) -> object: + calls.append(kwargs) + return object() + + monkeypatch.setattr(anthropic_client, "AsyncAnthropic", fake_async_anthropic) + monkeypatch.setattr(files, "AsyncAnthropic", fake_async_anthropic) + config = AnthropicConfig(model="claude-sonnet-5", api_key="test-key", open_telemetry=open_telemetry) + + config.create() + config.create_files_client() + + assert [call["open_telemetry"] for call in calls] == [open_telemetry, open_telemetry] + + +def test_copy_with_open_telemetry() -> None: + base = AnthropicConfig(model="claude-sonnet-5") + + copied = base.copy(open_telemetry=False) + + assert copied.open_telemetry is False + assert base.open_telemetry is None diff --git a/website/docs/user-guide/telemetry/agent.mdx b/website/docs/user-guide/telemetry/agent.mdx index 6d821d5c4c76..0ac9f454aa0d 100644 --- a/website/docs/user-guide/telemetry/agent.mdx +++ b/website/docs/user-guide/telemetry/agent.mdx @@ -64,6 +64,20 @@ invoke_agent assistant +-- await_human_input assistant # human-in-the-loop ``` +### Anthropic SDK spans + +The `anthropic` SDK traces its own API calls with OpenTelemetry, on by default. When a global tracer provider is +registered, each request also produces an `anthropic.messages.create` client span (scope `com.anthropic.sdk.python`), +nested under AG2's `chat` span, and the SDK sends a `traceparent` header. Set `open_telemetry` on `AnthropicConfig` +to configure or disable these spans; leaving it unset keeps the SDK's `ANTHROPIC_OPEN_TELEMETRY*` environment +variables in charge: + +```python linenums="1" +from ag2.config import AnthropicConfig + +config = AnthropicConfig(model="claude-sonnet-5", open_telemetry=False) # no SDK spans, only AG2's +``` + ## Span Types Every span includes an `ag2.span.type` attribute: