Skip to main content

Python SDK for the ModelCost API — track, govern, and optimize AI model spending.

Project description

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

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

modelcost_sdk-0.3.0.tar.gz (33.9 kB view details)

Uploaded Source

Built Distribution

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

modelcost_sdk-0.3.0-py3-none-any.whl (39.6 kB view details)

Uploaded Python 3

File details

Details for the file modelcost_sdk-0.3.0.tar.gz.

File metadata

  • Download URL: modelcost_sdk-0.3.0.tar.gz
  • Upload date:
  • Size: 33.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for modelcost_sdk-0.3.0.tar.gz
Algorithm Hash digest
SHA256 d80a73507606759807eaea96f307c5f1bba870814888b370b331486aac122f64
MD5 f76929deb5dfcfaf3561ac80f4a4f2b5
BLAKE2b-256 97db8c54f667a7ae2b58587acf111d51dd863b037c88a7338281fc2d8b564ac6

See more details on using hashes here.

File details

Details for the file modelcost_sdk-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: modelcost_sdk-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 39.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for modelcost_sdk-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4a182be5d6c32a3005a5ebdde29fee4dc9fd5be98444c3d23ca17423b25db32b
MD5 385975a1608b68be7a19036250df4db7
BLAKE2b-256 8c74e7ea4423d5244c830dadeb5240ca4cf48c8a40bee5870fa41dbbc4fff72d

See more details on using hashes here.

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