Skip to main content

A lightweight library to fetch and search AI model pricing data

Project description

Valuator

A lightweight Python library to fetch AI model pricing data and perform searches to retrieve input and output costs per token for matching models. Supports currency conversion for prices (e.g., INR, EUR, GBP, JPY), defaulting to USD.

PyPI Downloads

Installation

pip install valuator

Usage

import asyncio
from valuator import Valuator

async def main():
    valuator = Valuator()
    try:

        await valuator.initialize()

        print(valuator.get_model_costs("claude"))
        # Get prices in INR
        print(valuator.get_model_costs("claude", currency="INR"))
        # Get prices in EUR
        print(valuator.get_model_costs("claude", currency="EUR"))

        await valuator.initialize(force_refresh=False)
        print(valuator.get_model_costs("gpt.*"))
    finally:
        await valuator.close()

asyncio.run(main())

Example

import asyncio
import json
from valuator import Valuator

async def main():
    valuator = Valuator()
    try:
        await valuator.initialize()
        # Get prices in USD (default)
        print("Prices in USD:")
        data_usd = valuator.get_model_costs("claude")
        if data_usd:
            model_names = list(data_usd.keys())
            
            # First model
            if len(model_names) >= 1:
                first_model = model_names[0]
                first_model_data = data_usd[first_model]
                print("First model:")
                print(f"  Model name: {first_model}")
                print(f"  Input cost: {first_model_data['input_cost_per_token']}")
                print(f"  Output cost: {first_model_data['output_cost_per_token']}")
            
            # Second model (if available)
            if len(model_names) >= 2:
                second_model = model_names[1]
                second_model_data = data_usd[second_model]
                print("\nSecond model:")
                print(f"  Model name: {second_model}")
                print(f"  Input cost: {second_model_data['input_cost_per_token']}")
                print(f"  Output cost: {second_model_data['output_cost_per_token']}")
            else:
                print("\nNo second model found.")
            
            print("\nFull result (USD):")
            print(json.dumps(data_usd, indent=2))

        # Get prices in INR
        print("\nPrices in INR:")
        data_inr = valuator.get_model_costs("claude", currency="INR")
        if data_inr:
            model_names = list(data_inr.keys())
            
            # First model
            if len(model_names) >= 1:
                first_model = model_names[0]
                first_model_data = data_inr[first_model]
                print("First model:")
                print(f"  Model name: {first_model}")
                print(f"  Input cost: {first_model_data['input_cost_per_token']}")
                print(f"  Output cost: {first_model_data['output_cost_per_token']}")
            
            # Second model (if available)
            if len(model_names) >= 2:
                second_model = model_names[1]
                second_model_data = data_inr[second_model]
                print("\nSecond model:")
                print(f"  Model name: {second_model}")
                print(f"  Input cost: {second_model_data['input_cost_per_token']}")
                print(f"  Output cost: {second_model_data['output_cost_per_token']}")
            else:
                print("\nNo second model found.")
            
            print("\nFull result (INR):")
            print(json.dumps(data_inr, indent=2))

        # Get prices in EUR
        print("\nPrices in EUR:")
        data_eur = valuator.get_model_costs("claude"n, currency="EUR")
        if data_eur:
            model_names = list(data_eur.keys())
            
            if len(model_names) >= 1:
                first_model = model_names[0]
                first_model_data = data_eur[first_model]
                print("First model:")
                print(f"  Model name: {first_model}")
                print(f"  Input cost: {first_model_data['input_cost_per_token']}")
                print(f"  Output cost: {first_model_data['output_cost_per_token']}")
            
            if len(model_names) >= 2:
                second_model = model_names[1]
                second_model_data = data_eur[second_model]
                print("\nSecond model:")
                print(f"  Model name: {second_model}")
                print(f"  Input cost: {second_model_data['input_cost_per_token']}")
                print(f"  Output cost: {second_model_data['output_cost_per_token']}")
            else:
                print("\nNo second model found.")
            
            print("\nFull result (EUR):")
            print(json.dumps(data_eur, indent=2))
        else:
            print("No models found.")
    finally:
        await valuator.close()

