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 —
openaipackage (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 —
anthropicpackage (claude-opus-4-6, claude-sonnet-4-6, claude-sonnet-4-5, claude-haiku-4-5, and older variants) - Google Gemini —
google-genaipackage (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)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| costrace_sdk-0.1.4.tar.gz | 6.0 kB | Without provenance | Details for costrace_sdk-0.1.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| costrace_sdk-0.1.4-py3-none-any.whl | Python 3 | none | any | Details for costrace_sdk-0.1.4-py3-none-any.whl |
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}
|
Security for costrace-sdk 0.1.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.