Skip to main content

Costrace python SDK for tracking cost, token usage, and latency of LLM API calls. Works by monkey-patching supported LLM client libraries so existing code requires no changes.

Project description

costrace

Python SDK for tracking cost, token usage, and latency of LLM API calls. Works by monkey-patching supported LLM client libraries so existing code requires no changes.

Supported Providers

  • OpenAIopenai package (gpt-5.2, gpt-5, gpt-5-mini, gpt-5-nano, o3, o4-mini, gpt-4o, gpt-4o-mini, gpt-4.1, gpt-4-turbo, gpt-4, gpt-3.5-turbo)
  • Anthropicanthropic package (claude-opus-4-6, claude-sonnet-4-6, claude-sonnet-4-5, claude-haiku-4-5, and older variants)
  • Google Geminigoogle-genai package (gemini-2.0-flash, gemini-2.0-flash-lite, gemini-1.5-pro, gemini-1.5-flash, gemini-1.5-flash-8b)

Install

Install with the provider(s) you use:

pip install costrace-sdk[openai]       # OpenAI only
pip install costrace-sdk[anthropic]    # Anthropic only
pip install costrace-sdk[gemini]       # Gemini only
pip install costrace-sdk[all]          # all providers

Usage

Call init() once at startup. After that, use your LLM SDKs as normal — all calls are automatically tracked.

import costrace
import openai

costrace.init(api_key="your-costrace-api-key")

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

Works the same way with Anthropic and Gemini:

import anthropic

client = anthropic.Anthropic()
response = client.messages.create(
    model="claude-sonnet-4-5-20250929",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello"}],
)
from google import genai

client = genai.Client(api_key="your-gemini-key")
response = client.models.generate_content(
    model="gemini-2.0-flash",
    contents="Hello",
)

What Gets Tracked

Each LLM call sends a trace to the Costrace backend containing:

  • Provider and model name
  • Input and output token counts
  • Latency in milliseconds
  • Calculated cost in USD
  • Success/error status

Traces are sent in a background thread — they don't block your application.

Configuration

# Required: your Costrace API key
costrace.init(api_key="your-key")

# Optional: custom backend endpoint
costrace.init(api_key="your-key", endpoint="https://your-endpoint.com/v1/traces")

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

costrace_sdk-0.1.3.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

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

costrace_sdk-0.1.3-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file costrace_sdk-0.1.3.tar.gz.

File metadata

  • Download URL: costrace_sdk-0.1.3.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for costrace_sdk-0.1.3.tar.gz
Algorithm Hash digest
SHA256 001a7ed64ae2c18ce85654bc5db33753aba4524294f094c9cf384d5b97f49a69
MD5 cd45700da0110293f5aa58df2b656db7
BLAKE2b-256 0d204786f86645712fb1cbe9b9cdbedc986e8a151f31c7787668bf428f22bd35

See more details on using hashes here.

File details

Details for the file costrace_sdk-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: costrace_sdk-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for costrace_sdk-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 234a402fe5b8b1821bc73e7bfedda008ed78f7bb07048169437564bd07facb07
MD5 3baeed82dc98089792c017e29c3a2e79
BLAKE2b-256 da35f9b2ac7f5fdbd544dbab7fd552cca4b2e2d590479a0b6475e4e5707e9123

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