Skip to main content

A Python library that meters Anthropic usage to Revenium.

Project description

Revenium Middleware for Anthropic

PyPI version Python Versions Documentation License: MIT

A middleware library for metering and monitoring Anthropic API usage in Python applications.

A middleware library for metering and monitoring Anthropic API usage in Python applications.

Features

  • ** Precise Usage Tracking**: Monitor tokens, costs, and request counts across all Anthropic API endpoints
  • ** Seamless Integration**: Drop-in middleware that works with minimal code changes
  • ️ Flexible Configuration: Customize metering behavior to suit your application needs

Installation

pip install revenium-middleware-anthropic

Usage

Zero-Config Integration

Simply export your REVENIUM_METERING_API_KEY and import the middleware. Your Anthropic calls will be metered automatically:

import anthropic
import revenium_middleware_anthropic

client = anthropic.Anthropic()

message = client.messages.create(
    model="claude-3-7-sonnet-20250219",
    max_tokens=20000,
    temperature=1,
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                     "text": "What is the meaning of life, the universe and everything?",
                }
            ]
        }
    ]
)
print(message.content)

The middleware automatically intercepts Anthropic API calls and sends metering data to Revenium without requiring any changes to your existing code. Make sure to set the REVENIUM_METERING_API_KEY environment variable for authentication with the Revenium service.

Enhanced Tracking with Metadata

For more granular usage tracking and detailed reporting, add the usage_metadata parameter:

import anthropic
import revenium_middleware_anthropic

client = anthropic.Anthropic()

message = client.messages.create(
    model="claude-3-7-sonnet-20250219",
    max_tokens=20000,
    temperature=1,
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": "What is the meaning of life, the universe and everything?",
                }
            ]
        }
    ],
    usage_metadata={
         "trace_id": "conv-28a7e9d4",
         "task_type": "summarize-customer-issue",
         "subscriber": {
             "id": "subscriberid-1234567890",
             "email": "user@example.com",
             "credential": {
                 "name": "engineering-api-key",
                 "value": "sk-ant-api03-..."
             }
         },
         "organization_id": "acme-corp",
         "subscription_id": "startup-plan-Q1",
         "product_id": "saas-app-gold-tier",
         "agent": "support-agent",
    }
)
print(message.content)

Streaming Support

The middleware also supports Anthropic's streaming API. For streaming responses, use the usage_metadata parameter to provide metadata for analytics. The middleware will automatically track token usage and send metering data when the stream completes.

Example Code

View the examples directory for more code samples for both streaming and non-streaming AI calls.

️ Metadata Fields

The usage_metadata parameter supports the following fields:

Field Description Use Case
trace_id Unique identifier for a conversation or session Group multi-turn conversations into single event for performance & cost tracking
task_type Classification of the AI operation by type of work Track cost & performance by purpose (e.g., classification, summarization)
subscriber Object containing subscriber information Track cost & performance by individual users and their credentials
subscriber.id The id of the subscriber from non-Revenium systems Track cost & performance by individual users (if customers are anonymous or tracking by emails is not desired)
subscriber.email The email address of the subscriber Track cost & performance by individual users (if customer e-mail addresses are known)
subscriber.credential Object containing credential information Track cost & performance by API keys and credentials
subscriber.credential.name An alias for an API key used by one or more users Track cost & performance by individual API keys
subscriber.credential.value The key value associated with the subscriber (i.e an API key) Track cost & performance by API key value (normally used when the only identifier for a user is an API key)
organization_id Customer or department ID from non-Revenium systems Track cost & performance by customers or business units
subscription_id Reference to a billing plan in non-Revenium systems Track cost & performance by a specific subscription
product_id Your product or feature making the AI call Track cost & performance across different products
agent Identifier for the specific AI agent Track cost & performance performance by AI agent
response_quality_score The quality of the AI response (0..1) Track AI response quality

All metadata fields are optional. Adding them enables more detailed reporting and analytics in Revenium.

Compatibility

  • Python 3.8+
  • Anthropic Python SDK

Logging

This module uses Python's standard logging system. You can control the log level by setting the REVENIUM_LOG_LEVEL environment variable:

# Enable debug logging
export REVENIUM_LOG_LEVEL=DEBUG

# Or when running your script
REVENIUM_LOG_LEVEL=DEBUG python your_script.py

Available log levels:

  • DEBUG: Detailed debugging information
  • INFO: General information (default)
  • WARNING: Warning messages only
  • ERROR: Error messages only
  • CRITICAL: Critical error messages only

Contributing

See CONTRIBUTING.md

Security

See SECURITY.md

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • Built by the Revenium team

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

revenium_middleware_anthropic-0.2.23.tar.gz (13.4 kB view details)

Uploaded Source

Built Distribution

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

File details

Details for the file revenium_middleware_anthropic-0.2.23.tar.gz.

File metadata

File hashes

Hashes for revenium_middleware_anthropic-0.2.23.tar.gz
Algorithm Hash digest
SHA256 8a1b5eaf687f3e7b9d7b85311665835e8e35451d30f47b39a0e94220606840da
MD5 0d1c5d2c648b6a0574124c36419b7e79
BLAKE2b-256 881a5000e0f635c71387ec8027f81809d06726cba7649193fa105d277c1669ac

See more details on using hashes here.

File details

Details for the file revenium_middleware_anthropic-0.2.23-py3-none-any.whl.

File metadata

File hashes

Hashes for revenium_middleware_anthropic-0.2.23-py3-none-any.whl
Algorithm Hash digest
SHA256 0cf02ee150b0fbe451240f694297b61e90bab49a9abfe9845894c08576b5ec73
MD5 b23d71059c53b8d23ce86d5f0b4e90ce
BLAKE2b-256 064d239977c56bb6bcae01b4502d3eb9c0c3b8b4973bd513e50e3fb2313d5f8d

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