Skip to main content

Precise Token Tracking SDK for tknOps

Project description

tknOps LLM Analytics SDK

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

Features

  • Automatic Usage Tracking: Capture token usage, cost, and latency.
  • 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",
    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 |

Comparison: Development vs Production

During local development, you can point the SDK to your local API instance without changing code by setting the environment variable:

export TKNOPS_API_URL="http://localhost:8000"

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.2.tar.gz (5.1 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.2-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tknops_llm-0.0.2.tar.gz
  • Upload date:
  • Size: 5.1 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.2.tar.gz
Algorithm Hash digest
SHA256 79e92a97cb2704e68e7530958d20e3660e886cdb957ecfc26f2b2e65b686a8c2
MD5 ab1ef218481a56795b7c5762da093078
BLAKE2b-256 8f8dd4ef09bb332ab2a2c3b33302b944e5fc0f4bf359c591a1259d1111dd4c13

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tknops_llm-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 6.0 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 46d7c954b44bebcc246562073b1514dea78b7440c4caf307245d29392d66d5c8
MD5 bdbc65f07e366f155ef0bc00560302a3
BLAKE2b-256 a1943646d67796a6408e3375942cfd29f017075eed7e0c541d92b4baaebb7b16

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