Skip to main content

Auto-track LLM cost, latency, and usage. Two lines of code, every provider.

Project description

LLM Tracer — Python SDK

Track cost, latency, and token usage across OpenAI, Anthropic, and Google Gemini — in one line of code.

version

Install

pip install llmtracer-sdk

Quick Start

import llmtracer

llmtracer.init(api_key="lt_...")

# That's it. All OpenAI, Anthropic, and Google Gemini calls are now tracked automatically.

No wrappers, no callbacks, no code changes. The SDK auto-patches your provider clients at import time.

View your dashboard at llmtracer.dev.

What Gets Captured

Every LLM call is automatically tracked with:

  • Provider, model, tokens (input + output), latency, cost
  • Google Gemini: thinking tokens (2.5 models), tool tokens, cached tokens
  • Anthropic: cache creation + read tokens
  • OpenAI: reasoning tokens (o1/o3/o4), cached tokens
  • Caller file, function, and line number
  • Auto-flush on process exit (no manual flush needed)

Environment Variable Pattern

import os
import llmtracer

llmtracer.init(
    api_key=os.environ["LLMTRACER_API_KEY"],
    debug=True,  # prints token counts to console
)

Trace Context and Tags

with llmtracer.trace(tags={"feature": "chat", "user_id": "u_sarah"}):
    response = client.chat.completions.create(...)

Tags appear in the dashboard's Breakdown page and Top Tags card. Use them to answer questions like "which user costs the most?" or "which feature should I optimize?"

Tagging Patterns

Pattern Tag Example
Track cost by feature feature "chat", "search", "summarize"
Track cost by user user_id "u_sarah", "u_mike"
Track cost by customer (B2B) customer "acme-corp", "initech"
Track cost by conversation conversation_id "conv_abc123"
Track environment env "production", "staging"

Supported Providers

Provider Package Auto-patched
OpenAI openai
Anthropic anthropic
Google Gemini google-genai

LangChain Support

If you use LangChain with ChatOpenAI, ChatAnthropic, or ChatGoogleGenerativeAI, the underlying SDK calls are auto-captured. No callback handler needed — just llmtracer.init() and you're done.

Debug Mode

Enable debug=True to print token counts to the console:

llmtracer.init(api_key="lt_...", debug=True)
[llmtracer] openai gpt-4o | 1,247 in → 384 out | $0.0094 | 1.2s
[llmtracer] anthropic claude-sonnet-4-5 | 2,100 in → 512 out (cache_read: 1,800) | $0.0031 | 0.8s
[llmtracer] google gemini-2.5-pro | 900 in → 280 out (thinking: 1,420) | $0.0067 | 2.1s

Requirements

  • Python 3.8+
  • Works with any version of openai, anthropic, or google-genai SDKs

Zero Dependencies

The core SDK uses only Python stdlib (urllib.request, threading, hashlib).

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

llmtracer_sdk-2.2.2.tar.gz (28.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

llmtracer_sdk-2.2.2-py3-none-any.whl (18.7 kB view details)

Uploaded Python 3

File details

Details for the file llmtracer_sdk-2.2.2.tar.gz.

File metadata

  • Download URL: llmtracer_sdk-2.2.2.tar.gz
  • Upload date:
  • Size: 28.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for llmtracer_sdk-2.2.2.tar.gz
Algorithm Hash digest
SHA256 5e628bbf94640ade5faba0526851212006f68870b40c785435edb2e005ee59cc
MD5 7f6cdd442f58bae9fd8aabb35f0a6372
BLAKE2b-256 b23def2b245911163bd4a055a0d134b507d5a7c5c359ae540c45ff92bf837452

See more details on using hashes here.

File details

Details for the file llmtracer_sdk-2.2.2-py3-none-any.whl.

File metadata

  • Download URL: llmtracer_sdk-2.2.2-py3-none-any.whl
  • Upload date:
  • Size: 18.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for llmtracer_sdk-2.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f914486ab5462bc574e860c6270a955fa7f3d2e79e3e813fbe6c3eb3bdc93e4a
MD5 feb147a3b1b5f4ad0a7789726e5f4f71
BLAKE2b-256 905ac6ae81af9df9718f1d5b347674e017602f7a4928e3f1823922c062fc9cee

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