Python SDK for TokVigil - AI usage control platform
Project description
TokVigil Python SDK
Official Python SDK for TokVigil - AI usage control platform.
Manage rate limits, budgets, and policies for your AI-powered applications.
Installation
pip install tokvigil
Quick Start
from tokvigil import TokVigil
# Initialize client
tv = TokVigil(api_key="tv_live_xxx")
# Check if request is allowed
result = tv.evaluate(
user_id="user_123",
plan="free",
feature="chat",
model="gpt-4o-mini",
input_tokens=100
)
if result.allowed:
# Make your AI call
response = openai.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Hello!"}]
)
# Log the usage
tv.log_usage(
request_id="req_123",
user_id="user_123",
model="gpt-4o-mini",
input_tokens=response.usage.prompt_tokens,
output_tokens=response.usage.completion_tokens,
status="allowed"
)
else:
print(f"Blocked: {result.message}")
# result.reason_code: "DAILY_REQUEST_LIMIT_EXCEEDED"
# result.limit_state.requests_today: 50
# result.limit_state.requests_limit_daily: 50
Features
- ✅ Check requests against policies
- ✅ Log AI usage (tokens, cost, latency)
- ✅ Get usage analytics
- ✅ Automatic retry with backoff
- ✅ Type hints and dataclasses
Usage
Evaluate Request
result = tv.evaluate(
user_id="user_123",
model="gpt-4o-mini",
plan="free", # optional
feature="chat", # optional
input_tokens=100, # optional
input_text="Hello", # optional (estimates tokens)
)
print(result.allowed) # True or False
print(result.reason_code) # "ALLOWED" or "DAILY_REQUEST_LIMIT_EXCEEDED"
print(result.message) # Human readable message
print(result.estimated_cost_usd) # 0.0001
print(result.limit_state.requests_today) # 45
print(result.limit_state.requests_limit_daily) # 50
Log Usage
tv.log_usage(
request_id="req_123",
user_id="user_123",
model="gpt-4o-mini",
input_tokens=100,
output_tokens=50,
status="allowed",
plan="free",
feature="chat",
latency_ms=350,
)
Check and Call (Helper)
Automatically evaluate, call AI, and log usage:
def call_openai():
return openai.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Hello"}]
)
result, response = tv.check_and_call(
user_id="user_123",
model="gpt-4o-mini",
ai_function=call_openai,
plan="free",
feature="chat"
)
if result.allowed:
print(response.choices[0].message.content)
else:
print(f"Blocked: {result.message}")
Get Usage Analytics
# Summary
summary = tv.get_usage_summary()
print(summary.total_requests)
print(summary.total_tokens)
print(summary.total_cost_usd)
# Recent usage
recent = tv.get_recent_usage(page=1, page_size=20)
for record in recent.items:
print(f"{record.user_id}: {record.total_tokens} tokens")
# Usage by user
by_user = tv.get_usage_by_user()
for group in by_user.items:
print(f"{group.group}: {group.requests} requests, ${group.cost_usd}")
# Usage by feature
by_feature = tv.get_usage_by_feature()
for group in by_feature.items:
print(f"{group.group}: {group.requests} requests")
Error Handling
from tokvigil import TokVigil, RateLimitError, AuthenticationError
tv = TokVigil(api_key="tv_live_xxx")
try:
result = tv.evaluate(user_id="user_123", model="gpt-4o-mini")
except AuthenticationError as e:
print(f"Invalid API key: {e.message}")
except RateLimitError as e:
print(f"Rate limited. Retry after {e.retry_after} seconds")
except TokVigilError as e:
print(f"Error: {e.message}")
Configuration
tv = TokVigil(
api_key="tv_live_xxx",
base_url="https://api.tokvigil.com", # Custom API URL
timeout=30, # Request timeout in seconds
retry_count=3, # Number of retries
retry_delay=1, # Delay between retries
)
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
tokvigil-0.1.0.tar.gz
(10.4 kB
view details)
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 tokvigil-0.1.0.tar.gz.
File metadata
- Download URL: tokvigil-0.1.0.tar.gz
- Upload date:
- Size: 10.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
558898459a07a82d54396585968d6b94350d4f545bb1fcd866407613eb51c9a3
|
|
| MD5 |
b00a51a83c5aadd5fbe41c64e9e3338d
|
|
| BLAKE2b-256 |
468fc290fe78248e3738a627eedf4ef1e9bb49187dea414da6d00609f3acc64d
|
File details
Details for the file tokvigil-0.1.0-py3-none-any.whl.
File metadata
- Download URL: tokvigil-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c3b08cde8ed97ab0deb38f3451da54315441caa437764d1987af9c9154d474cf
|
|
| MD5 |
cda5083406c49b66623eaa6e68f40aeb
|
|
| BLAKE2b-256 |
48005f4b68a48bc272f9e96d70dbfcc63f92c91f0d402ca8e41ec474bbe27afd
|