Skip to main content

Track AI API usage with TokenTally

Project description

TokenTally Python SDK

Track your AI API usage with TokenTally. Get real-time analytics, cost tracking, and usage insights for Claude, OpenAI, and other AI providers.

Installation

pip install tokentally

Quick Start

from tokentally import TokenTally

# Initialize the client with your API key
tt = TokenTally(api_key="tt_your_api_key")

# Track usage manually
response = tt.track(
    tokens_in=150,
    tokens_out=300,
    model="claude-3-sonnet-20240229",
    provider="anthropic",
    metadata={
        "feature": "chat",
        "user_id": "user123",
    }
)

print(f"Recorded! Cost: ${response.cost_usd:.6f}")

Usage with Context Manager

The context manager automatically tracks timing:

from tokentally import TokenTally
import anthropic

tt = TokenTally(api_key="tt_your_api_key")
client = anthropic.Anthropic()

with tt.track_usage(
    model="claude-3-sonnet-20240229",
    metadata={"feature": "summarization"}
) as ctx:
    # Make your API call
    response = client.messages.create(
        model="claude-3-sonnet-20240229",
        max_tokens=1000,
        messages=[{"role": "user", "content": "Summarize this..."}]
    )

    # Set the usage from the response
    ctx.set_usage(
        tokens_in=response.usage.input_tokens,
        tokens_out=response.usage.output_tokens,
        stop_reason=response.stop_reason,
    )

# Usage is automatically sent when the context exits
print(f"Cost: ${ctx.response.cost_usd:.6f}")

Configuration

tt = TokenTally(
    api_key="tt_your_api_key",
    base_url="https://api.tokentally.io",  # Optional custom URL
    timeout=30.0,  # Request timeout in seconds
)

Error Handling

from tokentally import TokenTally, RateLimitError, AuthenticationError, TokenTallyError

tt = TokenTally(api_key="tt_your_api_key")

try:
    tt.track(tokens_in=100, tokens_out=200, model="claude-3-sonnet")
except RateLimitError:
    print("Rate limit exceeded. Upgrade your plan for unlimited usage.")
except AuthenticationError:
    print("Invalid API key. Check your credentials.")
except TokenTallyError as e:
    print(f"Error tracking usage: {e}")

Metadata

Add custom metadata to categorize your usage:

tt.track(
    tokens_in=100,
    tokens_out=200,
    model="claude-3-sonnet-20240229",
    metadata={
        "user_id": "user123",
        "feature_name": "chat_completion",
        "environment": "production",
        "code_path": "services.chat.complete",
        "request_id": "abc-123",
    }
)

License

MIT License - see LICENSE for details.

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

tokentally-0.1.0.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

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

tokentally-0.1.0-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for tokentally-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e94ca7331aa9ca78be61821153afafafe53b771b0f127f43ccba1877c41baca3
MD5 dd7f45e76ffd5be0c051dc1a661cce72
BLAKE2b-256 194ef79b19d75b252a775450023a4e562d537fa1465e42be4f045c90d57c5991

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for tokentally-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b52a3360c2b5816d47672925fda92041bfe794d5a2a033844c34c8a08ba73c9f
MD5 fe32236efe3a8b43d9de3d2fa30f2241
BLAKE2b-256 f54d974762a495f973a1700e71808debee67957cc8cdaaf2fae5e68eef8915e2

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