fix(agents): exclude discovery from invocation billing

This commit is contained in:
Joshua Valluru 2026-09-26 17:23:32 -07:00
parent 5b64f51c1e
commit cc7cb34e10
3 changed files with 12 additions and 6 deletions

View file

@ -240,7 +240,7 @@ async def prepare_agent_invocation(
if not await AgentRequestHandler.is_agent_allowed(effective.agent_id, auth):
raise_identity_failure(AgentIdentityFailure(message="The caller is not permitted to invoke this agent"))
auth.invoked_agent_id = effective.agent_id
if auth.agent_id is None and (effective.identity_managed or effective.litellm_budget_table is not None):
if billable and auth.agent_id is None and (effective.identity_managed or effective.litellm_budget_table is not None):
auth.billing_agent_policy = effective
raw_fee: Final = (effective.litellm_params or MappingProxyType({})).get("cost_per_query", 0.0) if billable else 0.0
try:

View file

@ -3244,7 +3244,8 @@ async def _authorize_authenticated_request(
user_api_key_auth_obj,
target_name,
store,
billable=request_data.get("method")
billable=request.method == "POST"
and request_data.get("method")
in (None, "message/send", "message/stream", "SendMessage", "SendStreamingMessage"),
)
await _run_centralized_common_checks(

View file

@ -129,9 +129,11 @@ async def test_agent_budget_accumulates_across_credentials_and_denies_the_next_a
@pytest.mark.asyncio
@pytest.mark.parametrize("autonomous", (True, False))
@pytest.mark.parametrize("billable", (True, False))
async def test_invocation_prepares_target_fee_for_the_correct_agent(
monkeypatch: pytest.MonkeyPatch,
autonomous: bool,
billable: bool,
) -> None:
from unittest.mock import AsyncMock, MagicMock
@ -158,11 +160,14 @@ async def test_invocation_prepares_target_fee_for_the_correct_agent(
caller: Final = agent(agent_id="caller", object_permission=permission.model_dump())
auth.managed_agent_policy = caller
auth.billing_agent_policy = caller
await prepare_agent_invocation(auth, "agent", AgentIdentityStore.from_client(database))
assert auth.agent_invocation_cost == pytest.approx(0.25)
await prepare_agent_invocation(auth, "agent", AgentIdentityStore.from_client(database), billable=billable)
assert auth.agent_invocation_cost == pytest.approx(0.25 if billable else 0.0)
assert auth.invoked_agent_id == "agent"
assert auth.billing_agent_policy is not None
assert auth.billing_agent_policy.agent_id == ("caller" if autonomous else "agent")
if autonomous or billable:
assert auth.billing_agent_policy is not None
assert auth.billing_agent_policy.agent_id == ("caller" if autonomous else "agent")
else:
assert auth.billing_agent_policy is None
@pytest.mark.asyncio