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.

Installation

pip install valuator

Usage

import asyncio
from valuator import Valuator

async def main():
    valuator = Valuator()
    try:
        # Initialize with default force_refresh=True to fetch latest data
        await valuator.initialize()
        print(valuator.get_model_costs("claude.*haiku"))
        print(valuator.get_model_costs("gpt-4"))
        # Use force_refresh=False to prefer cache if unchanged
        await valuator.initialize(force_refresh=False)
        print(valuator.get_model_costs("gpt.*"))
    finally:
        await valuator.close()

asyncio.run(main())

Example

import asyncio
from valuator import Valuator

async def main():
    valuator = Valuator()
    try:
        await valuator.initialize()
        data = valuator.get_model_costs("us.anthropic.claude-3-5-sonnet-20240620-v1:0")
        if data:
            # Extract first matched model and its costs
            model_names = list(data.keys())
            selected_model = model_names[0]
            selected_model_costs = data[selected_model]

            print("Matched Model:")
            print(f"  Name: {selected_model}")
            print(f"  Input cost per token: {selected_model_costs['input_cost_per_token']}")
            print(f"  Output cost per token: {selected_model_costs['output_cost_per_token']}")
        else:
            print("No models found.")
    finally:
        await valuator.close()

asyncio.run(main())

Output:

Matched Model:
  Name: us.anthropic.claude-3-5-sonnet-20240620-v1:0
  Input cost per token: 3e-06
  Output cost per token: 1.5e-05

Features

  • Fetches model pricing data.
  • Automatically checks if the remote JSON has changed using ETag headers.
  • Defaults to fetching the latest data (force_refresh=True) to ensure up-to-date model prices.
  • Performs regex-based searches on model names for flexible matching.
  • Returns only input_cost_per_token and output_cost_per_token for matched models.
  • 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.0.0.tar.gz (4.1 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.0.0-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for valuator-1.0.0.tar.gz
Algorithm Hash digest
SHA256 b042f5a549c2416eddf86ddf1f7da5608d76f0d1c242452409046babd6081866
MD5 58e982f88f0bac25c02be2acf7d1aa0c
BLAKE2b-256 ef707d966588ddd21d85073af17078118de65020cd2e2bca211949b38304bdd0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: valuator-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 4.4 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.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4fc6dde56cff54c2d4ca0c8e158b3c783af4e3c142c6ed49c5214719a3fe9210
MD5 e1c0cde021a26f2a27712843c131ffb2
BLAKE2b-256 1f749862bb6b9e5630406c425a5176bafb4dea5c33e4a81c3006533850f5bcfb

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