Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion ag2/config/anthropic/anthropic_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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"}
Expand Down
10 changes: 10 additions & 0 deletions ag2/config/anthropic/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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: ...
Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions ag2/config/anthropic/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
35 changes: 35 additions & 0 deletions test/config/anthropic/test_anthropic_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 14 additions & 0 deletions website/docs/user-guide/telemetry/agent.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading