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
Metadata
Release files for tokvigil 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| tokvigil-0.1.0.tar.gz | 10.4 kB | Without provenance | Details for tokvigil-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| tokvigil-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for tokvigil-0.1.0-py3-none-any.whl |
Total release size: 19.9 kB
Release files / tokvigil-0.1.0.tar.gz
| Download URL | tokvigil-0.1.0.tar.gz |
|---|---|
| Size | 10.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
558898459a07a82d54396585968d6b94350d4f545bb1fcd866407613eb51c9a3
|
|
BLAKE2b-256 checksum How to use checksums |
468fc290fe78248e3738a627eedf4ef1e9bb49187dea414da6d00609f3acc64d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.0
|
Release files / tokvigil-0.1.0-py3-none-any.whl
| Download URL | tokvigil-0.1.0-py3-none-any.whl |
|---|---|
| Size | 9.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c3b08cde8ed97ab0deb38f3451da54315441caa437764d1987af9c9154d474cf
|
|
BLAKE2b-256 checksum How to use checksums |
48005f4b68a48bc272f9e96d70dbfcc63f92c91f0d402ca8e41ec474bbe27afd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.0
|
Security for tokvigil 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.