Skip to main content

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

Project description

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

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

spend_hawk_sdk-0.1.1.tar.gz (13.8 kB view details)

Uploaded Source

Built Distribution

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

spend_hawk_sdk-0.1.1-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

Details for the file spend_hawk_sdk-0.1.1.tar.gz.

File metadata

  • Download URL: spend_hawk_sdk-0.1.1.tar.gz
  • Upload date:
  • Size: 13.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for spend_hawk_sdk-0.1.1.tar.gz
Algorithm Hash digest
SHA256 cb031379c237c913df854c0f77e6dd0db0c2e9a727bfa13eee2b9ab4ab526a7a
MD5 6fabf51102bcbcd249c9642b4ad6c803
BLAKE2b-256 6ee4394f14a6336b54eb36389c46643c6b09292356bbd055aeb1d8171ec2006e

See more details on using hashes here.

File details

Details for the file spend_hawk_sdk-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: spend_hawk_sdk-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 12.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for spend_hawk_sdk-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f94cb33b01ecdf437d6355a485172150c499d0062ebcc36844c063ba1e6e9e94
MD5 052987c6685d58d765cc0bb1d66f7ca0
BLAKE2b-256 bdf18f67f9a245d4206490a95fe07103a964822ce3e4de27f5a9dcc2a66ba648

See more details on using hashes here.

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