Skip to main content

tokenguard (Python SDK)

Monitor AI agents, track LLM costs, debug failures — in 2 lines of code.

Install

pip install tokenguard

Quick Start — Auto-wrap Groq (zero code change)

from groq import Groq
from tokenguard import TokenGuard

aw = TokenGuard(
    api_key="tg_live_...",           # from your dashboard Settings
    base_url="http://localhost:3000", # your TokenGuard URL
)

# Wrap your existing Groq client — ONE LINE
groq = aw.wrap_groq(Groq(api_key="..."), agent_name="DietAgent")

# Use exactly as before — tracking happens automatically
response = groq.chat.completions.create(
    model="llama3-8b-8192",
    messages=[{"role": "user", "content": "Give me a low-carb meal plan"}],
)

print(response.choices[0].message.content)
# → Your dashboard now shows cost, tokens, latency for this call

Quick Start — Auto-wrap OpenAI

from openai import OpenAI
from tokenguard import TokenGuard

aw = TokenGuard(api_key="tg_live_...", base_url="http://localhost:3000")

# Wrap your OpenAI client
openai = aw.wrap_openai(OpenAI(api_key="..."), agent_name="SupportBot")

response = openai.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Hello"}],
)

Manual Tracing (full control)

from tokenguard import TokenGuard

aw = TokenGuard(api_key="tg_live_...", base_url="http://localhost:3000")

def get_diet_plan(user_message):
    with aw.trace("DietSuggestionAgent") as trace:

        # Track the Groq call
        llm = trace.llm(model="llama3-8b-8192", provider="groq")
        response = groq_client.chat.completions.create(
            model="llama3-8b-8192",
            messages=[{"role": "user", "content": user_message}],
        )
        llm.end(
            input_tokens=response.usage.prompt_tokens,
            output_tokens=response.usage.completion_tokens,
        )

        # Track a tool call (optional)
        tool = trace.tool("nutrition_database_lookup")
        foods = lookup_foods(user_message)
        tool.end(result=foods)

        return response.choices[0].message.content

What appears in your dashboard

Every call shows:

  • 💰 Cost — exact cost per request
  • ⏱️ Latency — how long it took
  • 🔢 Tokens — input and output token counts
  • 🐛 Errors — full error details with stack traces
  • 📊 Agent breakdown — which agents cost the most

Supported providers

Provider Method Notes
Groq aw.wrap_groq(client) LLaMA, Mixtral, Gemma
OpenAI aw.wrap_openai(client) GPT-4o, GPT-4o-mini
Any LLM Manual trace.llm() Works with any provider

Before you exit your script

aw.flush()  # makes sure all traces are sent before process ends

Release files for tokenguard-sdk 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tokenguard-sdk 0.1.0
File Size Uploaded
tokenguard_sdk-0.1.0.tar.gz 8.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tokenguard-sdk 0.1.0
File Interpreter ABI Platform
tokenguard_sdk-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 17.0 kB

Release files / tokenguard_sdk-0.1.0.tar.gz

Download URL tokenguard_sdk-0.1.0.tar.gz
Size 8.6 kB
Tags Source
SHA-256 checksum
How to use checksums
c90ac69bea256fff01686a15c04ac40d9637394e610d752674820479163478bf
BLAKE2b-256 checksum
How to use checksums
edac7d87f1b41da09a2bf30be199dd6eac4947f8ca94ba9fd116d541ef729ed9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.9

Release files / tokenguard_sdk-0.1.0-py3-none-any.whl

Download URL tokenguard_sdk-0.1.0-py3-none-any.whl
Size 8.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
80db377124ac174810f9ac42327016f51ff95b396b9cb42a1bd9fa9147960097
BLAKE2b-256 checksum
How to use checksums
fda1861e6234c402d5b72adc087d3304520cb8b314a761a5a151ce1cc5c345d2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.9

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page