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.2.0.tar.gz (30.8 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.2.0-py3-none-any.whl (37.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: modelcost_sdk-0.2.0.tar.gz
  • Upload date:
  • Size: 30.8 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.2.0.tar.gz
Algorithm Hash digest
SHA256 d1c10bfccaa264b362f8bf4e73584333d1789eb0d845b37a51e6858dbdcb77fc
MD5 5f29f0a226808bd90e9a70ffe7d13c08
BLAKE2b-256 fdb4c7973fb44f8538c52d8c0fd99cb685b1e9b748b0244af4f95f42a26d5198

See more details on using hashes here.

File details

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

File metadata

  • Download URL: modelcost_sdk-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 37.5 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.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a5915ac30e358120889d835c793ec61ce9d9e4ddc2903423e72e0594c7cace9b
MD5 b9884cb545f5a57fd6a38b1b4d998100
BLAKE2b-256 4b4425f1a65eae2c9e630a7db0ba243b9418278f66942b4d10df784389c2b267

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