Skip to main content

Automatic LLM cost tracking for OpenAI and Anthropic

Project description

Spend Hawk SDK

Automatic LLM cost tracking for OpenAI and Anthropic.

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 and Anthropic)
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, etc.)
  • ✅ Anthropic (Claude 3 Opus, Sonnet, Haiku)

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

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"}]
)

Multi-agent system

import spend_hawk
spend_hawk.patch_all()

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

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

# Dashboard shows costs per agent

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.0.tar.gz (12.0 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.0-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: spend_hawk_sdk-0.1.0.tar.gz
  • Upload date:
  • Size: 12.0 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.0.tar.gz
Algorithm Hash digest
SHA256 ddba95c2dd021d53d1a20fbf286b48a3d00f2ceecd884012fcea51af92b805a2
MD5 c3d65de53ebc0708ce4b6c31bcf6543c
BLAKE2b-256 629d3138fb09ee92c30043fdb9c463c36b4334cf040b93ab2d48cbd57c6f5b6f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: spend_hawk_sdk-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.2 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bb09daae9df90e660a47e5f181c43517d34fb82ec0962819684e8a9a85ff8d4e
MD5 a4b679dd0fb307bef65e758097e10df1
BLAKE2b-256 7780af719be34e3e6745ed681e40b9b0f2f6ebb200af110e9cef5fcb3284e98c

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