diff --git a/litellm/proxy/agent_endpoints/auth/managed_authorization.py b/litellm/proxy/agent_endpoints/auth/managed_authorization.py index 48980c780a0..ec2949b1b05 100644 --- a/litellm/proxy/agent_endpoints/auth/managed_authorization.py +++ b/litellm/proxy/agent_endpoints/auth/managed_authorization.py @@ -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: diff --git a/litellm/proxy/auth/user_api_key_auth.py b/litellm/proxy/auth/user_api_key_auth.py index 2829bc95b42..014c7f4fcdf 100644 --- a/litellm/proxy/auth/user_api_key_auth.py +++ b/litellm/proxy/auth/user_api_key_auth.py @@ -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( diff --git a/tests/test_litellm/proxy/agent_endpoints/auth/test_managed_authorization.py b/tests/test_litellm/proxy/agent_endpoints/auth/test_managed_authorization.py index e0cda7ce62d..946067ab6d8 100644 --- a/tests/test_litellm/proxy/agent_endpoints/auth/test_managed_authorization.py +++ b/tests/test_litellm/proxy/agent_endpoints/auth/test_managed_authorization.py @@ -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