Skip to main content

Automatic LLM cost tracking for OpenAI, Anthropic, and more

Project description

Tokenr Python SDK

Automatic LLM cost tracking in one line of code.

Track costs from OpenAI, Anthropic, and other LLM providers with zero code changes. Get real-time visibility into spending by agent, feature, team, or any dimension you need.

Features

  • One-line setup — Add tokenr.init() and you're done
  • Zero code changes — Use OpenAI/Anthropic SDK as normal
  • Automatic tracking — Token counts and costs tracked automatically
  • Async by default — Never slows down your app
  • Multi-provider — OpenAI, Anthropic (more coming)
  • Rich attribution — Track by agent, feature, team, or custom tags
  • Production-ready — Handles errors gracefully, never crashes your app

Installation

pip install tokenr

Quickstart

OpenAI

import tokenr
import openai

# One line to enable tracking
tokenr.init(token="your-tokenr-token")

# Use OpenAI exactly as you normally would
response = openai.chat.completions.create(
    model="gpt-4",
    messages=[{"role": "user", "content": "Hello!"}]
)

# That's it — costs are automatically tracked to Tokenr

Anthropic

import tokenr
from anthropic import Anthropic

tokenr.init(token="your-tokenr-token")

client = Anthropic(api_key="your-anthropic-key")
response = client.messages.create(
    model="claude-opus-4-5",
    messages=[{"role": "user", "content": "Hello!"}]
)

# Automatically tracked!

Configuration

Environment Variables

export TOKENR_TOKEN="your-token"
import tokenr
tokenr.init()  # Reads from TOKENR_TOKEN

All Options

tokenr.init(
    token="your-token",           # API token (or TOKENR_TOKEN env var)
    url="https://tokenr.co/...",  # Override API URL (optional)
    agent_id="my-app",            # Default agent ID for all requests
    tags={"environment": "prod"}, # Default tags applied to every request
    enabled=True,                 # Set False to disable tracking entirely
    debug=False,                  # Print tracking info to console
)

Disable in Development

import os
tokenr.init(
    token=os.getenv("TOKENR_TOKEN"),
    enabled=os.getenv("ENV") == "production"
)

Advanced Usage

Track by Agent

# Option 1: Set a default agent ID at init
tokenr.init(token="...", agent_id="customer-support-bot")

# Option 2: Override per request
response = openai.chat.completions.create(
    model="gpt-4",
    messages=[...],
    tokenr_agent_id="sales-assistant"
)

Track by Feature

response = openai.chat.completions.create(
    model="gpt-4",
    messages=[...],
    tokenr_feature="chat",
    tokenr_agent_id="support-bot"
)

Multi-Tenant Tracking

response = openai.chat.completions.create(
    model="gpt-4",
    messages=[...],
    tokenr_team_id="team-123",
    tokenr_agent_id="shared-bot"
)

Custom Tags

response = openai.chat.completions.create(
    model="gpt-4",
    messages=[...],
    tokenr_tags={
        "customer_id": "cust_123",
        "conversation_id": "conv_456",
        "language": "en",
        "priority": "high"
    }
)

Manual Tracking

For providers not yet auto-patched, or when you need full control:

import tokenr

tokenr.track(
    provider="custom-provider",
    model="custom-model-v1",
    input_tokens=1000,
    output_tokens=500,
    agent_id="my-agent",
    feature_name="translation",
    tags={"language": "es"}
)

Per-Request Parameters

Add any of these to OpenAI or Anthropic calls:

Parameter Type Description
tokenr_agent_id str Agent identifier
tokenr_feature str Feature name
tokenr_team_id str Team or customer ID
tokenr_tags dict Arbitrary metadata

tokenr.track() Reference

tokenr.track(
    provider="openai",    # required
    model="gpt-4",        # required
    input_tokens=100,     # required
    output_tokens=50,     # required
    agent_id=None,
    feature_name=None,
    team_id=None,
    status="success",     # "success" or "error"
    latency_ms=None,
    tags=None,
    requested_at=None,    # ISO 8601 timestamp; defaults to now
)

Supported Providers

Provider Auto-Tracking Manual Tracking
OpenAI Yes Yes
Anthropic Yes Yes
Google AI Coming soon Yes
Cohere Coming soon Yes
Custom Yes

How It Works

  1. tokenr.init() wraps the create method on OpenAI and Anthropic clients
  2. After each call, token counts are read from the response usage field
  3. A background daemon thread sends the data to Tokenr asynchronously
  4. If tracking fails for any reason, your app is completely unaffected

Examples

See the examples/ directory:

Getting Your API Token

  1. Sign up at tokenr.co
  2. Go to API Tokens and create a token
  3. Copy it — it's shown only once
export TOKENR_TOKEN="your-token-here"

Troubleshooting

No data showing up?

tokenr.init(token="your-token", debug=True)
# Prints: [Tokenr] Tracked: gpt-4 - $0.0012
  1. Verify echo $TOKENR_TOKEN returns your token
  2. Confirm you're making actual LLM API calls
  3. Check network access to tokenr.co

Errors? The SDK never raises — it always fails silently. Use debug=True to see what's happening.

Security

This SDK is open source so you can audit exactly what data is sent and when. The short version:

  • Only token counts, model names, and your attribution metadata are transmitted
  • No prompt content or response content ever leaves your application
  • All requests use HTTPS
  • The tracker runs in a daemon thread and cannot block your process

License

MIT — see LICENSE

Support

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

tokenr-0.1.0.tar.gz (13.2 kB view details)

Uploaded Source

Built Distribution

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

tokenr-0.1.0-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tokenr-0.1.0.tar.gz
  • Upload date:
  • Size: 13.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for tokenr-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6cd071177522541d18cd3595b9f6dae9dec284fc7524c67fa5f6255be6870b85
MD5 c8fa2d0b50759c212d4d92626514e72e
BLAKE2b-256 637e512efb07a3af0acf2dd1a541bba0ca242a3f0a22bdd52b47f28415f6a9d6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tokenr-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for tokenr-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 13b8dd66fbfd15ecbe338954c21c1ea1a729a634c1faa7f96b1bd8a9defc4333
MD5 1d9dcb3b6218a2d61f33804c7679ad6c
BLAKE2b-256 d1f30c6d77e2490cea2ecf774831d2d7a524fb8f4a488abeb8aa9f3ed4d0ada0

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