Anthropic Cost Tracking: Build a Claude Usage Logger in Python
Anthropic Cost Tracking: Build a Claude Usage Logger in Python Anthropic cost tracking is a small wrapper around your Claude API calls that records, for every single request, how many fresh input tokens you sent, how many tokens you paid to write into the prompt cache, how many you read back from it, how many output tokens the model produced, and what that request cost under the rates you configure. Put it between your code and the Anthropic client, log one line per call to a file, and the next time a number on the Claude Console looks wrong, you can point at the exact feature, user, or prompt that produced it. If you already built the OpenAI version of this wrapper , this is its Claude twin — the same sixty-line shape, but the usage object it reads is different in exactly the way that makes Claude bills confusing. That difference is the whole reason this tutorial exists. OpenAI reports one prompt-token count and tells you how many of those tokens were cached. Claude reports three se...