Skip to main content

Precise Token Tracking SDK for tknOps

Project description

tknOps LLM Analytics SDK

The Python SDK for tknOps, an AI cost and usage analytics platform.

Features

  • Automatic Usage Tracking: Capture token usage and cost.
  • Privacy-First: Prompt and response content are NOT stored by default.
  • Cost Calculation: Built-in pricing registry for common models (OpenAI, Anthropic).
  • Environment Tagging: Tag events as prod, dev, or staging.
  • Framework Support: Automatic extraction for OpenAI and LangChain response objects.

Installation

pip install tknops-llm

Usage

Initialization

from tknops_llm.client import AIAnalytics

client = AIAnalytics(
    api_key="your_api_key_here", # Get your API Key from the tknOps Dashboard
    environment="prod", # Optional: "prod", "dev", "staging". Default: "prod"
    collect_content=False # Optional: Set to True to collect prompt/response text. Default: False
)

1. Automatic Tracking (OpenAI / LangChain)

Use track_response to automatically extract metrics and calculate costs from response objects.

from langchain_openai import ChatOpenAI

llm = ChatOpenAI(model="gpt-4o", ...)
response = llm.invoke("Hello world!")

# Automatically extracts tokens and calculates cost based on model name
client.track_response(
    response=response,
    user_id="user-123",
    response_type="langchain", # "openai" or "langchain"
    tags=["chatbot", "customer-service"]
)

2. Manual Tracking

If you are using a custom model or provider, you can track events manually.

client.track(
    user_id="user-123",
    model="llama-3-8b",
    provider="together-ai",
    input_tokens=150,
    output_tokens=50,
    cost_usd=0.0002, # Optional: Calculated by you
    latency_ms=450,
    tags=["custom-model"]
)

3. Content Collection (Privacy)

By default, the SDK does not send the prompt or response text to the server. To enable content debugging:

# Initialize with collection enabled
client = AIAnalytics(..., collect_content=True)

# OR pass it explicitly in manual track (only if initialized with True)
client.track(..., prompt_text="My prompt", response_text="My response")

Configuration

| Parameter | Description | Default | |Params|---|---| | api_key | Your Project API Key | Required | | environment | Environment tag (e.g. prod, dev) | "prod" | | collect_content | If True, sends text content to server. | False |

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

tknops_llm-0.0.4.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

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

tknops_llm-0.0.4-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file tknops_llm-0.0.4.tar.gz.

File metadata

  • Download URL: tknops_llm-0.0.4.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.4

File hashes

Hashes for tknops_llm-0.0.4.tar.gz
Algorithm Hash digest
SHA256 718721091b274f5b721bed1a7f9c7503c10b1f09e590e68b676cd6cf7b0bcf5f
MD5 cd89abd1a5c89cfa147ec33b070181b9
BLAKE2b-256 c0150d6e52c15c9109b0ac87ebb46d400fd0085ae4b7fb673bf8e1d307273d08

See more details on using hashes here.

File details

Details for the file tknops_llm-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: tknops_llm-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.4

File hashes

Hashes for tknops_llm-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 eec2c41e163c93cccfca316c92945d84aa78e58af8f4a45a88d1cdc47dfe32b1
MD5 a85d990d672bea2110862d56e0a446c7
BLAKE2b-256 5c67f5daa388bc22c564f8c0766ae218220d696f4174afe11a0eda181ace9c69

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