Track and optimize your AI spending with Tokengrip
Project description
tokengrip
Track and optimize your AI spending with Tokengrip.
Installation
pip install tokengrip
Quick Start
from tokengrip import Tokengrip, wrap_openai
import openai
tg = Tokengrip(api_key="tq_live_...")
client = wrap_openai(openai.OpenAI(), tg)
# Usage is automatically tracked
response = client.chat.completions.create(
model="gpt-4.1",
messages=[{"role": "user", "content": "Hello!"}],
)
# Don't forget to flush on exit
tg.shutdown()
Wrappers
OpenAI
from tokengrip import Tokengrip, wrap_openai
import openai
tg = Tokengrip(api_key="tq_live_...", project_id="my-project")
client = wrap_openai(openai.OpenAI(), tg)
# Chat Completions API
response = client.chat.completions.create(
model="gpt-4.1",
messages=[{"role": "user", "content": "Hello!"}],
)
# Responses API
response = client.responses.create(
model="gpt-4.1",
input="Hello!",
)
# Streaming works too
stream = client.chat.completions.create(
model="gpt-4.1",
messages=[{"role": "user", "content": "Hello!"}],
stream=True,
)
for chunk in stream:
print(chunk.choices[0].delta.content or "", end="")
Anthropic
from tokengrip import Tokengrip, wrap_anthropic
import anthropic
tg = Tokengrip(api_key="tq_live_...", project_id="my-project")
client = wrap_anthropic(anthropic.Anthropic(), tg)
response = client.messages.create(
model="claude-sonnet-4-5-20250929",
max_tokens=1024,
messages=[{"role": "user", "content": "Hello!"}],
)
Async Clients
Both sync and async clients are supported transparently:
import openai
from tokengrip import Tokengrip, wrap_openai
tg = Tokengrip(api_key="tq_live_...")
client = wrap_openai(openai.AsyncOpenAI(), tg)
response = await client.chat.completions.create(
model="gpt-4.1",
messages=[{"role": "user", "content": "Hello!"}],
)
Options
tg = Tokengrip(
api_key="tq_live_...", # Required
base_url="https://...", # Default: https://tokengrip.com
project_id="my-project", # Applied to all records
agent_name="my-agent", # Applied to all records
debug=True, # Log tracking activity
batch_size=10, # Records per batch
flush_interval_ms=5000, # Auto-flush interval
)
# Wrapper-specific options
client = wrap_openai(openai.OpenAI(), tg, {
"project_id": "override", # Override per-wrapper
"agent_name": "override",
"task_type": "summarization",
"capture_content": True, # Capture truncated prompt/response
})
Manual Tracking
tg.track(
provider="openai",
model="gpt-4.1",
inputTokens=500,
outputTokens=150,
latencyMs=1200,
projectId="my-project",
)
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
tokengrip-0.1.0.tar.gz
(10.3 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
tokengrip-0.1.0-py3-none-any.whl
(10.8 kB
view details)
File details
Details for the file tokengrip-0.1.0.tar.gz.
File metadata
- Download URL: tokengrip-0.1.0.tar.gz
- Upload date:
- Size: 10.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
92c132c6bfb9e98234f688334c851b6f076756708f376e4ec2a8665de48cecf0
|
|
| MD5 |
3bba8c3c788d834fd370c4f67df2bb04
|
|
| BLAKE2b-256 |
58c38c8b07f97582d017526281e7205a92d7f59bc7e1a121e1e0d8325b019cdc
|
File details
Details for the file tokengrip-0.1.0-py3-none-any.whl.
File metadata
- Download URL: tokengrip-0.1.0-py3-none-any.whl
- Upload date:
- Size: 10.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
89680d38872cf05c11b5a12f035062ba6415efdc366900db2c6a7d58697abd5d
|
|
| MD5 |
d21447e60942ecfb95a976606ab698be
|
|
| BLAKE2b-256 |
84b168e2a0746010f67a3522a8a62bdd81f63310a654c4517dde5d5a5f22c9f7
|