Skip to main content

Track cost, latency, and usage across all your LLM providers.

Project description

LLM Tracer — Python SDK

Track cost, latency, and usage across all your LLM providers in one dashboard.

Works with OpenAI, Anthropic, Google, and any provider accessible through LangChain.

Install

pip install llmtracer-sdk

Quick Start — Global Handler (recommended)

Three lines. Every LLM call captured automatically — no changes to your chains or agents.

from llmtracer import LLMTracerCallbackHandler
from langchain_core.globals import set_handler

tracer = LLMTracerCallbackHandler(api_key="lt_...")
set_handler(tracer)

That's it. Every LLM call in your application — ChatOpenAI, ChatAnthropic, ChatGoogleGenerativeAI — is automatically tracked with model, tokens, cost, and latency. No need to pass callbacks=[tracer] to individual chains or agents.

View your dashboard at llmtracer.dev.

Environment variable pattern

import os
from llmtracer import LLMTracerCallbackHandler
from langchain_core.globals import set_handler

tracer = LLMTracerCallbackHandler(
    api_key=os.environ["LLMTRACER_API_KEY"],
    tags={"env": os.environ.get("ENVIRONMENT", "development")},
    enabled=os.environ.get("LLMTRACER_ENABLED", "true").lower() == "true",
)
set_handler(tracer)

Advanced — Per-Invocation Callbacks

Use per-invocation callbacks when you need to attach per-call tags (e.g., feature, user, customer) to specific chains or agents:

from llmtracer import LLMTracerCallbackHandler

tracer = LLMTracerCallbackHandler(
    api_key="lt_...",
    tags={"env": "production"}
)

result = await agent.ainvoke(
    input_data,
    config={
        "callbacks": [tracer],
        "metadata": {
            "llmtracer_tags": {
                "feature": "chat",
                "user_id": "u_sarah",
                "customer": "acme-corp",
            }
        }
    }
)

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"

Quick Start — Direct (no LangChain)

If you're calling provider SDKs directly:

from llmtracer import LLMTracer
from openai import OpenAI

tracer = LLMTracer(api_key="lt_...", tags={"env": "production"})

client = OpenAI()
tracer.instrument_openai(client)

# All calls are now tracked automatically
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Hello"}],
    llmtracer={"tags": {"feature": "chat"}}  # optional per-call tags
)

Multi-provider

from openai import OpenAI
from anthropic import Anthropic

tracer = LLMTracer(api_key="lt_...", tags={"env": "production"})

openai_client = OpenAI()
anthropic_client = Anthropic()

tracer.instrument_openai(openai_client)
tracer.instrument_anthropic(anthropic_client)

# Both are now tracked in the same dashboard

Supported Providers

Provider LangChain Callback Direct Instrumentor
OpenAI
Anthropic
Google GenAI
Azure OpenAI Coming soon
AWS Bedrock Coming soon
Any LangChain LLM

Configuration

tracer = LLMTracerCallbackHandler(
    api_key="lt_...",           # Required — from llmtracer.dev/settings
    tags={"env": "production"}, # Optional — global tags
    flush_interval=5.0,         # Seconds between batch flushes (default: 5)
    batch_size=50,              # Events per batch (default: 50)
    enabled=True,               # Set False to disable without removing code
)

How It Works

  1. The callback handler hooks into LangChain's on_chat_model_start / on_llm_end lifecycle
  2. It captures: model name, provider, input/output tokens, latency, success/failure
  3. Events are buffered in memory and flushed every 5 seconds (or at batch_size)
  4. Flush happens in a background thread — zero latency impact on your LLM calls
  5. Events are POSTed to the LLM Tracer Cloud Function which calculates cost server-side

Zero Dependencies

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

LangChain callback mode requires langchain-core (which you already have if you're using LangChain).

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-1.0.1.tar.gz (13.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-1.0.1-py3-none-any.whl (15.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: llmtracer_sdk-1.0.1.tar.gz
  • Upload date:
  • Size: 13.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-1.0.1.tar.gz
Algorithm Hash digest
SHA256 c8631f97d963ed1cdee6fc6f008a0d23d75aca6a1054b5d1ce52063d0fe99a40
MD5 b402ba4313fac524ae9bbe3b3d13239b
BLAKE2b-256 b982f7d31b2637c681d125d3e9f87cb948e7b0e566a9d52c7461f3c19e6dc855

See more details on using hashes here.

File details

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

File metadata

  • Download URL: llmtracer_sdk-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 15.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-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8f3c9d948f664aaa4d8d8a5d8ed0b8e14672ca1d4f383ac7a2b7e23ac54fcd06
MD5 c86d9f42f874f1f5af28b0c47b525579
BLAKE2b-256 161a862c4ea3e365effa3c0abf472b6c3fa21079ecba41dfb23872fb622a5f47

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