fix(a2a): populate response usage in a2a chat transformation - #31980
Conversation
Greptile SummaryThis PR fixes the A2A chat provider always returning
Confidence Score: 5/5Safe to merge — the change is additive, wrapped in a best-effort try/except so a tokenizer failure cannot break the response path, and it exactly mirrors the pattern already in production for three other agent bridge providers. The fix is a small, well-scoped addition that follows a pattern proven across three other providers in the same codebase. The estimation runs inside a try/except so any edge-case failure degrades gracefully to the existing 0/0/0 behaviour rather than raising. The new test is mock-only and validates the core regression directly. No files require special attention.
|
| Filename | Overview |
|---|---|
| litellm/llms/a2a/chat/transformation.py | Adds best-effort token usage estimation via token_counter, following the identical pattern used by langgraph, vertex_ai/agent_engine, and azure_ai/agents providers. Usage and import are correct. |
| tests/test_litellm/llms/a2a/chat/test_a2a_chat_transformation.py | New regression test using mocks only; correctly verifies that prompt_tokens, completion_tokens, and total_tokens are all non-zero and self-consistent after the fix. |
Reviews (2): Last reviewed commit: "test(a2a): rename to avoid module basena..." | Re-trigger Greptile
| except Exception: # noqa: BLE001 - best-effort estimate; a tokenizer hiccup must not break the response | ||
| pass |
There was a problem hiding this comment.
The bare
except Exception: pass silently swallows any token-counting failure with no log output, making it impossible to diagnose why usage stays at zero for a user. Both langgraph/chat/transformation.py and vertex_ai/agent_engine/transformation.py use the same pattern but emit a verbose_logger.warning so the failure is at least visible in debug logs.
| except Exception: # noqa: BLE001 - best-effort estimate; a tokenizer hiccup must not break the response | |
| pass | |
| except Exception as e: # noqa: BLE001 - best-effort estimate; a tokenizer hiccup must not break the response | |
| verbose_logger.warning(f"A2A: failed to estimate token usage: {e}") |
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
…est_transformation.py
Relevant issues
Linear ticket
Resolves LIT-3291
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
@greptileaiand received a Confidence Score of at least 4/5 before requesting a maintainer reviewDelays in PR merge?
If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).
Screenshots / Proof of Fix
Called the
a2a/provider through/chat/completionson a local proxy configured with per-token pricing for the model:Before fix:
{"choices": [...], "usage": {"prompt_tokens": 0, "completion_tokens": 0, "total_tokens": 0}}After fix:
{"choices": [...], "usage": {"prompt_tokens": 12, "completion_tokens": 9, "total_tokens": 21}}usageis no longer0/0/0, and spend logs now show real cost computed from the configuredinput_cost_per_token/output_cost_per_tokenagainst this usage (previously spend was$0.00because cost is priced off a zero token count).Type
🐛 Bug Fix
Changes
The
a2a/chat-completions provider (litellm/llms/a2a/chat/transformation.py) never setusageon the transformed response, so callers ofmodel=a2a/<agent>always gotusage: {prompt_tokens: 0, completion_tokens: 0, total_tokens: 0}directly in the API response, and per-token cost always computed to$0.00since it prices off that zero usage.A2A agents don't return token usage in their protocol responses, so this estimates it with
litellm.utils.token_counteroff the request messages and response text, following the same pattern already used by thelanggraphandlangflowagent-bridge providers for the same "agent doesn't report usage" situation.