Skip to content

fix(a2a): record agent cost_per_query and input tokens on native send path - #31979

Merged
Sameerlite merged 2 commits into
litellm_internal_stagingfrom
litellm_fix_a2a_agent_spend_native
Jul 3, 2026
Merged

Sameerlite merged 2 commits into
litellm_internal_stagingfrom
litellm_fix_a2a_agent_spend_native

Conversation

@michelligabriele

Copy link
Copy Markdown
Contributor

Relevant issues

Linear ticket

Resolves LIT-3291

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have added meaningful tests
  • My PR passes all CI/CD checks (e.g., lint, format, unit tests)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • I have requested a Greptile review by commenting @greptileai and received a Confidence Score of at least 4/5 before requesting a maintainer review

Delays 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:

curl -s -X POST "http://localhost:4000/a2a/$AGENT_ID" \
  -H "Authorization: Bearer sk-1234" -H "Content-Type: application/json" \
  -d '{"jsonrpc":"2.0","id":"req-1","method":"message/send","params":{"message":{"role":"user","parts":[{"kind":"text","text":"Say hello in one word"}],"messageId":"m-1"}}}'

sleep 15
curl -s "http://localhost:4000/spend/logs" -H "Authorization: Bearer sk-1234" \
  | python3 -m json.tool | grep -E '"(model|call_type|spend|total_tokens|prompt_tokens|completion_tokens)"'

Before fix:

model=a2a_agent/Echo Agent  call_type=asend_message  spend=0.0  prompt_tokens=0  completion_tokens=7  total_tokens=7

After fix (agent configured with cost_per_query=0.5):

model=a2a_agent/Echo Agent  call_type=asend_message  spend=0.5  prompt_tokens=5  completion_tokens=7  total_tokens=12

spend now matches the configured cost_per_query exactly, and prompt_tokens is no longer always 0.

Type

🐛 Bug Fix

Changes

An A2A agent configured with cost_per_query / input_cost_per_token / output_cost_per_token always logged $0.00 spend and prompt_tokens=0 when invoked through the native /a2a endpoint.

Two independent gaps caused this:

  1. asend_message's non-streaming flow received the agent's pricing params but never wrote them onto model_call_details["litellm_params"], so A2ACostCalculator always read None for cost_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.
  2. Input-token extraction operated on the raw request object, whose parts are Part RootModels (kind/text live on part.root, not part itself), so the object-based extraction branch always returned empty text. The response side already worked because it operates on a model_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_message with pricing passed via the litellm_params dict (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).

@greptile-apps

greptile-apps Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

Fixes 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 A2ACostCalculator always read None for cost_per_query; and input text extraction operated on raw Pydantic RootModel objects instead of dicts, so prompt_tokens was always 0.

  • _set_litellm_params_on_logging_obj is added to main.py and called in asend_message after usage is computed, merging cost_per_query / input_cost_per_token / output_cost_per_token from litellm_params into model_call_details["litellm_params"] without overwriting existing proxy context — mirroring the pattern already used by the streaming path.
  • utils.py calls model_dump(mode="json") on the input message before text extraction, so the dict code path is taken for SDK Part RootModels whose kind/text fields live on .root, not directly on the object.
  • Two focused regression tests are added: one exercising asend_message with pricing via the litellm_params dict, one exercising the usage calculator with a real SDK request object.

Confidence Score: 5/5

Safe 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.

Important Files Changed

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

codecov Bot commented Jul 2, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.30769% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
litellm/a2a_protocol/main.py 90.90% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@Sameerlite Sameerlite left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Sameerlite
Sameerlite merged commit 30ddef7 into litellm_internal_staging Jul 3, 2026
74 checks passed
@Sameerlite
Sameerlite deleted the litellm_fix_a2a_agent_spend_native branch July 3, 2026 03:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants