Skip to main content

OpenTelemetry-based SDK for tracking and billing AI agent usage with Cost Guard protection

Project description

AgentBill Python SDK

OpenTelemetry-based SDK for automatically tracking and billing AI agent usage.

Installation

From PyPI (Recommended)

pip install agentbill-py-sdk

From PyPI (Coming Soon)

pip install agentbill

From Source

# Clone your repository and install
git clone https://github.com/yourusername/agentbill-python-sdk.git
cd agentbill-python-sdk
pip install -e .

File Structure

agentbill-python/
├── agentbill/
│   ├── __init__.py
│   ├── client.py
│   ├── tracer.py
│   └── types.py
├── examples/
│   ├── openai_basic.py
│   ├── anthropic_basic.py
│   ├── bedrock_basic.py
│   ├── azure_openai_basic.py
│   ├── mistral_basic.py
│   └── google_ai_basic.py
├── tests/
│   ├── test_agentbill.py
│   └── test_tracer.py
├── README.md
├── setup.py
├── pyproject.toml
├── pytest.ini
├── Makefile
├── CHANGELOG.md
├── CONTRIBUTING.md
├── SECURITY.md
└── LICENSE

Quick Start

from agentbill import AgentBill
import openai

# Initialize AgentBill
agentbill = AgentBill.init({
    "api_key": "your-api-key",
    "customer_id": "customer-123",
    "debug": True
})

# Wrap your OpenAI client
client = agentbill.wrap_openai(openai.OpenAI(
    api_key="sk-..."
))

# Use normally - all calls are automatically tracked!
response = client.chat.completions.create(
    model="gpt-4",
    messages=[{"role": "user", "content": "Hello!"}]
)

Features

  • ✅ Zero-config instrumentation
  • ✅ Accurate token & cost tracking
  • ✅ Multi-provider support (OpenAI, Anthropic, Bedrock, Azure OpenAI, Mistral, Google AI)
  • ✅ Rich metadata capture
  • ✅ OpenTelemetry-based

Supported Providers

  • OpenAI - GPT-4, GPT-5, Embeddings, DALL-E, Whisper, TTS
  • Anthropic - Claude Sonnet, Claude Opus
  • AWS Bedrock - Claude, Titan, and other Bedrock models
  • Azure OpenAI - GPT models via Azure
  • Mistral AI - Mistral models
  • Google AI - Gemini Pro, Gemini Flash

Provider Examples

OpenAI

from agentbill import AgentBill
import openai

agentbill = AgentBill.init({"api_key": "your-api-key"})
client = agentbill.wrap_openai(openai.OpenAI(api_key="sk-..."))

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

Anthropic

from agentbill import AgentBill
import anthropic

agentbill = AgentBill.init({"api_key": "your-api-key"})
client = agentbill.wrap_anthropic(anthropic.Anthropic(api_key="sk-ant-..."))

response = client.messages.create(
    model="claude-sonnet-4-20250514",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello!"}]
)

AWS Bedrock

from agentbill import AgentBill
import boto3

agentbill = AgentBill.init({"api_key": "your-api-key"})
bedrock = agentbill.wrap_bedrock(boto3.client('bedrock-runtime'))

response = bedrock.invoke_model(
    modelId='anthropic.claude-v2',
    body=json.dumps({
        "prompt": "Hello!",
        "max_tokens_to_sample": 300
    })
)

Azure OpenAI

from agentbill import AgentBill
from openai import AzureOpenAI

agentbill = AgentBill.init({"api_key": "your-api-key"})
client = agentbill.wrap_azure_openai(AzureOpenAI(
    api_key="your-azure-key",
    api_version="2024-02-01",
    azure_endpoint="https://your-resource.openai.azure.com"
))

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

Mistral AI

from agentbill import AgentBill
from mistralai import Mistral

agentbill = AgentBill.init({"api_key": "your-api-key"})
client = agentbill.wrap_mistral(Mistral(api_key="your-mistral-key"))

response = client.chat.complete(
    model="mistral-large-latest",
    messages=[{"role": "user", "content": "Hello!"}]
)

Google AI (Gemini)

from agentbill import AgentBill
import google.generativeai as genai

agentbill = AgentBill.init({"api_key": "your-api-key"})
genai.configure(api_key="your-google-key")
model = genai.GenerativeModel('gemini-pro')
wrapped_model = agentbill.wrap_google_ai(model)

response = wrapped_model.generate_content("Hello!")

Configuration

config = {
    "api_key": "your-api-key",        # Required
    "base_url": "https://...",         # Optional
    "customer_id": "customer-123",     # Optional
    "debug": True                       # Optional
}

agentbill = AgentBill.init(config)

Publishing to PyPI

Prerequisites

  1. Create a PyPI account at https://pypi.org/account/register/
  2. Generate an API token at https://pypi.org/manage/account/token/
  3. Create ~/.pypirc file with your token:
[pypi]
username = __token__
password = pypi-YOUR_API_TOKEN_HERE

Publishing Steps

# Build the package
python setup.py sdist bdist_wheel

# Upload to PyPI
pip install twine
twine upload dist/*

GitHub Repository Setup

  1. Create your GitHub repository (e.g., agentbill-python-sdk)
  2. Push all files (agentbill/, examples/, LICENSE, setup.py, README.md)
  3. Tag releases: git tag v1.0.0 && git push origin v1.0.0
  4. Users can install with: pip install agentbill-py-sdk

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

agentbill_py_sdk-7.6.1.tar.gz (43.5 kB view details)

Uploaded Source

Built Distribution

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

agentbill_py_sdk-7.6.1-py3-none-any.whl (47.9 kB view details)

Uploaded Python 3

File details

Details for the file agentbill_py_sdk-7.6.1.tar.gz.

File metadata

  • Download URL: agentbill_py_sdk-7.6.1.tar.gz
  • Upload date:
  • Size: 43.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for agentbill_py_sdk-7.6.1.tar.gz
Algorithm Hash digest
SHA256 e0ace2f5a0969ddb7d23fb8c1040a7b0103d5e08c5f952950ff2ca3f9bdbb4ad
MD5 8bbeb63a471d9eadd2c8a954596f8a62
BLAKE2b-256 c30d003c4bc4b1847f3360b5549ee8463a775218fef6d1790949ab9759d07fce

See more details on using hashes here.

File details

Details for the file agentbill_py_sdk-7.6.1-py3-none-any.whl.

File metadata

File hashes

Hashes for agentbill_py_sdk-7.6.1-py3-none-any.whl
Algorithm Hash digest
SHA256 eb7d6af4267a7666432c0a8732951b07cce762855c1e6e5a8b8e9ebe8b45fc45
MD5 b1680bf720a32f90b2884851c7f0becd
BLAKE2b-256 d8cbfdab69e47b982a839af0850a234fcfddcc0680bd8a70e060fb3bbe345e67

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