Metadata-Version: 2.4
Name: revenium-middleware-anthropic
Version: 0.2.2
Summary: A Python library that meters Anthropic usage to Revenium.
Author-email: Revenium <info@revenium.io>
License: GNU Lesser General Public License v3 or later (LGPLv3+)
Project-URL: Homepage, https://github.com/revenium/revenium-middleware-anthropic-python
Project-URL: Bug Tracker, https://github.com/revenium/revenium-middleware-anthropic-python/issues
Project-URL: Documentation, https://github.com/revenium/revenium-middleware-anthropic-python/blob/main/README.md
Keywords: anthropic,middleware,logging,token-usage,metering,revenium
Classifier: Development Status :: 4 - Beta
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: License :: OSI Approved :: GNU Lesser General Public License v3 or later (LGPLv3+)
Classifier: Operating System :: OS Independent
Classifier: Intended Audience :: Developers
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.8
Description-Content-Type: text/markdown
Requires-Dist: wrapt
Requires-Dist: anthropic
Requires-Dist: revenium_middleware_core
Provides-Extra: dev
Requires-Dist: pytest>=7.0.0; extra == "dev"
Requires-Dist: pytest-cov; extra == "dev"
Requires-Dist: flake8; extra == "dev"
Requires-Dist: black; extra == "dev"
Requires-Dist: mypy; extra == "dev"
Requires-Dist: freezegun; extra == "dev"

# 🤖 Revenium Middleware for Anthropic

[![PyPI version](https://img.shields.io/pypi/v/revenium-middleware-anthropic.svg)](https://pypi.org/project/revenium-middleware-anthropic/)
[![Python Versions](https://img.shields.io/pypi/pyversions/revenium-middleware-anthropic.svg)](https://pypi.org/project/revenium-middleware-anthropic/)
[![Documentation Status](https://readthedocs.org/projects/revenium-middleware-anthropic/badge/?version=latest)](https://revenium-middleware-anthropic.readthedocs.io/en/latest/?badge=latest)
[![License: LGPL v3](https://img.shields.io/badge/License-LGPL%20v3-blue.svg)](https://www.gnu.org/licenses/lgpl-3.0)

[//]: # ([![Build Status]&#40;https://github.com/revenium/revenium-middleware-anthropic/actions/workflows/ci.yml/badge.svg&#41;]&#40;https://github.com/revenium/revenium-middleware-anthropic/actions&#41;)

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

```bash
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:

```python
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:

```python
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-1c3b-4e5f-8a9b-7d1e3f2c1b4a",
        "task_id": "chat-summary-af23c910",
        "task_type": "text-classification",
        "subscriber_identity": "customer-email@example.com",
        "organization_id": "acme-corporation-12345",
        "subscription_id": "startup-plan-quarterly-2025-Q1",
        "product_id": "intelligent-document-processor-v3",
        "source_id": "mobile-app-ios-v4.2",
        "ai_provider_key_name": "anthropic-production-key1",
        "agent": "customer-support-assistant-v2",
    }
)
print(message.content)
```

#### 🏷️ Metadata Fields

The `usage_metadata` parameter supports the following fields:

| Field                  | Description                                     | Use Case                                                          |
|------------------------|-------------------------------------------------|-------------------------------------------------------------------|
| `trace_id`             | Unique identifier for a conversation or session | Track multi-turn conversations                                    |
| `task_id`              | Identifier for a specific AI task               | Group related API calls for a single task                         |
| `task_type`            | Classification of the AI operation              | Categorize usage by purpose (e.g., classification, summarization) |
| `subscriber_identity`  | End-user identifier                             | Track usage by individual users                                   |
| `organization_id`      | Customer or department identifier               | Allocate costs to business units                                  |
| `subscription_id`      | Reference to a billing plan                     | Associate usage with specific subscriptions                       |
| `product_id`           | The product or feature using AI                 | Track usage across different products                             |
| `source_id`            | Origin of the request                           | Monitor usage by platform or app version                          |
| `ai_provider_key_name` | Identifier for the API key used                 | Track usage by different API keys                                 |
| `agent`                | Identifier for the specific AI agent            | Compare performance across different AI agents                    |

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

## 🔄 Compatibility

- 🐍 Python 3.8+
- 🤖 Anthropic Python SDK

## 📚 Documentation

Full documentation is available
at [https://revenium-middleware-anthropic.readthedocs.io/](https://revenium-middleware-anthropic.readthedocs.io/)

## 👥 Contributing

Contributions are welcome! Please check out our [contributing guidelines](CONTRIBUTING.md) for details.

1. 🍴 Fork the repository
2. 🌿 Create your feature branch (`git checkout -b feature/amazing-feature`)
3. 💾 Commit your changes (`git commit -m 'Add some amazing feature'`)
4. 🚀 Push to the branch (`git push origin feature/amazing-feature`)
5. 🔍 Open a Pull Request

## 📄 License

This project is licensed under the GNU Lesser General Public License v3.0 (LGPL-3.0) - see the [LICENSE](LICENSE) file
for details.

## 🙏 Acknowledgments

- 🔥 Thanks to the Anthropic team for creating an excellent API
- 💖 Built with ❤️ by the Revenium team
