Python client for ModelPricing.ai cost estimates
Project description
modelpricing-ai
Python client for the ModelPricing.ai API — estimate LLM usage costs with a single call.
Installation
pip install modelpricing-ai
Quick Start
from modelpricing_ai import ModelPricingClient
client = ModelPricingClient(api_key="YOUR_API_KEY")
estimate = client.estimate(
model="gpt-4o-mini",
tokens_in=1000,
tokens_out=500,
trace_id={"requestId": "abc-123"},
)
print(f"Cost: ${estimate.total:.6f}")
Async Usage
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=1000,
tokens_out=500,
trace_id={"requestId": "abc-123"},
)
print(f"Cost: ${estimate.total:.6f}")
asyncio.run(main())
Response Structure
Both estimate() and await estimate() return an EstimateResponse object:
estimate.total # float — total USD cost
estimate.model # str — canonical model name
estimate.traceId # dict | None — your pass-through trace ID
estimate.breakdown # EstimateBreakdownGroup
.input # EstimateBreakdown
.unit # str — e.g. "token"
.branch # str — pricing tier that matched
.qty # int — number of input tokens
.rate # float — per-unit rate
.subtotal # float — input cost
.output # EstimateBreakdown (same fields for output tokens)
Configuration
| Parameter | Default | Description |
|---|---|---|
api_key |
required | Your ModelPricing.ai API key (also reads MODELPRICING_API_KEY env var) |
base_url |
"https://api.modelpricing.ai" |
API base URL (also reads MODELPRICING_BASE_URL env var) |
timeout |
30.0 |
Request timeout in seconds |
max_retries |
3 |
Maximum retry attempts for transient errors |
session |
None |
Optional requests.Session (sync) or aiohttp.ClientSession (async) |
client = ModelPricingClient(
api_key="YOUR_API_KEY",
base_url="https://api.modelpricing.ai",
timeout=30.0,
max_retries=3,
)
Error Handling
The client raises typed exceptions for different failure modes:
| Exception | HTTP Status | When |
|---|---|---|
Unauthorized |
401 | Invalid or missing API key |
ValidationError |
422 | Invalid model name or metrics |
NotFound |
404 | Unknown endpoint |
ServerError |
5xx | Server-side failures |
All exceptions inherit from ModelPricingError and include a status_code attribute.
from modelpricing_ai.errors import Unauthorized, ValidationError, ServerError
try:
estimate = client.estimate(model="gpt-4o-mini", tokens_in=1000, tokens_out=500)
except Unauthorized:
print("Check your API key")
except ValidationError as e:
print(f"Bad request: {e}")
except ServerError:
print("Server error — will be retried automatically")
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 retries with exponential backoff (1 s, 2 s, 4 s up to 10 s max)
# Increase retries for unreliable networks
client = ModelPricingClient(api_key="YOUR_API_KEY", max_retries=5)
# Disable retries (no retry attempts)
client = ModelPricingClient(api_key="YOUR_API_KEY", max_retries=0)
License
MIT
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-2026.2.6.tar.gz.
File metadata
- Download URL: modelpricing_ai-2026.2.6.tar.gz
- Upload date:
- Size: 7.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
65a38ffb306bf945fbf0a0b8c3e62e088640cf2b41e431c4d3a3273702607ce0
|
|
| MD5 |
3b0944aa19cd319bf9e88d11c51cc32f
|
|
| BLAKE2b-256 |
640744b0fdfb3afa8182b4f2a95c0cdab79dd203b099a566fd45513466031f4e
|
Provenance
The following attestation bundles were made for modelpricing_ai-2026.2.6.tar.gz:
Publisher:
publish-python.yml on humanspeak/modelpricing-ai
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
modelpricing_ai-2026.2.6.tar.gz -
Subject digest:
65a38ffb306bf945fbf0a0b8c3e62e088640cf2b41e431c4d3a3273702607ce0 - Sigstore transparency entry: 946016508
- Sigstore integration time:
-
Permalink:
humanspeak/modelpricing-ai@d22b2e4c050cdc4e59f39c81976db73dc1f6d00e -
Branch / Tag:
refs/heads/main - Owner: https://github.com/humanspeak
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-python.yml@d22b2e4c050cdc4e59f39c81976db73dc1f6d00e -
Trigger Event:
push
-
Statement type:
File details
Details for the file modelpricing_ai-2026.2.6-py3-none-any.whl.
File metadata
- Download URL: modelpricing_ai-2026.2.6-py3-none-any.whl
- Upload date:
- Size: 11.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
59807739474d11c1e341250501f77908e480c8ef1998ad48f2a7b4e1a10c0516
|
|
| MD5 |
5ad0056d33bc1bcb7a1518b7efe1359c
|
|
| BLAKE2b-256 |
951b70b496a1f2113ea83b36352ca16131652c4f2ed74b3adaf8a23c93fe1070
|
Provenance
The following attestation bundles were made for modelpricing_ai-2026.2.6-py3-none-any.whl:
Publisher:
publish-python.yml on humanspeak/modelpricing-ai
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
modelpricing_ai-2026.2.6-py3-none-any.whl -
Subject digest:
59807739474d11c1e341250501f77908e480c8ef1998ad48f2a7b4e1a10c0516 - Sigstore transparency entry: 946016565
- Sigstore integration time:
-
Permalink:
humanspeak/modelpricing-ai@d22b2e4c050cdc4e59f39c81976db73dc1f6d00e -
Branch / Tag:
refs/heads/main - Owner: https://github.com/humanspeak
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-python.yml@d22b2e4c050cdc4e59f39c81976db73dc1f6d00e -
Trigger Event:
push
-
Statement type: