429 error with Azure Cost Management API - what to do?

helen gross 10 Reputation points
2023-03-30T03:24:40.4533333+00:00
Greetings AZ Community,
I am getting a 429 error whenever I interrogate the Cost Management API via Python.
This was happening occasionally but now I am being well and truly throttled.
Can you please advise if adjusting the rate limits in the Azure Resource Manager would help the situation?
Also  “x-ms-ratelimit-microsoft.costmanagement-entity-retry-after” from the header of the API only retrieves ‘none’ after the 429 error.
Could you please advise how I should know how much time I should be waiting for API to go back to normal after a 429 error?

Also, I have a Python Azure SDK question.

I have the following code which should give me the Reservation Transactions which is from the cost management Python SDK examples in GitHub. (I have changed the subscription and billing account ids) and the file pvar.env contains the client_id, tenant_id and client_secret. The service principal has been 
configured. Why would I get a client authentication error 401 when I run this? Ie caller is not authorised.

from azure.identity import DefaultAzureCredential
from azure.mgmt.consumption import ConsumptionManagementClient
from dotenv import load_dotenv
"""
# PREREQUISITES
    pip install azure-identity
    pip install azure-mgmt-consumption
# USAGE
    python reservation_transactions_list_by_enrollment_number.py

    Before run the sample, please set the values of the client ID, tenant ID and client secret
    of the AAD application as environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID,
    AZURE_CLIENT_SECRET. For more info about how to get the value, please see:
    https://docs.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal
"""
load_dotenv("Pvar.env")

def main():
    client = ConsumptionManagementClient(
        credential=DefaultAzureCredential(),
        subscription_id="xxx-xxx-xxx-xxx-xxxx",
    )

    response = client.reservation_transactions.list(
        billing_account_id="xxxxxxxxx",
    )
    for item in response:
        print(item)

# x-ms-original-file: specification/consumption/resource-manager/Microsoft.Consumption/stable/2021-10-01/examples/ReservationTransactionsListByEnrollmentNumber.json
if __name__ == "__main__":
    main()


Azure Cost Management
Azure Cost Management
A Microsoft offering that enables tracking of cloud usage and expenditures for Azure and other cloud providers.
3,578 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Kirill Starodubtsev 5 Reputation points
    2023-03-30T12:43:20.9166667+00:00

    Since last week, we are also seeing a lot of 429 rate-limiting errors with Azure Cost Management API.

    It would be helpful to get information about if the limits were recently changed.

    Currently, we trying to read "x-ms-ratelimit-microsoft.costmanagement-entity-retry-after" and then waiting for that amount of seconds.

    1 person found this answer helpful.

  2. Broeder, drs. N. den (Nico) DOMC 5 Reputation points
    2024-02-23T10:32:49.1066667+00:00

    I had the same issue in an Azure workbook. I could solved this issue by adding a header: X-Ms-Command-Name: CostAnalysis

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.