Precise Token Tracking SDK for tknOps
Project description
tknOps Python SDK
Precise Token Tracking for Multi-Tenant SaaS.
The tknops-llm SDK provides full visibility into AI costs per user, team, and organization. It automatically captures tokens, cost, and latency from your LLM calls.
Installation
pip install .
# OR
pip install tknops-llm
Initialization
Initialize the client with your API Key (from the dashboard) and the Analzyer URL.
from tknops_llm.client import AIAnalytics
# Initialize the client
# The client runs a background thread to batch/send events asynchronously.
tracker = AIAnalytics(
api_key="your_api_key_here",
base_url="http://localhost:8000" # Update with your deployed analyzer URL
)
Usage
1. Manual Tracking
If you manually calculate tokens or want to log generic events:
tracker.track(
user_id="user-123",
model="gpt-4",
provider="openai",
input_tokens=50,
output_tokens=120,
cost_usd=0.004,
latency_ms=1500,
tags=["production", "chatbot"],
metadata={"conversation_id": "abc-123"},
prompt_text="Hello, how are you?",
response_text="I'm doing well, thank you!"
)
2. Automatic OpenAI/LangChain Response Tracking
The SDK can automatically extract metrics from standard response objects.
OpenAI Example:
import openai
response = openai.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": "Say hello!"}]
)
tracker.track_response(
response=response,
user_id="user-123",
cost_per_1k_input=0.0015,
cost_per_1k_output=0.002,
tags=["test"]
)
LangChain Example:
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4")
response = llm.invoke("Tell me a joke")
tracker.track_response(
response=response,
user_id="user-123",
tags=["langchain"]
)
Shutdown
The client uses a daemon thread. To ensure all pending events are flushed before your script exits, call:
tracker.shutdown()
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.1.tar.gz.
File metadata
- Download URL: tknops_llm-0.0.1.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
07fdf0549e6a2d55c72ae3075499c9e6f926c9951d1ce63b4cc01394bfce49a2
|
|
| MD5 |
91f2b4089b7723045c61cc164bfed33b
|
|
| BLAKE2b-256 |
1107ee1aae64ba159f4c37b75f663741d590e34d464dcf54aa54dfa3d8000c45
|
File details
Details for the file tknops_llm-0.0.1-py3-none-any.whl.
File metadata
- Download URL: tknops_llm-0.0.1-py3-none-any.whl
- Upload date:
- Size: 5.2 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 |
9b6d94c6b5b83bebeed36974916354d90a82ccf810cdfe6d6a9b91dfa20df43c
|
|
| MD5 |
9096a9cc8c81bc324f7e231610097671
|
|
| BLAKE2b-256 |
392047b10c7d6a03ae92c1dc162634ccaefe80e9c1e1fe23053224d95cef8b17
|