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.1.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.1-py3-none-any.whl (39.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: modelcost_sdk-0.3.1.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.1.tar.gz
Algorithm Hash digest
SHA256 70dc8d40de92150c2f94b5adc68c33c11b92b69b2a64ca599946de97ef7c4853
MD5 9b9748802fcead18e909626464e5c05b
BLAKE2b-256 43433eaa6335675522dfb4d2a72642603f150da26594f1e25c4d768327e0b146

See more details on using hashes here.

File details

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

File metadata

  • Download URL: modelcost_sdk-0.3.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e79a745fffb2aa849c0b97d5d4736b2e746ac964dddf174b0b589235fdc44519
MD5 5f65bab9645dfeb824d5636efd9410f4
BLAKE2b-256 90f26106b5caf81511eea3a37f1767f1c9dd7557ceac9e70581dded4925cae37

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