Skip to main content

ModelCost Python SDK

Python SDK for the ModelCost API. Track, govern, and optimize your AI model spending in real time.

Installation

pip install modelcost

For development:

pip install modelcost[dev]

Quick Start

import modelcost

# Initialize the SDK
modelcost.init(api_key="mc_your_api_key", org_id="org_123")

# Wrap your OpenAI client for automatic tracking
import openai
client = openai.OpenAI()
wrapped = modelcost.wrap(client)

# All calls are now tracked automatically
response = wrapped.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Hello!"}],
)

# Or track costs manually
modelcost.track_cost(
    provider="openai",
    model="gpt-4o",
    input_tokens=150,
    output_tokens=50,
    feature="chatbot",
)

# Check budget before expensive operations
budget = modelcost.check_budget(feature="chatbot", estimated_cost=0.50)
if not budget.allowed:
    print(f"Budget exceeded: {budget.reason}")

# Scan text for PII before sending to models
result = modelcost.scan_pii("Contact me at test@example.com")
if result.detected:
    print(f"PII found: {result.entities}")

# Get current usage summary
usage = modelcost.get_usage()

# Flush any buffered events and shut down
modelcost.shutdown()

Configuration

You can configure the SDK via environment variables:

Variable Description
MODELCOST_API_KEY Your API key (must start with mc_)
MODELCOST_ORG_ID Your organization ID
MODELCOST_ENV Environment name (default: production)
MODELCOST_BASE_URL API base URL (default: https://api.modelcost.ai)

License

MIT

Metadata

Release files for modelcost-sdk 0.3.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for modelcost-sdk 0.3.3
File Size Uploaded
modelcost_sdk-0.3.3.tar.gz 33.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for modelcost-sdk 0.3.3
File Interpreter ABI Platform
modelcost_sdk-0.3.3-py3-none-any.whl Python 3 none any Details

Total release size: 73.5 kB

Release files / modelcost_sdk-0.3.3.tar.gz

Download URL modelcost_sdk-0.3.3.tar.gz
Size 33.9 kB
Tags Source
SHA-256 checksum
How to use checksums
77e82bf0eb0dc26ed17bcb71a1810f4b242a04b2444e8f0b49037764ba1660cd
BLAKE2b-256 checksum
How to use checksums
53fc6906cd5b8ca02c170bb9de314548113ca5805373e97c2f8f6cfdfb0632fa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.13

Release files / modelcost_sdk-0.3.3-py3-none-any.whl

Download URL modelcost_sdk-0.3.3-py3-none-any.whl
Size 39.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1e4d7bc5535034e7999fa62d47441649f5bcca72d3099dfc7657b9f513eb59e5
BLAKE2b-256 checksum
How to use checksums
32ef265a9e198867b2415f83adf9008382b15cb8f6baa0e498ca2c9e8624ebd2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.13

Release history Release notifications | RSS feed

This release

0.3.3 This release

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page