Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(a2a): populate response usage in a2a chat transformation (#31980)

michelligabrielecbe23d6184af
diff --git a/litellm/llms/a2a/chat/transformation.py b/litellm/llms/a2a/chat/transformation.py
index c9623a817bf..113c000f352 100644
--- a/litellm/llms/a2a/chat/transformation.py
+++ b/litellm/llms/a2a/chat/transformation.py
@@ -10,7 +10,7 @@ import httpx
from litellm.llms.base_llm.base_model_iterator import BaseModelResponseIterator
from litellm.llms.base_llm.chat.transformation import BaseConfig, BaseLLMException
from litellm.types.llms.openai import AllMessageValues
-from litellm.types.utils import Choices, Message, ModelResponse
+from litellm.types.utils import Choices, Message, ModelResponse, Usage
from ..common_utils import (
A2AError,
@@ -312,6 +312,25 @@ class A2AConfig(BaseConfig):
# Set ID from response
model_response.id = response_json.get("id", str(uuid.uuid4()))
+ # A2A agents don't return token usage; estimate it so per-token pricing
+ # produces real cost and callers don't receive usage of 0/0/0.
+ try:
+ from litellm.utils import token_counter
+
+ prompt_tokens = token_counter(model="gpt-3.5-turbo", messages=messages)
+ completion_tokens = token_counter(model="gpt-3.5-turbo", text=text, count_response_tokens=True)
+ setattr(
+ model_response,
+ "usage",
+ Usage(
+ prompt_tokens=prompt_tokens,
+ completion_tokens=completion_tokens,
+ total_tokens=prompt_tokens + completion_tokens,
+ ),
+ )
+ except Exception: # noqa: BLE001 - best-effort estimate; a tokenizer hiccup must not break the response
+ pass
+
return model_response
def get_model_response_iterator(
diff --git a/tests/test_litellm/llms/a2a/chat/test_a2a_chat_transformation.py b/tests/test_litellm/llms/a2a/chat/test_a2a_chat_transformation.py
new file mode 100644
index 00000000000..2e11c68244c
--- /dev/null
+++ b/tests/test_litellm/llms/a2a/chat/test_a2a_chat_transformation.py
@@ -0,0 +1,42 @@
+"""Tests for litellm/llms/a2a/chat/transformation.py response transform."""
+
+from unittest.mock import MagicMock
+
+from litellm.llms.a2a.chat.transformation import A2AConfig
+from litellm.types.utils import ModelResponse
+
+
+def _raw_response(text: str) -> MagicMock:
+ raw = MagicMock()
+ raw.status_code = 200
+ raw.headers = {}
+ raw.json.return_value = {
+ "jsonrpc": "2.0",
+ "id": "resp-1",
+ "result": {
+ "kind": "message",
+ "parts": [{"kind": "text", "text": text}],
+ },
+ }
+ return raw
+
+
+def test_transform_response_sets_usage():
+ """Regression: A2AConfig.transform_response must populate usage so per-token
+ pricing computes real cost and callers don't get usage 0/0/0."""
+ result = A2AConfig().transform_response(
+ model="a2a/test-agent",
+ raw_response=_raw_response("hello from the agent"),
+ model_response=ModelResponse(),
+ logging_obj=MagicMock(),
+ request_data={},
+ messages=[{"role": "user", "content": "hi there agent"}],
+ optional_params={},
+ litellm_params={},
+ encoding=None,
+ )
+
+ assert result.usage is not None
+ assert result.usage.prompt_tokens > 0
+ assert result.usage.completion_tokens > 0
+ assert result.usage.total_tokens == (result.usage.prompt_tokens + result.usage.completion_tokens)