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.2.tar.gz (4.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.2-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: costrace_sdk-0.1.2.tar.gz
  • Upload date:
  • Size: 4.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.2.tar.gz
Algorithm Hash digest
SHA256 96f1561eccf71e0afa3b9d486ce4a5aeafdaf823af26fd7b4b2da8092c036e08
MD5 fc9aa30a2c2d7b68382645e51e9c2145
BLAKE2b-256 3d92ce98e03f08c61019a6c97b8cd11158a47e9fd02fa2902a8e0d089d2ad034

See more details on using hashes here.

File details

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

File metadata

  • Download URL: costrace_sdk-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 5.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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b485ac85d64199582ee72447c1de9423a43815d52f7a67fca535715369374a21
MD5 601f14e2e35e30e60224edd221115832
BLAKE2b-256 5591b4521b431b059baaf781dc05f8c6a60c3b89b6450f5dde5bdd81aa49d73b

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