Skip to main content

Lightweight API cost tracker for research labs

Project description

Hong Lab AI Cost Tracker

A lightweight Python SDK that transparently tracks LLM API costs. Wrap your existing client with tracker.wrap() — costs are logged automatically.

Supports OpenAI, Google Gemini, Anthropic, and third-party proxies (e.g. apiyihe.org).

Installation

pip install hong-lab-ai-cost

# With specific provider support
pip install "hong-lab-ai-cost[openai]"
pip install "hong-lab-ai-cost[all]"       # OpenAI + Gemini + Anthropic

Usage

OpenAI

from openai import OpenAI
from hong_lab_ai_cost import CostTracker

tracker = CostTracker(project="MyProject", user="kyle", email="kyle@aucklanduni.ac.nz")
client = tracker.wrap(OpenAI(api_key="sk-..."))

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Hello!"}],
)

print(tracker.summary())

Google Gemini

from google import genai
from hong_lab_ai_cost import CostTracker

tracker = CostTracker(project="MyProject", user="kyle", email="kyle@aucklanduni.ac.nz")
client = tracker.wrap(genai.Client(api_key="..."))

response = client.models.generate_content(model="gemini-2.5-flash", contents="Hello!")

Anthropic

import anthropic
from hong_lab_ai_cost import CostTracker

tracker = CostTracker(project="MyProject", user="kyle", email="kyle@aucklanduni.ac.nz")
client = tracker.wrap(anthropic.Anthropic(api_key="..."))

response = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello!"}],
)

Third-party Proxy

from openai import OpenAI
from hong_lab_ai_cost import CostTracker

tracker = CostTracker(project="MyProject", user="kyle", email="kyle@aucklanduni.ac.nz")
client = tracker.wrap(OpenAI(api_key="...", base_url="https://z.apiyihe.org/v1"))

response = client.chat.completions.create(model="gpt-4o-mini", messages=[...])

Manual Recording

For unsupported providers, record usage manually:

tracker.record(model="llama-3-8b", prompt_tokens=1000, completion_tokens=500)

Configuration

All settings can be provided via constructor arguments, environment variables, or a .cost-tracker.yaml file (priority: constructor > env > yaml > defaults).

Setting Constructor Env Variable Default
Project name project= COST_TRACKER_PROJECT "default"
User name user= COST_TRACKER_USER None
Email email= COST_TRACKER_EMAIL None
Remote API remote_url= COST_TRACKER_REMOTE_URL None (local only)
Storage dir storage_dir= .cost-tracker/

Example .cost-tracker.yaml:

project: DentalVLM
user: kyle
email: kyle@aucklanduni.ac.nz
remote_url: https://cost-api-xxxx.run.app

Remote Sync

By default, the SDK only saves records locally. To enable automatic upload to a remote server, configure remote_url:

Method 1: Constructor argument

tracker = CostTracker(
    project="MyProject",
    user="kyle",
    email="kyle@aucklanduni.ac.nz",
    remote_url="https://cost-api-xxxx.run.app",  # Add this to enable upload
)

Method 2: Environment variable

export COST_TRACKER_REMOTE_URL="https://cost-api-xxxx.run.app"

Method 3: Config file .cost-tracker.yaml

remote_url: https://cost-api-xxxx.run.app

Records are uploaded to POST {remote_url}/api/v1/usage/batch with X-Lab-User and X-Lab-Email headers. The server validates these against a whitelist.

If the upload fails (network error, server down), the record is kept locally and retried on the next tracker.flush() or at process exit.

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

hong_lab_ai_cost-0.1.0.tar.gz (13.5 kB view details)

Uploaded Source

Built Distribution

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

hong_lab_ai_cost-0.1.0-py3-none-any.whl (13.8 kB view details)

Uploaded Python 3

File details

Details for the file hong_lab_ai_cost-0.1.0.tar.gz.

File metadata

  • Download URL: hong_lab_ai_cost-0.1.0.tar.gz
  • Upload date:
  • Size: 13.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for hong_lab_ai_cost-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8c864fbc729a5d30e1d710168071ad8d6cdf1f8992b702657707efa93de71d65
MD5 b71abf879f02befb06d5be06a6746a64
BLAKE2b-256 eb30ffa68e78d395698fb375391f05e652256a4c043a027ba0081f5c836f74cb

See more details on using hashes here.

File details

Details for the file hong_lab_ai_cost-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for hong_lab_ai_cost-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 43d43f4d9789da4089ddcb94e9df119fa4e8d2fb88d55a6da2064a51b178db8d
MD5 cff53a146b923133857b0d618c0429f2
BLAKE2b-256 23f0b7e31eddec28847723ea5ee7ea157bae03441219583ff207b8b9d7dece5d

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