Created
January 7, 2025 18:49
-
-
Save demoray/1543ef97b109173e22d7c1605a39f729 to your computer and use it in GitHub Desktop.
Get Azure AI consumption pricing for token usage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from requests import get | |
import json | |
def main(): | |
api_url = ( | |
"https://prices.azure.com/api/retail/prices?api-version=2021-10-01-preview" | |
) | |
query = [ | |
"armRegionName eq 'eastus'", | |
"serviceName eq 'Cognitive Services'", | |
"serviceFamily eq 'AI + Machine Learning'", | |
"priceType eq 'Consumption'", | |
"contains(meterName, 'Tokens')", | |
"unitOfMeasure eq '1K'", | |
] | |
query = " and ".join(query) | |
data = {} | |
response = get(api_url, params={"$filter": query}) | |
while True: | |
response.raise_for_status() | |
raw = json.loads(response.text) | |
next_page = raw.get("NextPageLink") | |
for item in raw["Items"]: | |
data[item["meterName"]] = item["retailPrice"] | |
if not next_page: | |
break | |
response = get(next_page) | |
print(json.dumps(data, indent=4, sort_keys=True)) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment