revenium-middleware-anthropic


Namerevenium-middleware-anthropic JSON
Version 0.2.1 PyPI version JSON
download
home_pageNone
SummaryA Python library that meters Anthropic usage to Revenium.
upload_time2025-03-22 01:51:48
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseGNU Lesser General Public License v3 or later (LGPLv3+)
keywords anthropic middleware logging token-usage metering revenium
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 🤖 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](https://github.com/revenium/revenium-middleware-anthropic/actions/workflows/ci.yml/badge.svg)](https://github.com/revenium/revenium-middleware-anthropic/actions))

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

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "revenium-middleware-anthropic",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "anthropic, middleware, logging, token-usage, metering, revenium",
    "author": null,
    "author_email": "Revenium <info@revenium.io>",
    "download_url": "https://files.pythonhosted.org/packages/de/b4/d30009c21267454cf2576f1a4d256fd794185ff0d8660a84cee7c472fd4c/revenium_middleware_anthropic-0.2.1.tar.gz",
    "platform": null,
    "description": "# \ud83e\udd16 Revenium Middleware for Anthropic\n\n[![PyPI version](https://img.shields.io/pypi/v/revenium-middleware-anthropic.svg)](https://pypi.org/project/revenium-middleware-anthropic/)\n[![Python Versions](https://img.shields.io/pypi/pyversions/revenium-middleware-anthropic.svg)](https://pypi.org/project/revenium-middleware-anthropic/)\n[![Documentation Status](https://readthedocs.org/projects/revenium-middleware-anthropic/badge/?version=latest)](https://revenium-middleware-anthropic.readthedocs.io/en/latest/?badge=latest)\n[![License: LGPL v3](https://img.shields.io/badge/License-LGPL%20v3-blue.svg)](https://www.gnu.org/licenses/lgpl-3.0)\n\n[//]: # ([![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;)\n\nA middleware library for metering and monitoring Anthropic API usage in Python applications. \ud83d\udc0d\u2728\n\n## \u2728 Features\n\n- **\ud83d\udcca Precise Usage Tracking**: Monitor tokens, costs, and request counts across all Anthropic API endpoints\n- **\ud83d\udd0c Seamless Integration**: Drop-in middleware that works with minimal code changes\n- **\u2699\ufe0f Flexible Configuration**: Customize metering behavior to suit your application needs\n\n## \ud83d\udce5 Installation\n\n```bash\npip install revenium-middleware-anthropic\n```\n\n## \ud83d\udd27 Usage\n\n### \ud83d\udd04 Zero-Config Integration\n\nSimply export your REVENIUM_METERING_API_KEY and import the middleware.\nYour Anthropic calls will be metered automatically:\n\n```python\nimport anthropic\nimport revenium_middleware_anthropic\n\nclient = anthropic.Anthropic()\n\nmessage = client.messages.create(\n    model=\"claude-3-7-sonnet-20250219\",\n    max_tokens=20000,\n    temperature=1,\n    messages=[\n        {\n            \"role\": \"user\",\n            \"content\": [\n                {\n                    \"type\": \"text\",\n                     \"text\": \"What is the meaning of life, the universe and everything?\",\n                }\n            ]\n        }\n    ]\n)\nprint(message.content)\n```\n\nThe middleware automatically intercepts Anthropic API calls and sends metering data to Revenium without requiring any\nchanges to your existing code. Make sure to set the `REVENIUM_METERING_API_KEY` environment variable for authentication\nwith the Revenium service.\n\n### \ud83d\udcc8 Enhanced Tracking with Metadata\n\nFor more granular usage tracking and detailed reporting, add the `usage_metadata` parameter:\n\n```python\nimport anthropic\nimport revenium_middleware_anthropic\n\nclient = anthropic.Anthropic()\n\nmessage = client.messages.create(\n    model=\"claude-3-7-sonnet-20250219\",\n    max_tokens=20000,\n    temperature=1,\n    messages=[\n        {\n            \"role\": \"user\",\n            \"content\": [\n                {\n                    \"type\": \"text\",\n                    \"text\": \"What is the meaning of life, the universe and everything?\",\n                }\n            ]\n        }\n    ],\n    usage_metadata={\n        \"trace_id\": \"conv-28a7e9d4-1c3b-4e5f-8a9b-7d1e3f2c1b4a\",\n        \"task_id\": \"chat-summary-af23c910\",\n        \"task_type\": \"text-classification\",\n        \"subscriber_identity\": \"customer-email@example.com\",\n        \"organization_id\": \"acme-corporation-12345\",\n        \"subscription_id\": \"startup-plan-quarterly-2025-Q1\",\n        \"product_id\": \"intelligent-document-processor-v3\",\n        \"source_id\": \"mobile-app-ios-v4.2\",\n        \"ai_provider_key_name\": \"anthropic-production-key1\",\n        \"agent\": \"customer-support-assistant-v2\",\n    }\n)\nprint(message.content)\n```\n\n#### \ud83c\udff7\ufe0f Metadata Fields\n\nThe `usage_metadata` parameter supports the following fields:\n\n| Field                  | Description                                     | Use Case                                                          |\n|------------------------|-------------------------------------------------|-------------------------------------------------------------------|\n| `trace_id`             | Unique identifier for a conversation or session | Track multi-turn conversations                                    |\n| `task_id`              | Identifier for a specific AI task               | Group related API calls for a single task                         |\n| `task_type`            | Classification of the AI operation              | Categorize usage by purpose (e.g., classification, summarization) |\n| `subscriber_identity`  | End-user identifier                             | Track usage by individual users                                   |\n| `organization_id`      | Customer or department identifier               | Allocate costs to business units                                  |\n| `subscription_id`      | Reference to a billing plan                     | Associate usage with specific subscriptions                       |\n| `product_id`           | The product or feature using AI                 | Track usage across different products                             |\n| `source_id`            | Origin of the request                           | Monitor usage by platform or app version                          |\n| `ai_provider_key_name` | Identifier for the API key used                 | Track usage by different API keys                                 |\n| `agent`                | Identifier for the specific AI agent            | Compare performance across different AI agents                    |\n\nAll metadata fields are optional. Adding them enables more detailed reporting and analytics in Revenium.\n\n## \ud83d\udd04 Compatibility\n\n- \ud83d\udc0d Python 3.8+\n- \ud83e\udd16 Anthropic Python SDK\n\n## \ud83d\udcda Documentation\n\nFull documentation is available\nat [https://revenium-middleware-anthropic.readthedocs.io/](https://revenium-middleware-anthropic.readthedocs.io/)\n\n## \ud83d\udc65 Contributing\n\nContributions are welcome! Please check out our [contributing guidelines](CONTRIBUTING.md) for details.\n\n1. \ud83c\udf74 Fork the repository\n2. \ud83c\udf3f Create your feature branch (`git checkout -b feature/amazing-feature`)\n3. \ud83d\udcbe Commit your changes (`git commit -m 'Add some amazing feature'`)\n4. \ud83d\ude80 Push to the branch (`git push origin feature/amazing-feature`)\n5. \ud83d\udd0d Open a Pull Request\n\n## \ud83d\udcc4 License\n\nThis project is licensed under the GNU Lesser General Public License v3.0 (LGPL-3.0) - see the [LICENSE](LICENSE) file\nfor details.\n\n## \ud83d\ude4f Acknowledgments\n\n- \ud83d\udd25 Thanks to the Anthropic team for creating an excellent API\n- \ud83d\udc96 Built with \u2764\ufe0f by the Revenium team\n",
    "bugtrack_url": null,
    "license": "GNU Lesser General Public License v3 or later (LGPLv3+)",
    "summary": "A Python library that meters Anthropic usage to Revenium.",
    "version": "0.2.1",
    "project_urls": {
        "Bug Tracker": "https://github.com/revenium/revenium-middleware-anthropic-python/issues",
        "Documentation": "https://github.com/revenium/revenium-middleware-anthropic-python/blob/main/README.md",
        "Homepage": "https://github.com/revenium/revenium-middleware-anthropic-python"
    },
    "split_keywords": [
        "anthropic",
        " middleware",
        " logging",
        " token-usage",
        " metering",
        " revenium"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "cadfa5c94d5d4d8dcaef03de4eca1b01b83b65b8c998eed27dc87739827d3a45",
                "md5": "397a82b411f38d61211b515e8e262078",
                "sha256": "ab35e3055cffadd3e7d225b64e55f006f1c57ed0b5b5cecf03a3b18c098e022d"
            },
            "downloads": -1,
            "filename": "revenium_middleware_anthropic-0.2.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "397a82b411f38d61211b515e8e262078",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 5809,
            "upload_time": "2025-03-22T01:51:46",
            "upload_time_iso_8601": "2025-03-22T01:51:46.990811Z",
            "url": "https://files.pythonhosted.org/packages/ca/df/a5c94d5d4d8dcaef03de4eca1b01b83b65b8c998eed27dc87739827d3a45/revenium_middleware_anthropic-0.2.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "deb4d30009c21267454cf2576f1a4d256fd794185ff0d8660a84cee7c472fd4c",
                "md5": "6f28c76c38541423bbf3b92875d132fa",
                "sha256": "3b38fec87ff17098ae69bcc45647ebbbcfedc7571cd0232bf0b2f9f8a1cd6935"
            },
            "downloads": -1,
            "filename": "revenium_middleware_anthropic-0.2.1.tar.gz",
            "has_sig": false,
            "md5_digest": "6f28c76c38541423bbf3b92875d132fa",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 7675,
            "upload_time": "2025-03-22T01:51:48",
            "upload_time_iso_8601": "2025-03-22T01:51:48.001944Z",
            "url": "https://files.pythonhosted.org/packages/de/b4/d30009c21267454cf2576f1a4d256fd794185ff0d8660a84cee7c472fd4c/revenium_middleware_anthropic-0.2.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-03-22 01:51:48",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "revenium",
    "github_project": "revenium-middleware-anthropic-python",
    "github_not_found": true,
    "lcname": "revenium-middleware-anthropic"
}
        
Elapsed time: 0.50249s