Search code examples
python-3.xamazon-web-servicesamazon-comprehend

Calculate AWS Comprehend Sentiment cost


I'd like to programmatically estimate the cost to call the AWS Comprehend Sentiment API. I searched SO and the AWS calculators but couldn't find a way. Also, I'm sure the costs for the amount of text I'll be sending will be small but I really want to know.

Based on the pricing info here I wrote the code below. Is it correct?

text = ["What a horrible rainy day today", 
        "What a great day today", 
        "This is a neutral statement"]

numChars = sum(len(i) for i in text)

#Sentiment is measured in units of 100 characters, with a 3 unit (300 character) minimum charge per request.
numUnits = int(math.ceil(numChars / 100))

# Up to 10M units
if numUnits < 10000000:
    pricePerunit = 0.0001
    sentimentCost = numUnits * pricePerunit

# From 10M-50M units
elif numUnits >= 10000000 and numUnits <= 50000000:
    pricePerunit = 0.0001
    sentimentCost = 9999999 * pricePerunit

    pricePerunit = 0.00005
    sentimentCost = sentimentCost + ((numUnits - 10000000) * pricePerunit)

# Over 50M units.
elif numUnits > 50000000:
    pricePerunit = 0.0001
    sentimentCost = 9999999 * pricePerunit

    pricePerunit = 0.00005
    sentimentCost = sentimentCost + (40000000 * pricePerunit)

    pricePerunit = 0.000025
    sentimentCost = sentimentCost + ((numUnits - 49999999) * pricePerunit)

print("\nEstimated $ charges to call AWS Comprehend Sentiment are: %0.5f\n" % sentimentCost)

Solution

  • No, this calculation is not correct. Specifically:

    • you need to round up for units so use math.ceil(numChars / 100)
    • the cost/unit is different for the first 10M, the next 40M, and anything beyond 50M, and you have mistakenly assumed that all units are charged at the marginal rate. Your code will calculate the cost of 10M+1 units as (10M+1) * 0.00005 when it should be 10M*0.0001 + 1*0.00005
    • also, your code will crash with exactly 10000000 or 50000000 units