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

Uploaded Python 3

File details

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

File metadata

  • Download URL: modelcost_sdk-0.3.3.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.3.tar.gz
Algorithm Hash digest
SHA256 77e82bf0eb0dc26ed17bcb71a1810f4b242a04b2444e8f0b49037764ba1660cd
MD5 c4b6bae71248f4fafa316ada523ca1b4
BLAKE2b-256 53fc6906cd5b8ca02c170bb9de314548113ca5805373e97c2f8f6cfdfb0632fa

See more details on using hashes here.

File details

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

File metadata

  • Download URL: modelcost_sdk-0.3.3-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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 1e4d7bc5535034e7999fa62d47441649f5bcca72d3099dfc7657b9f513eb59e5
MD5 94129cdd1a48419fb8b60ac373c41863
BLAKE2b-256 32ef265a9e198867b2415f83adf9008382b15cb8f6baa0e498ca2c9e8624ebd2

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