Skip to main content

Python client for ModelPricing.ai cost estimates

Project description

modelpricing-ai

Python client for the ModelPricing.ai API.

Installation

pip install modelpricing-ai

Usage (sync, typed)

from modelpricing_ai import ModelPricingClient

client = ModelPricingClient(api_key="YOUR_API_KEY")

estimate = client.estimate(
    model="gpt-4o-mini",
    tokens_in=125000,
    tokens_out=42000,
    trace_id={"requestId": "abc-123"},
)
print(estimate.total)  # total USD cost
  • Base URL: https://api.modelpricing.ai
  • Auth: Authorization: Bearer <API_KEY>
  • Endpoint: POST /v1/estimate
  • Request body:
{
    "metrics": { "tokensIn": 1000, "tokensOut": 2000 },
    "model": "<model-name>",
    "traceId": { "...": "..." }
}

Error handling

  • 401 Unauthorized: invalid or missing API key
  • 422 Unprocessable Entity: validation error (e.g., invalid model or metrics)
  • Other 4xx/5xx: raised as a generic server error

Retry behavior

The client automatically retries on transient errors with exponential backoff:

  • Retries: 5xx server errors and network/connection errors
  • No retry: 4xx client errors (401, 404, 422)
  • Default: 3 attempts with exponential backoff (1s, 2s, 4s, up to 10s max)

Configure retry behavior:

# Customize max retries (default is 3)
client = ModelPricingClient(api_key="YOUR_API_KEY", max_retries=5)

# Disable retries
client = ModelPricingClient(api_key="YOUR_API_KEY", max_retries=1)

This handles transient infrastructure issues like Cloudflare errors, DNS failures, and temporary service disruptions.

License

MIT

Async usage (typed)

import asyncio
from modelpricing_ai import AsyncModelPricingClient

async def main():
    client = AsyncModelPricingClient(api_key="YOUR_API_KEY")
    estimate = await client.estimate(
        model="gpt-4o-mini",
        tokens_in=125000,
        tokens_out=42000,
        trace_id={"requestId": "abc-123"},
    )
    print(estimate.total)  # total USD cost

asyncio.run(main())

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

modelpricing_ai-2026.2.3.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

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

modelpricing_ai-2026.2.3-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

Details for the file modelpricing_ai-2026.2.3.tar.gz.

File metadata

  • Download URL: modelpricing_ai-2026.2.3.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for modelpricing_ai-2026.2.3.tar.gz
Algorithm Hash digest
SHA256 0136cbf5cab1534d887896cebd887897cff08fefb033b01816ed8efb6d1fb156
MD5 e4c32dfac48942a345efd2cda8627fd0
BLAKE2b-256 4cd5f3e23f07016f88dd247126bf0b8257d8e9a0c1be70ed5dc61f56be63220e

See more details on using hashes here.

Provenance

The following attestation bundles were made for modelpricing_ai-2026.2.3.tar.gz:

Publisher: publish-python.yml on humanspeak/modelpricing-ai

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file modelpricing_ai-2026.2.3-py3-none-any.whl.

File metadata

File hashes

Hashes for modelpricing_ai-2026.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 13b3ce2d5ad41fd1b82d2da7483bfb21f5b53cbd79b3e21ee94b832131e4ec96
MD5 128997281c87a175c8b70d1a661d87b3
BLAKE2b-256 f87c50bc7ab156cb2630ecc6c09c20dfd046ff59a9587a961eef9b813af0228e

See more details on using hashes here.

Provenance

The following attestation bundles were made for modelpricing_ai-2026.2.3-py3-none-any.whl:

Publisher: publish-python.yml on humanspeak/modelpricing-ai

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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