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, orstaging. - 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,
response_type="langchain", # "openai" or "langchain"
user_id="user-123", # Optional: Internal user ID
feature="summarization", # Optional: Feature name
team="marketing", # Optional: Team or Dept name
agent="assistant_v1", # Optional: Specific agent ID
tags=["bot", "v2-test"] # Optional: Custom tags for filtering
)
2. Manual Tracking
If you are using a custom model or provider, you can track events manually.
client.track(
model="llama-3-8b",
provider="together-ai",
input_tokens=150,
output_tokens=50,
user_id="user-123", # Optional: Internal user ID
feature="adhoc-query", # Optional: Feature name
team="data-science", # Optional: Team name
agent="research-bot", # Optional: Agent ID
cost_usd=0.0002, # Optional: Calculated by you
latency_ms=450, # Optional: Latency in ms
tags=["custom-model"] # Optional: Custom tags
)
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")
Tracking Parameters
The following parameters can be passed to tracking methods (track_response, track):
| Parameter | Type | Description |
|---|---|---|
user_id |
str |
Optional. The unique ID of the end-user in your system. Used for per-user cost analysis. |
feature |
str |
Optional. The name of the feature or module where the AI is used (e.g., "summarization", "chat"). |
team |
str |
Optional. The team or department responsible for this usage (e.g., "marketing", "customer-success"). |
agent |
str |
Optional. The specific agent or bot identifier (e.g., "assistant_v1", "billing_bot"). |
environment |
str |
Optional. The deployment stage. Defaults to prod. Common values: stage, prod, dev. |
tags |
List[str] |
Optional. A list of custom strings for granular filtering and grouping. |
Configuration
| Parameter | Description | Default |
|---|---|---|
api_key |
Your Project API Key (obtained from tknOps dashboard) | Required |
environment |
Default environment tag for all events | "prod" |
collect_content |
If True, sends prompt/response text to the server for debugging. |
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tknops_llm-0.0.5.tar.gz.
File metadata
- Download URL: tknops_llm-0.0.5.tar.gz
- Upload date:
- Size: 5.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6a9c7c015b1c0ba5f05f529795ff90eb7da0c3d61745de2356b07ab428cd9659
|
|
| MD5 |
bc914b9b47ba5f69b485ca0b41c1b606
|
|
| BLAKE2b-256 |
42a13fc8f2be301bc538164e4b6f2a2adb402ca9c829a69d26647e95dec24740
|
File details
Details for the file tknops_llm-0.0.5-py3-none-any.whl.
File metadata
- Download URL: tknops_llm-0.0.5-py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4c4ec49fbf93ce3ea000a93986238a3a063b94d0a45b279354dc65e6249f6c99
|
|
| MD5 |
f06980ff261ed14ad778f6f0b83b499b
|
|
| BLAKE2b-256 |
57df545137f59a55feaf2ec142f44ee8f0ab84c0aeb8c88907499428c523400f
|