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.3.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.3-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tknops_llm-0.0.3.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.3.tar.gz
Algorithm Hash digest
SHA256 67390cddfe22d60c69569bbd63f4c680bcbf60c7405911b5d5f81d3fe43515d5
MD5 7e32f09c38145adc7e3ce9fcb52eb25e
BLAKE2b-256 8523807fcd9f772805eec0cda716072d7faffaaaf098b1b20716e4d86925ed28

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tknops_llm-0.0.3-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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 ce7395c81d5772f035b19e34b5a66a96f85f5d56b3ea30fe6384d889a980f1cd
MD5 57eab768e8865ceaa85d3ebe04f4755a
BLAKE2b-256 b7db116d03bc40d710b9981f89a637945dc561c945336dd915dc0f212f08a50f

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