asyncio.run(main())

Example Output

Prices in USD:
First model:
  Model name: us.anthropic.claude-3-5-sonnet-20240620-v1:0
  Input cost: 3e-06
  Output cost: 1.5e-05

Second model:
  Model name: us.anthropic.claude-3-sonnet-20240101-v1:0
  Input cost: 4e-06
  Output cost: 2e-05

Full result (USD):
{
  "us.anthropic.claude-3-5-sonnet-20240620-v1:0": {
    "input_cost_per_token": 3e-06,
    "output_cost_per_token": 1.5e-05
  },
  "us.anthropic.claude-3-sonnet-20240101-v1:0": {
    "input_cost_per_token": 4e-06,
    "output_cost_per_token": 2e-05
  }
}

Prices in INR:
First model:
  Model name: us.anthropic.claude-3-5-sonnet-20240620-v1:0
  Input cost: 0.0002629968906
  Output cost: 0.001314984453

Second model:
  Model name: us.anthropic.claude-3-sonnet-20240101-v1:0
  Input cost: 0.0003506625208
  Output cost: 0.00175332604

Full result (INR):
{
  "us.anthropic.claude-3-5-sonnet-20240620-v1:0": {
    "input_cost_per_token": 0.0002629968906,
    "output_cost_per_token": 0.001314984453
  },
  "us.anthropic.claude-3-sonnet-20240101-v1:0": {
    "input_cost_per_token": 0.0003506625208,
    "output_cost_per_token": 0.00175332604
  }
}

Prices in EUR:
First model:
  Model name: us.anthropic.claude-3-5-sonnet-20240620-v1:0
  Input cost: 2.57636796e-06
  Output cost: 1.28818398e-05

Second model:
  Model name: us.anthropic.claude-3-sonnet-20240101-v1:0
  Input cost: 3.43515728e-06
  Output cost: 1.71757864e-05

Full result (EUR):
{
  "us.anthropic.claude-3-5-sonnet-20240620-v1:0": {
    "input_cost_per_token": 2.57636796e-06,
    "output_cost_per_token": 1.28818398e-05
  },
  "us.anthropic.claude-3-sonnet-20240101-v1:0": {
    "input_cost_per_token": 3.43515728e-06,
    "output_cost_per_token": 1.71757864e-05
  }
}

Features

  • Fetches AI model pricing data from a specified URL or local cache.
  • Supports currency conversion for prices to any currency listed in the currency API (e.g., INR, EUR, GBP, JPY), defaulting to USD.
  • Automatically checks if remote JSON files (model prices and currency rates) have changed using ETag headers.
  • Defaults to fetching the latest data (force_refresh=True) to ensure up-to-date model prices and exchange rates.
  • Performs searches on model names for flexible matching, returning up to 5 matches.
  • Returns input_cost_per_token and output_cost_per_token for matched models in a dictionary format.
  • Optimized for low memory usage with efficient data structures (sets, cached regex).
  • Asynchronous HTTP requests for fast data retrieval.

Requirements

  • Python 3.8+
  • aiohttp>=3.8.0

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

valuator-1.1.3.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

valuator-1.1.3-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file valuator-1.1.3.tar.gz.

File metadata

  • Download URL: valuator-1.1.3.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for valuator-1.1.3.tar.gz
Algorithm Hash digest
SHA256 93709486f8805fb06b0d6f69d68b40fe6418880ad7406df317c9a5280d6968f6
MD5 991d5a138b058039f2c57d23f79311c5
BLAKE2b-256 e74f81761595b9ef92658301e1b9fecb2e268d6ac640feac632e5f791f56e18e

See more details on using hashes here.

File details

Details for the file valuator-1.1.3-py3-none-any.whl.

File metadata

  • Download URL: valuator-1.1.3-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for valuator-1.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 6be2198cfe080615f6a4fe4323b8eb2162cc6adbd381820f4fe0f327b01ca134
MD5 f388ee5d6908ca67516ee56b7e6c586e
BLAKE2b-256 550847130a8224476d35e70194ec9018e4d8fea03592017827a3367aa5a4e245

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page