Skip to main content

Spend Hawk SDK

Automatic LLM cost tracking for OpenAI, Anthropic, and Google Generative AI.

Track your AI API costs automatically with zero code changes. Spend Hawk intercepts API responses (never your API keys!) and sends usage metrics to your dashboard.

Features

  • 🔒 Secure: Never touches your API keys or request data
  • ⚡ Non-blocking: < 1ms overhead, async background sends
  • 🧵 Thread-safe: Works with multithreading and async code
  • 📊 Automatic: No code changes needed after setup
  • 💰 Accurate: Tracks tokens, latency, and calculates costs

Installation

pip install spend-hawk-sdk

Quick Start

import spend_hawk
from openai import OpenAI

# 1. Initialize Spend Hawk (patches OpenAI, Anthropic, and Google)
spend_hawk.patch_all()

# 2. Optional: Set project context
spend_hawk.set_context(project_id="my-project", agent="my-agent")

# 3. Use OpenAI as normal - metrics auto-tracked!
client = OpenAI()
response = client.chat.completions.create(
    model="gpt-4",
    messages=[{"role": "user", "content": "Hello!"}]
)

# Metrics automatically sent to Spend Hawk backend 🎉

Configuration

Set environment variables:

export SPEND_HAWK_API_KEY="your-api-key"
export SPEND_HAWK_API_ENDPOINT="https://api.spendhawk.com"  # optional
export SPEND_HAWK_PROJECT_ID="my-project"  # optional
export SPEND_HAWK_AGENT="my-agent"  # optional

Or configure in code:

import spend_hawk

spend_hawk.config.api_key = "your-api-key"
spend_hawk.config.project_id = "my-project"

Dynamic Context

Tag API calls dynamically:

# Set context for all subsequent calls
spend_hawk.set_context(project_id="project-a", agent="agent-1")

# Use as context manager
with spend_hawk.context(project_id="project-b", agent="agent-2"):
    # API calls here will use this context
    response = client.chat.completions.create(...)

# Context restored after block

Supported Providers

  • ✅ OpenAI (GPT-4, GPT-3.5, GPT-4o, etc.)
  • ✅ Anthropic (Claude 3 Opus, Sonnet, Haiku, etc.)
  • ✅ Google Generative AI (Gemini Pro, Gemini 1.5, etc.)

Security Model

What we intercept:

  • Response metadata (model name, token counts, latency)

What we NEVER see:

  • Your API keys
  • Request prompts
  • Response content

The SDK only reads response objects after your API call completes. All tracking happens locally before sending anonymized metrics.

How It Works

  1. patch_all() monkey-patches OpenAI and Anthropic clients
  2. When you make an API call, the SDK:
    • Starts a timer
    • Calls the original API method
    • Extracts tokens and latency from the response
    • Calculates cost using current pricing
    • Sends metrics asynchronously (non-blocking)
    • Returns the original response unchanged

Total overhead: < 1ms per API call.

Error Handling

Network failures or backend errors will never crash your code. All metric sending happens in a background thread with automatic retries.

# Even if Spend Hawk backend is down, this works fine
response = client.chat.completions.create(...)  # ✅ Never crashes

Testing

Run tests:

pip install pytest
pytest tests/

Examples

Basic usage with OpenAI

import spend_hawk
spend_hawk.patch_all()

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

With Anthropic

import spend_hawk
spend_hawk.patch_all()

from anthropic import Anthropic
client = Anthropic()
response = client.messages.create(
    model="claude-3-sonnet-20240229",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello"}]
)

With Google Generative AI

import spend_hawk
spend_hawk.patch_all()

import google.generativeai as genai
from google.generativeai import GenerativeModel

genai.configure(api_key="your-api-key")
model = GenerativeModel("gemini-pro")
response = model.generate_content("Hello, Gemini!")

Multi-agent system

import spend_hawk
spend_hawk.patch_all()

# Track different agents and providers
with spend_hawk.context(agent="research-agent"):
    research_response = openai_call()

with spend_hawk.context(agent="writing-agent"):
    writing_response = anthropic_call()

with spend_hawk.context(agent="brainstorm-agent"):
    brainstorm_response = google_call()

# Dashboard shows costs per agent and provider

License

MIT

Support

Metadata

Release files for spend-hawk-sdk 0.1.2

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

Source distribution (sdist)

Source distribution for spend-hawk-sdk 0.1.2
File Size Uploaded Provenance
spend_hawk_sdk-0.1.2.tar.gz 15.1 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for spend-hawk-sdk 0.1.2
File Interpreter ABI Platform
spend_hawk_sdk-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 29.5 kB

Release files / spend_hawk_sdk-0.1.2.tar.gz

Download URL spend_hawk_sdk-0.1.2.tar.gz
Size 15.1 kB
Tags Source
SHA-256 checksum
How to use checksums
05434f90406839538b5fa257b145d0706c94cfa8ed135f4b1733e186e4ede795
BLAKE2b-256 checksum
How to use checksums
c2d2baf1ef782b2edcecbb7ae3f1f9e1252178dde573f4b07f772863c7f1adb4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release files / spend_hawk_sdk-0.1.2-py3-none-any.whl

Download URL spend_hawk_sdk-0.1.2-py3-none-any.whl
Size 14.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6fa8ee81fa0d2361112817c6445fc79ac540a260ca07393d0d8de56438971ea9
BLAKE2b-256 checksum
How to use checksums
c9161c619877d6048b762142be2409895b6c73d738ced86ceb00c5c5d10c2ab0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Security for spend-hawk-sdk 0.1.2

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.

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