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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file modelpricing_ai-0.0.1a1.tar.gz.
File metadata
- Download URL: modelpricing_ai-0.0.1a1.tar.gz
- Upload date:
- Size: 6.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bd4287d844233ede1732d543620e2b18e0b36500608e79fd57bf45617c084491
|
|
| MD5 |
4c1a8d6de3c0c828cd1184de73a5f0ed
|
|
| BLAKE2b-256 |
0fc5cb8b006dd15ed2c509d4a71f463c3d30135cb51b16085d5849234b23f161
|
File details
Details for the file modelpricing_ai-0.0.1a1-py3-none-any.whl.
File metadata
- Download URL: modelpricing_ai-0.0.1a1-py3-none-any.whl
- Upload date:
- Size: 10.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e78a89d13ce1c49666c193f797069019af8c9e55689c82547556d006edfe9b3b
|
|
| MD5 |
0af4cfbadfbcc4561d9fcf0a0c8e512d
|
|
| BLAKE2b-256 |
8d28d966a4f33668b6bbd22134cf37134ab52bffaeb42800ef65ce3dc79aa31d
|