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.

Supported Providers

  • OpenAI — openai 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)
  • Anthropic — anthropic package (claude-opus-4-6, claude-sonnet-4-6, claude-sonnet-4-5, claude-haiku-4-5, and older variants)
  • Google Gemini — google-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

Metadata

Release files for costrace-sdk 0.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for costrace-sdk 0.1.4
File Size Uploaded
costrace_sdk-0.1.4.tar.gz 6.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for costrace-sdk 0.1.4
File Interpreter ABI Platform
costrace_sdk-0.1.4-py3-none-any.whl Python 3 none any Details

Total release size: 12.6 kB

Release files / costrace_sdk-0.1.4.tar.gz

Download URL costrace_sdk-0.1.4.tar.gz
Size 6.0 kB
Tags Source
SHA-256 checksum
How to use checksums
fade1a7a9275c8697240276a4b95f554c3e38f7e919ffbb269b71c60cf810b2b
BLAKE2b-256 checksum
How to use checksums
7fc294cfd3d361d343aa0970e0018f63a02fd566d26fa6bcab52b8b801295c0b
Upload date
Uploaded using Trusted Publishing?
What is 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}

Release files / costrace_sdk-0.1.4-py3-none-any.whl

Download URL costrace_sdk-0.1.4-py3-none-any.whl
Size 6.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3c129306140134a165407e1023756ac71ded864f68f03d20d0e466c5e0901e91
BLAKE2b-256 checksum
How to use checksums
ca54ecc97fb2ff62cea8d51ce817f2cbc929c98415d3f84e07926d19c049c5bc
Upload date
Uploaded using Trusted Publishing?
What is 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}

Release history Release notifications | RSS feed

This release

0.1.4 This release

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page