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

pip install costrace

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.0.tar.gz (4.7 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.0-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: costrace_sdk-0.1.0.tar.gz
  • Upload date:
  • Size: 4.7 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.0.tar.gz
Algorithm Hash digest
SHA256 30d88b9858372e15f5251f52c70e6a10500a748d4056209b0dbe1d55da27596f
MD5 477ebfb609d6f79b24badc9c0d5d7023
BLAKE2b-256 ba374714807011d9a4e297635f7068a95151aaa46c264c7853a1742b9d055203

See more details on using hashes here.

File details

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

File metadata

  • Download URL: costrace_sdk-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.5 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6a3db5d2ce99fd671afde1f3d226d5c6240f903a9eaafb04ef41c07fc51a1477
MD5 2827ebf9950502ba08d0b777c9193b8e
BLAKE2b-256 3402e8bb93884ee8e4432fb82b115909cd5905dff4b497c51ec756225dca7a0c

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