I want to calculate cost of my chat thread, right now I can get token consumption details for input, output and cached tokens from response object. But whatever model I have used, I have to hard code prices for that somewhere in code or database.
How can I get pricing of OpenAI models dynamically via API so that whenever new model is added or if OpenAI is changing pricing for existing model, I do not need to worry about maintaining pricing data in my database or code.
OpenAI has not heard appeals for such programmatic data, or other model information such as context length or rate limits. Even though their own model pricing web page is serviced by such data.
Prices of a particular model ID have not changed in-place since 2022 - and then it was a price reduction. You won’t magically start using a new model - they should be checked for serviceability in your application first, besides discovering if the new price is within budget for production.
You can “get source” on this price calculator of mine, around line 600, for all the chat models that have vision, with the additional pricing fields you request (although there is more pricing information such as service tier rates to be had). It also is manually updated when OpenAI releases new model pricing…or doesn’t.
You can use the Docs MCP server to get the token costs for each model.
I would still have to confirm whether the tool is definitely using the official docs as its source, or whether there could be a delay after a pricing update. @_j is correct that model pricing has almost always been fixed after release for a long time.