# 🤖 Revenium Middleware for OpenAI
[](https://pypi.org/project/revenium-middleware-openai/)
[](https://pypi.org/project/revenium-middleware-openai/)
[](https://revenium-middleware-openai.readthedocs.io/en/latest/?badge=latest)
[](https://www.gnu.org/licenses/lgpl-3.0)
[//]: # ([](https://github.com/revenium/revenium-middleware-openai/actions))
A production-grade middleware library for metering and monitoring OpenAI API usage in Python applications. 🐍✨
## ✨ Features
- **📊 Precise Usage Tracking**: Monitor tokens, costs, and request counts across all OpenAI 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-openai
```
## 🔧 Usage
### 🔄 Zero-Config Integration
Simply export your REVENIUM_METERING_API_KEY and import the middleware.
Your OpenAI calls will be metered automatically:
```python
import openai
import revenium_middleware_openai
# Ensure REVENIUM_METERING_API_KEY environment variable is set
response = openai.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{
"role": "user",
"content": "What is the answer to life, the universe and everything?",
},
],
max_tokens=500,
)
print(response.choices[0].message.content)
```
The middleware automatically intercepts OpenAI 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 openai
import revenium_middleware_openai
response = openai.chat.completions.create(
model="gpt-4", # You can change this to other models like "gpt-3.5-turbo"
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{
"role": "user",
"content": "What is the meaning of life, the universe and everything?",
},
],
max_tokens=500,
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": "openai-production-key1",
"agent": "customer-support-assistant-v2",
},
)
print(response.choices[0].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+
- 🤖 OpenAI Python SDK 1.0.0+
- 🌐 Works with all OpenAI models and endpoints
## 📚 Documentation
Full documentation is available at [https://revenium-middleware-openai.readthedocs.io/](https://revenium-middleware-openai.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 OpenAI team for creating an excellent API
- 💖 Built with ❤️ by the Revenium team
Raw data
{
"_id": null,
"home_page": null,
"name": "revenium-middleware-openai",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": null,
"keywords": "openai, middleware, logging, token-usage, metering, revenium",
"author": null,
"author_email": "Revenium <info@revenium.io>",
"download_url": "https://files.pythonhosted.org/packages/31/eb/1a3a181d5219c27e25b565d63f90050e60650bbecc6353e97761f5ab882a/revenium_middleware_openai-0.2.0.tar.gz",
"platform": null,
"description": "# \ud83e\udd16 Revenium Middleware for OpenAI\n\n[](https://pypi.org/project/revenium-middleware-openai/)\n[](https://pypi.org/project/revenium-middleware-openai/)\n[](https://revenium-middleware-openai.readthedocs.io/en/latest/?badge=latest)\n[](https://www.gnu.org/licenses/lgpl-3.0)\n\n[//]: # ([](https://github.com/revenium/revenium-middleware-openai/actions))\n\nA production-grade middleware library for metering and monitoring OpenAI 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 OpenAI 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\n## \ud83d\udce5 Installation\n\n```bash\npip install revenium-middleware-openai\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 OpenAI calls will be metered automatically:\n\n```python\nimport openai\nimport revenium_middleware_openai\n\n# Ensure REVENIUM_METERING_API_KEY environment variable is set\n\nresponse = openai.chat.completions.create(\n model=\"gpt-4\", \n messages=[\n {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n {\n \"role\": \"user\",\n \"content\": \"What is the answer to life, the universe and everything?\",\n },\n ],\n max_tokens=500,\n)\n\nprint(response.choices[0].message.content)\n```\n\nThe middleware automatically intercepts OpenAI 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.\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 openai\nimport revenium_middleware_openai\n\nresponse = openai.chat.completions.create(\n model=\"gpt-4\", # You can change this to other models like \"gpt-3.5-turbo\"\n messages=[\n {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n {\n \"role\": \"user\",\n \"content\": \"What is the meaning of life, the universe and everything?\",\n },\n ],\n max_tokens=500,\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\": \"openai-production-key1\",\n \"agent\": \"customer-support-assistant-v2\",\n },\n)\nprint(response.choices[0].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 OpenAI Python SDK 1.0.0+\n- \ud83c\udf10 Works with all OpenAI models and endpoints\n\n## \ud83d\udcda Documentation\n\nFull documentation is available at [https://revenium-middleware-openai.readthedocs.io/](https://revenium-middleware-openai.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 for details.\n\n## \ud83d\ude4f Acknowledgments\n\n- \ud83d\udd25 Thanks to the OpenAI 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 OpenAI usage to Revenium.",
"version": "0.2.0",
"project_urls": {
"Bug Tracker": "https://github.com/revenium/revenium-middleware-openai-python/issues",
"Documentation": "https://github.com/revenium/revenium-middleware-openai-python/blob/main/README.md",
"Homepage": "https://github.com/revenium/revenium-middleware-openai-python"
},
"split_keywords": [
"openai",
" middleware",
" logging",
" token-usage",
" metering",
" revenium"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "75df6771a381272af7122cd810dd64c1907a8ed1cef20f2c20d414f5047c9ade",
"md5": "3e0c576fe6b284fa43c8225b57b725e0",
"sha256": "e43ffa92cb925d5640009745488f2fa529cf9be1b2829d6ceb01391360ce4e2d"
},
"downloads": -1,
"filename": "revenium_middleware_openai-0.2.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "3e0c576fe6b284fa43c8225b57b725e0",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 5796,
"upload_time": "2025-03-22T01:53:07",
"upload_time_iso_8601": "2025-03-22T01:53:07.074831Z",
"url": "https://files.pythonhosted.org/packages/75/df/6771a381272af7122cd810dd64c1907a8ed1cef20f2c20d414f5047c9ade/revenium_middleware_openai-0.2.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "31eb1a3a181d5219c27e25b565d63f90050e60650bbecc6353e97761f5ab882a",
"md5": "f3b32114729efbb0df983543b2868211",
"sha256": "9ea247ceb89fa616bc5b8e606c97fe8392b8ca2eb1a46754c46c46cd7decf14f"
},
"downloads": -1,
"filename": "revenium_middleware_openai-0.2.0.tar.gz",
"has_sig": false,
"md5_digest": "f3b32114729efbb0df983543b2868211",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 7484,
"upload_time": "2025-03-22T01:53:07",
"upload_time_iso_8601": "2025-03-22T01:53:07.964204Z",
"url": "https://files.pythonhosted.org/packages/31/eb/1a3a181d5219c27e25b565d63f90050e60650bbecc6353e97761f5ab882a/revenium_middleware_openai-0.2.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-03-22 01:53:07",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "revenium",
"github_project": "revenium-middleware-openai-python",
"github_not_found": true,
"lcname": "revenium-middleware-openai"
}