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
- 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
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
001a7ed64ae2c18ce85654bc5db33753aba4524294f094c9cf384d5b97f49a69
|
|
| MD5 |
cd45700da0110293f5aa58df2b656db7
|
|
| BLAKE2b-256 |
0d204786f86645712fb1cbe9b9cdbedc986e8a151f31c7787668bf428f22bd35
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
234a402fe5b8b1821bc73e7bfedda008ed78f7bb07048169437564bd07facb07
|
|
| MD5 |
3baeed82dc98089792c017e29c3a2e79
|
|
| BLAKE2b-256 |
da35f9b2ac7f5fdbd544dbab7fd552cca4b2e2d590479a0b6475e4e5707e9123
|