Python client for ModelPricing.ai cost estimates and tracking
Project description
modelpricing-ai
Python client for the ModelPricing.ai API — estimate LLM usage costs and track spending with a single call.
Installation
pip install modelpricing-ai
For async support (requires aiohttp):
pip install modelpricing-ai[async]
Quick Start
from modelpricing_ai import ModelPricingClient
with ModelPricingClient(api_key="YOUR_API_KEY") as client:
estimate = client.estimate(
model="claude-haiku-4-5-20251001",
tokens_in=1000,
tokens_out=500,
trace_id={"requestId": "abc-123"},
)
print(f"Cost: ${estimate.total:.6f}")
From a provider SDK response
If you already have a response object from the Anthropic or OpenAI SDK, pass it directly — the client pulls the model name and token counts for you. Works with Anthropic Messages, OpenAI Chat Completions, and OpenAI Responses API (Pydantic objects or plain dicts).
from anthropic import Anthropic
from modelpricing_ai import ModelPricingClient
anthropic = Anthropic()
response = anthropic.messages.create(
model="claude-haiku-4-5-20251001",
max_tokens=1024,
messages=[{"role": "user", "content": "hello"}],
)
with ModelPricingClient(api_key="YOUR_API_KEY") as client:
estimate = client.estimate_from_response(response)
print(f"Cost: ${estimate.total:.6f}")
from openai import OpenAI
from modelpricing_ai import ModelPricingClient
openai = OpenAI()
response = openai.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "hello"}],
)
with ModelPricingClient(api_key="YOUR_API_KEY") as client:
estimate = client.estimate_from_response(response)
The async client exposes the same method: await client.estimate_from_response(response).
Cache-read / cache-creation tokens are not yet priced separately — they're rolled into the input-token total. Raise an issue if you need finer-grained cache pricing.
Async Usage
Install the async extra, then use AsyncModelPricingClient as an async context manager:
import asyncio
from modelpricing_ai import AsyncModelPricingClient
async def main():
async with AsyncModelPricingClient(api_key="YOUR_API_KEY") as client:
estimate = await client.estimate(
model="claude-haiku-4-5-20251001",
tokens_in=1000,
tokens_out=500,
trace_id={"requestId": "abc-123"},
)
print(f"Cost: ${estimate.total:.6f}")
asyncio.run(main())
Response Structure
All estimate methods (estimate, estimate_from_response, and their async counterparts) 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) |
Parameters are resolved in order: constructor argument > environment variable > default.
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. All of them inherit from ModelPricingError and carry a status_code attribute:
| Exception | HTTP Status | When |
|---|---|---|
Unauthorized |
401 | Invalid or missing API key |
NotFound |
404 | Unknown endpoint |
ValidationError |
422 | Invalid model name or metrics |
ServerError |
5xx | Server-side failures (retried automatically) |
ModelPricingError |
other | Base class — raised on unexpected status codes or malformed 200s |
from modelpricing_ai.errors import Unauthorized, ValidationError, ServerError
try:
estimate = client.estimate(model="claude-haiku-4-5-20251001", 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 (0.1 s initial, 2 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
Credits
Made with ❤️ by Humanspeak
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.4.4.tar.gz.
File metadata
- Download URL: modelpricing_ai-2026.4.4.tar.gz
- Upload date:
- Size: 10.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8c9c2007007e6d588e18f416544a0dce9cb10bb7026909acfab3f4c8a9f44990
|
|
| MD5 |
d1d186f0b00b12d113355d59b1c6c954
|
|
| BLAKE2b-256 |
473181c400e00993a5994a6e34d7bc0fd30b14c0509d86911b15daa132ec1be1
|
Provenance
The following attestation bundles were made for modelpricing_ai-2026.4.4.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.4.4.tar.gz -
Subject digest:
8c9c2007007e6d588e18f416544a0dce9cb10bb7026909acfab3f4c8a9f44990 - Sigstore transparency entry: 1395710905
- Sigstore integration time:
-
Permalink:
humanspeak/modelpricing-ai@59b911b4168f1843bb047de6406048736745f6d1 -
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@59b911b4168f1843bb047de6406048736745f6d1 -
Trigger Event:
push
-
Statement type:
File details
Details for the file modelpricing_ai-2026.4.4-py3-none-any.whl.
File metadata
- Download URL: modelpricing_ai-2026.4.4-py3-none-any.whl
- Upload date:
- Size: 15.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f80287e3fb1b61f59bf77c94ef3028801fb89c4a7068ed5b270219a4073a5c27
|
|
| MD5 |
34cf6da933fe642db3af6379f6baae26
|
|
| BLAKE2b-256 |
6d3f65c8caac5a80b8e0969a6ac9101b43eb63657f198077f96285b87750767f
|
Provenance
The following attestation bundles were made for modelpricing_ai-2026.4.4-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.4.4-py3-none-any.whl -
Subject digest:
f80287e3fb1b61f59bf77c94ef3028801fb89c4a7068ed5b270219a4073a5c27 - Sigstore transparency entry: 1395710914
- Sigstore integration time:
-
Permalink:
humanspeak/modelpricing-ai@59b911b4168f1843bb047de6406048736745f6d1 -
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@59b911b4168f1843bb047de6406048736745f6d1 -
Trigger Event:
push
-
Statement type: