mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
Python: Fix Python OTel usage detail attributes (#6493)
* fix python otel usage detail attributes Map cached/read/reasoning usage detail fields to standard OTel GenAI attributes while preserving provider-specific legacy keys. Add focused coverage for direct response spans, aggregated agent spans, and provider usage parsing. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * address usage detail review feedback Omit missing OpenAI Responses usage detail counts while preserving zero-valued counts. Record zero-valued token usage in OTel histograms and add regression coverage. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
Unverified
parent
d7027fc1f9
commit
d7e8d2206d
@@ -1024,8 +1024,10 @@ class RawAnthropicClient(
|
||||
usage_details["input_token_count"] = usage.input_tokens
|
||||
if usage.cache_creation_input_tokens is not None:
|
||||
usage_details["anthropic.cache_creation_input_tokens"] = usage.cache_creation_input_tokens # type: ignore[typeddict-unknown-key]
|
||||
usage_details["cache_creation_input_token_count"] = usage.cache_creation_input_tokens
|
||||
if usage.cache_read_input_tokens is not None:
|
||||
usage_details["anthropic.cache_read_input_tokens"] = usage.cache_read_input_tokens # type: ignore[typeddict-unknown-key]
|
||||
usage_details["cache_read_input_token_count"] = usage.cache_read_input_tokens
|
||||
return usage_details
|
||||
|
||||
def _parse_contents_from_anthropic(
|
||||
|
||||
@@ -2354,6 +2354,27 @@ def test_parse_usage_with_cache_tokens(mock_anthropic_client: MagicMock) -> None
|
||||
assert result["input_token_count"] == 100
|
||||
assert result["anthropic.cache_creation_input_tokens"] == 20
|
||||
assert result["anthropic.cache_read_input_tokens"] == 30
|
||||
assert result["cache_creation_input_token_count"] == 20
|
||||
assert result["cache_read_input_token_count"] == 30
|
||||
|
||||
|
||||
def test_parse_usage_preserves_zero_cache_tokens(mock_anthropic_client: MagicMock) -> None:
|
||||
"""Test parsing usage preserves zero-valued mapped cache tokens."""
|
||||
client = create_test_anthropic_client(mock_anthropic_client)
|
||||
|
||||
mock_usage = MagicMock()
|
||||
mock_usage.input_tokens = 100
|
||||
mock_usage.output_tokens = 50
|
||||
mock_usage.cache_creation_input_tokens = 0
|
||||
mock_usage.cache_read_input_tokens = 0
|
||||
|
||||
result = client._parse_usage_from_anthropic(mock_usage)
|
||||
|
||||
assert result is not None
|
||||
assert result["anthropic.cache_creation_input_tokens"] == 0
|
||||
assert result["cache_creation_input_token_count"] == 0
|
||||
assert result["anthropic.cache_read_input_tokens"] == 0
|
||||
assert result["cache_read_input_token_count"] == 0
|
||||
|
||||
|
||||
# Code Execution Result Tests
|
||||
|
||||
Reference in New Issue
Block a user