Serverless is cheap, but "pay-per-use" can become a nightmare if you don't track the usage. Today, I added a cost-tracking layer to my Financial Agent.
The Logic
Most developers ignore the usage object returned by LLM APIs. I decided to use it.
Python
def calculate_ai_cost(input_tokens, output_tokens):
# AWS Nova Micro Pricing (us-east-1)
cost_input = (input_tokens / 1000) * 0.00035
cost_output = (output_tokens / 1000) * 0.00140
return round(cost_input + cost_output, 7)
Integration
Inside my Lambda handler, after every bedrock.invoke_model() call, I extract the token counts and pass them to this calculator. The result is immediately logged to CloudWatch using Structured JSON Logging.
The Result
I can now query CloudWatch Logs Insights to see exactly how much money I burned today on AI inferences. It brings a level of transparency that is essential for scaling any SaaS product.

Top comments (0)