Skip to main content

Python SDK for AISpendGuard — track AI API spend with tags-only observability

Project description

aispendguard

Python SDK for AISpendGuard — track AI API spend with tags-only observability. No prompts or outputs are ever sent.

Install

pip install aispendguard

Quick Start

from aispendguard import AISpendGuard, create_openai_event
import openai, time

client = AISpendGuard(api_key="asg_your_key_here")

openai_client = openai.OpenAI()
start = time.time()

response = openai_client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Classify: 'I want to cancel'"}],
)

event = create_openai_event(
    model="gpt-4o-mini",
    usage=response.usage,
    latency_ms=int((time.time() - start) * 1000),
    tags={
        "task_type": "classify",
        "feature": "ticket_triage",
        "route": "POST /api/classify",
    },
)
client.track(event)

Provider Helpers

OpenAI

from aispendguard import create_openai_event

event = create_openai_event(
    model="gpt-4o-mini",
    usage=response.usage,           # auto-extracts tokens, cache hits, reasoning
    latency_ms=420,
    resolved_model=response.model,  # e.g. "gpt-4o-mini-2024-07-18"
    tags={"task_type": "classify", "feature": "triage", "route": "POST /api/classify"},
)

Anthropic

from aispendguard import create_anthropic_event

event = create_anthropic_event(
    model="claude-sonnet-4-20250514",
    usage=message.usage,            # auto-extracts cache_read + cache_creation
    latency_ms=800,
    tags={"task_type": "summarize", "feature": "digest", "route": "POST /api/summarize"},
)

Google Gemini

from aispendguard import create_gemini_event

event = create_gemini_event(
    model="gemini-2.5-flash",
    usage=response.usage_metadata,  # auto-extracts thoughts_token_count
    latency_ms=500,
    tags={"task_type": "answer", "feature": "search", "route": "POST /api/search"},
)

Streaming Responses

With streaming, usage data arrives in the final chunk. Accumulate the stream first, then track:

# OpenAI streaming
stream = openai_client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Hello"}],
    stream=True,
    stream_options={"include_usage": True},  # required for usage in stream
)

usage = None
for chunk in stream:
    if chunk.usage:
        usage = chunk.usage
    # ... process chunk.choices

if usage:
    event = create_openai_event(
        model="gpt-4o-mini", usage=usage, latency_ms=latency, tags=tags
    )
    client.track(event)
# Anthropic streaming
with anthropic_client.messages.stream(
    model="claude-sonnet-4-20250514",
    messages=[{"role": "user", "content": "Hello"}],
    max_tokens=200,
) as stream:
    for text in stream.text_stream:
        pass  # process text
    message = stream.get_final_message()

event = create_anthropic_event(
    model="claude-sonnet-4-20250514", usage=message.usage, latency_ms=latency, tags=tags
)
client.track(event)

Configuration

client = AISpendGuard(
    api_key="asg_...",
    endpoint="https://www.aispendguard.com/api/ingest",  # default
    batch_size=10,          # events buffered before auto-send
    flush_interval=5.0,     # seconds between background flushes
    timeout=10.0,           # HTTP timeout
    strict=False,           # True = raise on errors instead of logging
    default_tags={          # merged into every event
        "environment": "prod",
        "service": "api",
    },
)

Required Tags

Every event must include these tags:

Tag Description
task_type What the model does: classify, summarize, generate, code, etc.
feature Your product feature name: ticket_triage, doc_search, etc.
route API route or function: POST /api/classify, handle_support, etc.

Privacy

This SDK never sends prompt content, messages, or model outputs. Only metadata (tokens, model, latency) and your tags are transmitted. Forbidden tag keys (like prompt, messages, content) are rejected at validation time.

License

MIT

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

aispendguard-0.1.0.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aispendguard-0.1.0-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file aispendguard-0.1.0.tar.gz.

File metadata

  • Download URL: aispendguard-0.1.0.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for aispendguard-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9cce48c1b32f7d64350cc0701302249b90dcd6fbed92fc5979d31027272d6d91
MD5 fc800de72c6e45e89be9aab9a8ca2a24
BLAKE2b-256 ede9d271ba232f86b560bae764ba150ca328b91c63120ed0a23644df9cbecd0b

See more details on using hashes here.

Provenance

The following attestation bundles were made for aispendguard-0.1.0.tar.gz:

Publisher: publish.yml on AISpendGuard/aispendguard-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file aispendguard-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: aispendguard-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for aispendguard-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d6d27b79d491d464723e3a8a23bf581a4e71168049b2948b274c8af08990ad91
MD5 e282692411225f5dd381f9d279d01568
BLAKE2b-256 803bf367d38207c0adf88843de1328e829106e6a373253a9c383040438ebfc3c

See more details on using hashes here.

Provenance

The following attestation bundles were made for aispendguard-0.1.0-py3-none-any.whl:

Publisher: publish.yml on AISpendGuard/aispendguard-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page