Repository navigation
fix(a2a): record agent cost_per_query and input tokens on native send path - #31979
Conversation
Greptile SummaryFixes two independent bugs in A2A agent cost tracking on the native non-streaming send path: agent pricing params were not merged into the proxy-built logging object, so
Confidence Score: 5/5Safe to merge — the changes are tightly scoped to the non-streaming A2A cost-tracking path and add no new external calls or schema changes. Both fixes are minimal and targeted: one merges three pricing keys into an existing dict, the other adds a single model_dump call before text extraction. The streaming path and completion-bridge path are untouched and unaffected. Two new regression tests directly exercise each defect, and the existing test suite assertions are unchanged (only line-wrap reformatting). No migration, schema, or auth-boundary changes are involved. No files require special attention.
|
| Filename | Overview |
|---|---|
| litellm/a2a_protocol/main.py | Adds _set_litellm_params_on_logging_obj to merge agent pricing params (cost_per_query, input/output_cost_per_token) into the proxy-provided logging object's litellm_params on the non-streaming path; called correctly after usage is set and before return. |
| litellm/a2a_protocol/utils.py | Adds model_dump(mode="json") call on the input message before text extraction so the dict branch is taken for SDK Part RootModels, fixing prompt_tokens always being 0. |
| tests/test_litellm/a2a_protocol/test_cost_calculator.py | Adds regression test for cost_per_query via litellm_params dict; remaining changes are pure line-wrap reformatting with no logic changes to existing assertions. |
| tests/test_litellm/a2a_protocol/test_utils.py | New file with a regression test that constructs a real SDK SendMessageRequest (with Part RootModel) and asserts prompt_tokens > 0, catching the exact object-extraction bug fixed in utils.py. |
Reviews (1): Last reviewed commit: "fix(a2a): record agent cost_per_query an..." | Re-trigger Greptile
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
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
Registered an A2A agent with per-query and per-token pricing, then invoked it through the native
/a2a/{agent_id}endpoint on a local proxy:Before fix:
After fix (agent configured with
cost_per_query=0.5):spendnow matches the configuredcost_per_queryexactly, andprompt_tokensis no longer always 0.Type
🐛 Bug Fix
Changes
An A2A agent configured with
cost_per_query/input_cost_per_token/output_cost_per_tokenalways logged$0.00spend andprompt_tokens=0when invoked through the native/a2aendpoint.Two independent gaps caused this:
asend_message's non-streaming flow received the agent's pricing params but never wrote them ontomodel_call_details["litellm_params"], soA2ACostCalculatoralways readNoneforcost_per_query. The streaming path already did this correctly (_build_streaming_logging_obj); this adds the missing merge step to the non-streaming path, mirroring that pattern.PartRootModels (kind/textlive onpart.root, notpartitself), so the object-based extraction branch always returned empty text. The response side already worked because it operates on amodel_dump()dict; this dumps the input message to a dict before extraction too, so both sides go through the same working code path.Added a regression test for each defect: one exercising
asend_messagewith pricing passed via thelitellm_paramsdict (matching how the proxy calls it), and one exercising the usage calculator with a real SDK request object (a dict-based test wouldn't catch the RootModel bug).