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.1.0.tar.gz (4.8 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.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: valuator-1.1.0.tar.gz
  • Upload date:
  • Size: 4.8 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.0.tar.gz
Algorithm Hash digest
SHA256 af0ec9f2ad40a6507a25c7191edd0c95252aa8f3e8a0bbeca087916eeff29bf8
MD5 1e47b8568823175e4c2ac264ae390373
BLAKE2b-256 f29e480abe45de5d6de1068af8087b8b58401dc155e8158df03d11ceba255377

See more details on using hashes here.

File details

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

File metadata

  • Download URL: valuator-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.0 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a714cf232907bf451c7366464d59762fce65a9319e357e4755ce39eb223282bc
MD5 ebb329348f2daebaaf58012af3358b11
BLAKE2b-256 5b62415dbca1bea6a1c2481f326f41f681735b467e6ed805da5cdee8ec95650f

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