Marcio Cunha

How to Dynamically List Input and Output Token Prices via API

Learn how to query the available models API to extract input and output token costs in real-time, preventing unexpected artificial intelligence budget overruns.

Marcio Cunha4 min
Also available in:EspañolPortuguês
Summary
  • Querying the models API in real-time eliminates reliance on static and outdated pricing spreadsheets.
  • The returned data structure clearly separates the cost per million tokens for input and output operations.
  • Automating this extraction protects software applications against sudden tariff adjustments by providers.
  • Local caching layers reduce unnecessary calls and optimize the overall performance of the billing system.
  • Handling network failures with fallback strategies ensures cost calculation continues even during instability.

The hidden cost challenge in artificial intelligence applications

When building software that utilizes language models, financial control is often the Achilles' heel of the operation. Artificial intelligence providers frequently update tariffs, release new versions, and modify cost structures with dizzying frequency. In practice, this means that a pricing table hardcoded into your source code today will be incorrect tomorrow, resulting in surprise invoices at the end of the month. To solve this structural problem, we must stop relying on static values and instead query the provider's infrastructure dynamically.

The good news is that major artificial intelligence platforms offer specific endpoints to list available models and their operational metadata. Instead of guessing how much it costs to process text, the software can ask the server directly for the current price tag at that exact second. This approach transforms cost management from a manual, reactive chore into an automated, resilient process that remains completely transparent to the engineering team.

Understanding the available models API

Listing models is a standard endpoint across most generative AI ecosystems. When we fire a GET HTTP request to this address, we receive a bulky JSON object containing a list of all the intelligences our API key has permission to access. Each item in this list represents a specific model and carries vital properties, such as the unique identifier, friendly name, maximum context window size, and crucially, pricing data.

In practice, the returned payload usually nests costs inside an economic metadata block. The developer's challenge is not just accessing this data, but normalizing the information, since different providers adopt distinct naming conventions for the same thing. While one service calls the processing price input_cost_per_token, another might use prompt_token_price. Carefully mapping these differences is the first step toward building a robust expenditure auditing system.

Step-by-step to fetch and extract prices programmatically

To fetch and process this data in an automated way, we can use any modern programming language. The basic procedure requires authentication via a secure token, sending the request, and reading the response body to isolate the input and output values. Below is a practical example using Python and the standard requests library to interact with the listing endpoint.

import requests

def get_model_prices(api_key):
    url = 'https://api.example.com/v1/models'
    headers = {'Authorization': f'Bearer {api_key}'}
    
    response = requests.get(url, headers=headers)
    if response.status_code != 200:
        raise Exception('Failed to fetch models API')
        
    data = response.json()
    price_table = {}
    
    for model in data.get('data', []):
        name = model.get('id')
        input_cost = model.get('pricing', {}).get('input', 0.0)
        output_cost = model.get('pricing', {}).get('output', 0.0)
        
        price_table[name] = {
            'input': input_cost,
            'output': output_cost
        }
        
    return price_table

The code above demonstrates how to transform a raw and complex response into a clean, organized dictionary. Each dictionary key represents the name of the artificial intelligence model, separately storing the charged value for processing the text we send and the text we receive back. This separation is fundamental because providers usually charge significantly higher rates to generate the response than to read the provided context.

Architecture decisions: caching, resilience, and fallbacks

Making an HTTP request to check prices every time a user sends a message would be a massive waste of resources and introduce unnecessary latency bottlenecks. Model prices change on a weekly or monthly basis, not every millisecond. In practice, this means we must implement an in-memory or high-speed database caching layer, such as Redis, storing the pricing table for a few hours before executing a fresh scan against the official API.

Another critical design point is operational resilience. If the artificial intelligence provider's service goes down or experiences extreme latency, the main application cannot simply stop working due to a missing updated price table. The correct engineering strategy consists of maintaining a local fallback file with the last known values and registering a silent alert for the technical team. Thus, the system continues operating with approximate financial safety while external infrastructure recovers.

Final considerations on AI financial monitoring

Automating token price listing eliminates data obsolescence in modern applications and safeguards businesses against unwanted billing surprises. By treating costs as dynamic variables discovered via code, engineering gains the autonomy to switch between different providers and model versions without rewriting complex business rules. The initial investment in structuring this retrieval and caching pays off massively during the first major market fluctuation or tariff table adjustment.