Skip to main content
Yanked

This release has been yanked by its maintainers, and will be ignored by installers, except when explicitly specified.
Reason given by maintainers: Superseded by revenium-python-sdk. Migrate with: pip install revenium-python-sdk

This package is no longer maintained. Its functionality is included in revenium-python-sdk, which is the supported package for new and existing work:

pip install revenium-python-sdk

Note: both packages provide the same top-level revenium_middleware import, so installing them side by side is not supported. Existing installations continue to work.

Revenium Core Middleware

PyPI version Python Versions Documentation License: MIT

A foundational library that provides core metering functionality shared across all Revenium AI provider-specific middleware implementations (OpenAI, Anthropic, Ollama, etc).

Features

  • ** Shared Core Functionality**: Provides the essential metering infrastructure used by all Revenium middleware implementations
  • ** Asynchronous Processing**: Background thread management for non-blocking metering operations
  • ** Graceful Shutdown**: Ensures all metering data is properly sent even during application shutdown
  • ** Provider Agnostic**: Designed to work with any AI provider through specific middleware implementations

Installation

pip install revenium-middleware

Usage

Direct Usage

While this package is primarily intended as a dependency for provider-specific middleware, you can use it directly:

from revenium_middleware import client, run_async_in_thread, shutdown_event

# Record usage directly
client.record_usage(
    model="gpt-4o",
    prompt_tokens=500,
    completion_tokens=200,
    user_id="user123",
    session_id="session456"
)

# Run async metering tasks in background threads
async def async_metering_task():
    await client.async_record_usage(
        model="gpt-3.5-turbo",
        prompt_tokens=300,
        completion_tokens=150,
        user_id="user789"
    )

thread = run_async_in_thread(async_metering_task())

# Application continues while metering happens in background

️ Building Provider-Specific Middleware

This library is designed to be extended by provider-specific middleware implementations:

from revenium_middleware import client, run_async_in_thread

# Example of how a provider-specific middleware might use the core
def record_provider_usage(response_data, metadata):
    # Extract token counts from provider-specific response format
    prompt_tokens = response_data.usage.prompt_tokens
    completion_tokens = response_data.usage.completion_tokens
    
    # Use the core client to record the usage
    run_async_in_thread(
        client.async_record_usage(
            model=response_data.model,
            prompt_tokens=prompt_tokens,
            completion_tokens=completion_tokens,
            **metadata
        )
    )

Tool Metering

The meter_tool decorator lets you meter arbitrary tool/function calls (web scrapers, image generators, database lookups, etc.) alongside your LLM API metering. This is available via revenium_metering v6.8.2+.

from revenium_middleware import meter_tool, configure

# Configure the metering client
configure(
    metering_url="https://api.revenium.io/meter",
    api_key="your-api-key",
)

# Decorate any tool function to automatically meter it
@meter_tool("my-web-scraper", operation="scrape")
def scrape_website(url):
    # Your scraping logic here
    return {"pages": 5, "data_mb": 2.3}

# The decorator captures timing, success/failure, and reports to Revenium
result = scrape_website("https://example.com")

You can also report tool calls manually:

from revenium_middleware import report_tool_call

report_tool_call(
    tool_id="my-tool",
    operation="fetch",
    duration_ms=1234,
    success=True,
    usage_metadata={"records": 42},
)

Compatibility

  • Python 3.8+
  • Compatible with all Revenium provider-specific middleware implementations

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

Documentation

For detailed documentation, visit docs.revenium.io

Contributing

See CONTRIBUTING.md

Code of Conduct

See CODE_OF_CONDUCT.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

Metadata

Release files for revenium-middleware 0.4.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for revenium-middleware 0.4.4
File Size Uploaded Provenance Actions
revenium_middleware-0.4.4.tar.gz 15.3 kB Without provenance Details for revenium_middleware-0.4.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for revenium-middleware 0.4.4
File Interpreter ABI Platform Actions
revenium_middleware-0.4.4-py3-none-any.whl Python 3 none any Details for revenium_middleware-0.4.4-py3-none-any.whl

Total release size: 29.0 kB

Release files / revenium_middleware-0.4.4.tar.gz

Download URL revenium_middleware-0.4.4.tar.gz
Size 15.3 kB
Tags Source
SHA-256 checksum
How to use checksums
e1ad0b200b8da0b08c186fd63994715e268c6d92dad805b593b86fd7cb4c97f4
BLAKE2b-256 checksum
How to use checksums
72cafbb946530597c1cf17e9fee44326be1975340bf6c44152daf901e82f7972
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.5

Release files / revenium_middleware-0.4.4-py3-none-any.whl

Download URL revenium_middleware-0.4.4-py3-none-any.whl
Size 13.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1b630a14a48a2f2017487b9d8dba17ed1478ec54b50e0f9ab68580995573244b
BLAKE2b-256 checksum
How to use checksums
64c8dc78d083b10c1ef823f001cd6bcf7bdfa80917ecbd6a341d095b57f8842a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.5

Security for revenium-middleware 0.4.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.4.4 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page