### Install Revenium SDK via pip Source: https://github.com/revenium/revenium-python-sdk/blob/main/README.md Commands to install the core Revenium SDK and optional provider-specific dependencies. ```bash # Core SDK pip install revenium-python-sdk # With a specific provider pip install revenium-python-sdk[openai] # Multiple providers pip install revenium-python-sdk[openai,anthropic,ollama] ``` -------------------------------- ### Install Revenium Python SDK via Pip Source: https://context7.com/revenium/revenium-python-sdk/llms.txt Commands to install the core Revenium SDK and specific provider-based dependencies using pip. ```bash # Core SDK pip install revenium-python-sdk # With specific providers pip install revenium-python-sdk[openai] pip install revenium-python-sdk[anthropic] pip install revenium-python-sdk[google-genai] pip install revenium-python-sdk[google-vertex] pip install revenium-python-sdk[ollama] pip install revenium-python-sdk[litellm] pip install revenium-python-sdk[perplexity-openai] # Multiple providers pip install revenium-python-sdk[openai,anthropic,ollama] ``` -------------------------------- ### Track OpenAI Embeddings Source: https://context7.com/revenium/revenium-python-sdk/llms.txt Example of initializing the OpenAI client with Revenium middleware to track embedding API calls. ```python import os from dotenv import load_dotenv from openai import OpenAI load_dotenv() import revenium_middleware.openai.middleware client = OpenAI() ``` -------------------------------- ### Anthropic API Calls with Python SDK Source: https://context7.com/revenium/revenium-python-sdk/llms.txt Demonstrates making API calls to Anthropic's Claude models using the Revenium Python SDK. It includes examples for basic requests and requests with detailed usage metadata for advanced reporting. ```python from dotenv import load_dotenv load_dotenv() import anthropic import revenium_middleware.anthropic client = anthropic.Anthropic() # Basic request - automatically tracked message = client.messages.create( model="claude-3-haiku-20240307", max_tokens=100, messages=[{"role": "user", "content": "Please verify you are ready to assist me."}] ) print(f"Response: {message.content[0].text}") # Request with metadata for advanced reporting message = client.messages.create( model="claude-3-haiku-20240307", max_tokens=100, messages=[{"role": "user", "content": "Explain machine learning briefly." }], usage_metadata={ "subscriber": { "id": "user-123", "email": "user@example.com", "credential": {"name": "api-key-prod", "value": "key-abc-123"} }, "organizationName": "AcmeCorp", "subscription_id": "plan-enterprise-2024", "productName": "customer-chatbot", "task_type": "doc-summary", "agent": "customer-support", "trace_id": "session-abc123", "response_quality_score": 0.95 } ) print(f"Response: {message.content[0].text}") ``` -------------------------------- ### Integrate Anthropic with AWS Bedrock Source: https://context7.com/revenium/revenium-python-sdk/llms.txt Configures the Anthropic Python client to route requests through AWS Bedrock. It includes examples for basic requests and streaming with custom metadata context. ```python import os from dotenv import load_dotenv import anthropic import revenium_middleware.anthropic from revenium_middleware.anthropic import usage_context load_dotenv() aws_region = os.getenv("AWS_REGION", "us-east-1") bedrock_base_url = f"https://bedrock-runtime.{aws_region}.amazonaws.com" client = anthropic.Anthropic(base_url=bedrock_base_url) response = client.messages.create( model="claude-3-haiku-20240307", messages=[{"role": "user", "content": "Hello! What is AI?"}], max_tokens=50 ) usage_context.set({"trace_id": "bedrock-streaming-demo-001", "organizationName": "AcmeCorp"}) with client.messages.stream(model="claude-3-haiku-20240307", messages=[{"role": "user", "content": "Count from 1 to 5"}], max_tokens=50) as stream: for text in stream.text_stream: print(text, end="", flush=True) ``` -------------------------------- ### Configure SDK via Environment Variables Source: https://context7.com/revenium/revenium-python-sdk/llms.txt Lists the required and optional environment variables for configuring the Revenium SDK, including API keys and provider-specific settings for OpenAI, Azure, and AWS. ```bash export REVENIUM_METERING_API_KEY="hak_your_revenium_key_here" export REVENIUM_METERING_BASE_URL="https://api.revenium.ai" export REVENIUM_SELECTIVE_METERING=true export REVENIUM_LOG_LEVEL=DEBUG export OPENAI_API_KEY="sk-..." export AZURE_OPENAI_ENDPOINT="https://your-resource.openai.azure.com/" export AWS_REGION="us-east-1" export OLLAMA_HOST="http://localhost:11434" ``` -------------------------------- ### Configure LiteLLM Middleware for Automatic Metering Source: https://context7.com/revenium/revenium-python-sdk/llms.txt Shows how to import the Revenium middleware before initializing LiteLLM to enable automatic usage tracking. Supports both basic completions and enhanced analytics via the usage_metadata parameter. ```python import os from dotenv import load_dotenv load_dotenv() # Import middleware BEFORE litellm import revenium_middleware.litellm.client.middleware import litellm # Configure LiteLLM proxy proxy_url = os.getenv("LITELLM_PROXY_URL") proxy_key = os.getenv("LITELLM_API_KEY") litellm.api_base = proxy_url litellm.api_key = proxy_key # Basic completion - automatically metered response = litellm.completion( model="gpt-4o-mini", messages=[{"role": "user", "content": "Say 'Hello from Revenium!' in one line."}] ) print(f"Response: {response.choices[0].message.content}") # Completion with metadata for enhanced analytics response = litellm.completion( model="gpt-4o-mini", messages=[{"role": "user", "content": "What is 2 + 2?"}], usage_metadata={ "organizationName": "AcmeCorp", "subscription_id": "premium-plan", "productName": "customer-chatbot", "agent": "math-helper", "task_type": "calculation", "trace_id": "session-001", "subscriber": {"id": "user-123", "email": "user@example.com"} } ) print(f"Response: {response.choices[0].message.content}") ``` -------------------------------- ### Record AI Usage and Async Metering Source: https://github.com/revenium/revenium-python-sdk/blob/main/README.md Demonstrates how to record AI usage metrics synchronously and how to offload metering tasks to background threads for non-blocking execution. ```python 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()) ``` -------------------------------- ### Create Embeddings with Python SDK Source: https://context7.com/revenium/revenium-python-sdk/llms.txt Demonstrates creating embeddings using the Revenium Python SDK. It shows basic embedding creation and how to include usage metadata for batch tracking, which is useful for detailed analytics. ```python from openai import OpenAI client = OpenAI() # Basic embeddings - automatically tracked embeddings = client.embeddings.create( model="text-embedding-3-small", input=[ "First document for batch processing", "Second document for batch processing", "Third document for batch processing" ] ) print(f"Model: {embeddings.model}") print(f"Usage: {embeddings.usage}") print(f"Embeddings count: {len(embeddings.data)}") # Embeddings with metadata for batch tracking embeddings = client.embeddings.create( model="text-embedding-3-small", input=[ "Document 1: Streaming responses provide real-time feedback", "Document 2: Metadata enables rich business analytics", "Document 3: Batch processing improves efficiency" ], usage_metadata={ "subscriber": {"id": "batch-processor-123"}, "organizationName": "AcmeCorp", "task_type": "batch-document-embedding", "productName": "customer-chatbot", "trace_id": "batch-12345", "agent": "document-processor" } ) ``` -------------------------------- ### Configure Logging Levels Source: https://github.com/revenium/revenium-python-sdk/blob/main/README.md Demonstrates how to adjust the verbosity of the SDK's internal logging using environment variables. ```bash # Enable debug logging export REVENIUM_LOG_LEVEL=DEBUG # Or when running your script REVENIUM_LOG_LEVEL=DEBUG python your_script.py ``` -------------------------------- ### Instrument OpenAI Chat Completions Source: https://context7.com/revenium/revenium-python-sdk/llms.txt Demonstrates how to import the Revenium middleware to automatically track OpenAI chat completion requests, including support for custom business metadata. ```python import os from dotenv import load_dotenv from openai import OpenAI load_dotenv() # Import middleware to enable automatic tracking import revenium_middleware.openai.middleware client = OpenAI() # Basic request - automatically tracked response = client.chat.completions.create( model="gpt-4o-mini", messages=[ {"role": "user", "content": "Hello! Introduce yourself in one sentence."} ] ) print(response.choices[0].message.content) # Request with rich metadata for analytics response = client.chat.completions.create( model="gpt-4o-mini", messages=[ {"role": "user", "content": "What is machine learning?"} ], usage_metadata={ "organizationName": "AcmeCorp", "productName": "customer-chatbot", "subscriber": { "id": "user-123", "email": "user@example.com", "credential": { "name": "api-key-name", "value": "api-key-value" } }, "task_type": "chat-completion", "trace_id": "session-abc-123", "agent": "my-agent", "subscription_id": "plan-pro", "response_quality_score": 0.95 } ) print(response.choices[0].message.content) ``` -------------------------------- ### Azure OpenAI Chat Completion with Python SDK Source: https://context7.com/revenium/revenium-python-sdk/llms.txt Shows how to perform chat completions with Azure OpenAI using the Revenium Python SDK. It covers basic usage and advanced scenarios with custom usage metadata for enterprise analytics. ```python import os import time from dotenv import load_dotenv from openai import AzureOpenAI import revenium_middleware.openai.middleware load_dotenv() # Create Azure OpenAI client azure = AzureOpenAI( azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"), api_key=os.getenv("AZURE_OPENAI_API_KEY"), api_version=os.getenv("AZURE_OPENAI_API_VERSION", "2024-12-01-preview") ) deployment = os.getenv("AZURE_OPENAI_DEPLOYMENT") # Basic Azure chat completion - automatically tracked with Azure provider info response = azure.chat.completions.create( model=deployment, messages=[{"role": "user", "content": "What are the benefits of using Azure OpenAI?"}] ) print(f"Response: {response.choices[0].message.content}") print(f"Usage: {response.usage}") # Azure chat with rich metadata response = azure.chat.completions.create( model=deployment, messages=[{"role": "user", "content": "Explain Azure OpenAI in 3 points." }], usage_metadata={ "subscriber": {"id": "azure-user-789", "email": "azure-dev@company.com"}, "organizationName": "AcmeCorp", "productName": "customer-chatbot", "task_type": "azure-comparison", "trace_id": f"azure-{int(time.time() * 1000)}", "agent": "azure-openai-python-basic" } ) print(f"Response: {response.choices[0].message.content}") # Tracked with Azure provider + rich metadata for enterprise analytics ``` -------------------------------- ### Manage Background Metering Tasks Source: https://context7.com/revenium/revenium-python-sdk/llms.txt Shows how to record usage and execute metering tasks asynchronously in background threads to avoid blocking the main application flow. ```python 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" ) # Start background thread - application continues while metering happens thread = run_async_in_thread(async_metering_task()) ``` -------------------------------- ### Anthropic Streaming API with Python SDK Source: https://context7.com/revenium/revenium-python-sdk/llms.txt Illustrates how to stream responses from Anthropic's Claude models using the Revenium Python SDK. It demonstrates setting metadata in context for streaming and using the context manager API for automatic token metering. ```python from dotenv import load_dotenv load_dotenv() import anthropic import revenium_middleware.anthropic from revenium_middleware.anthropic import usage_context # Set metadata in context for streaming usage_context.set({ "subscriber": { "email": "ai@revenium.io", "credential": {"name": "content-api-key"} }, "trace_id": "streaming-conv-001", "task_type": "creative-writing", "agent": "anthropic-streaming-python", "organizationName": "AcmeCorp", "productName": "customer-chatbot" }) client = anthropic.Anthropic() # Use streaming API with context manager with client.messages.stream( model="claude-3-haiku-20240307", max_tokens=200, temperature=0.9, messages=[{ "role": "user", "content": "Write a short poem about artificial intelligence and creativity." }] ) as stream: for text in stream.text_stream: print(text, end="", flush=True) print() ``` -------------------------------- ### Meter Arbitrary Tool Calls Source: https://github.com/revenium/revenium-python-sdk/blob/main/README.md Shows how to configure the client and use the @meter_tool decorator or manual reporting to track non-LLM operations like web scraping or database lookups. ```python from revenium_middleware import meter_tool, configure, report_tool_call # 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): return {"pages": 5, "data_mb": 2.3} # Manual reporting report_tool_call( tool_id="my-tool", operation="fetch", duration_ms=1234, success=True, usage_metadata={"records": 42}, ) ``` -------------------------------- ### Track LLM Requests with Metadata in Ollama Source: https://context7.com/revenium/revenium-python-sdk/llms.txt Demonstrates how to perform a chat completion request using the Ollama client while attaching custom Revenium usage metadata. This metadata allows for granular tracking of tasks, subscribers, and organizational metrics. ```python response = ollama.chat( model='qwen2.5:0.5b', messages=[{'role': 'user', 'content': 'What is quantum computing?'}], usage_metadata={ "trace_id": "conv-28a7e9d4", "task_type": "analyze-spectral-data", "subscriber": { "id": "1473847563", "email": "carol@finoptic.com", "credential": {"name": "Engineering API Key", "value": "hak-abc123456"} }, "organizationName": "Finoptic Labs", "subscription_id": "sub_gold_1234567890", "productName": "spectral-analyzer-gold", "agent": "chemistry-agent", "response_quality_score": 0.95 } ) print(response['message']['content']) ``` -------------------------------- ### Report Tool Calls Manually with Revenium SDK Source: https://context7.com/revenium/revenium-python-sdk/llms.txt Demonstrates how to manually report tool execution data to the Revenium metering service. This is useful for custom operations where automatic instrumentation is not applicable. ```python from revenium_middleware import report_tool_call, configure # Configure the metering client configure( metering_url="https://api.revenium.io/meter", api_key="your-api-key" ) # Report a tool call manually report_tool_call( tool_id="my-tool", operation="fetch", duration_ms=1234, success=True, usage_metadata={"records": 42} ) # Report a failed tool call report_tool_call( tool_id="database-query", operation="search", duration_ms=5678, success=False, usage_metadata={"error": "timeout", "query_complexity": "high"} ) ``` -------------------------------- ### Patch OpenAI Client for Perplexity Tracking Source: https://context7.com/revenium/revenium-python-sdk/llms.txt Demonstrates how to use the Revenium Perplexity middleware to automatically patch the OpenAI client. This enables seamless metering of requests made to Perplexity AI models. ```python import os from dotenv import load_dotenv from openai import OpenAI load_dotenv() # Import Revenium middleware (automatically patches OpenAI) import revenium_middleware.perplexity # Create OpenAI client with Perplexity base URL client = OpenAI( api_key=os.getenv("PERPLEXITY_API_KEY"), base_url="https://api.perplexity.ai" ) # Make a chat completion request - automatically metered response = client.chat.completions.create( model="sonar-pro", messages=[{ "role": "user", "content": "What is the capital of France? Answer in one sentence." }] ) print(f"Assistant: {response.choices[0].message.content}") print(f"Tokens used: {response.usage.total_tokens}") ``` -------------------------------- ### Send HTTP Requests to LiteLLM Proxy with Headers Source: https://context7.com/revenium/revenium-python-sdk/llms.txt Provides a method for sending direct HTTP requests to a LiteLLM proxy, utilizing custom 'x-revenium-' headers to pass metadata for analytics without using the SDK middleware. ```python import os import json import requests from dotenv import load_dotenv load_dotenv() proxy_base_url = os.getenv("LITELLM_PROXY_URL") api_key = os.getenv("LITELLM_API_KEY") proxy_url = f"{proxy_base_url.rstrip('/')}/chat/completions" headers = { "Content-Type": "application/json", "Authorization": f"Bearer {api_key}", "x-revenium-trace-id": "conv-28a7e9d4", "x-revenium-task-type": "analyze-spectral-data", "x-revenium-organization-name": "Finoptic Labs", "x-revenium-product-name": "spectral-analyzer-gold", "x-revenium-agent": "chemistry-agent", "x-revenium-subscriber-email": "carol@finoptic.com", "x-revenium-subscriber-id": "1473847563" } data = { "model": "openai/gpt-4o-mini", "messages": [ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Please verify you're ready to assist me."} ] } response = requests.post(proxy_url, headers=headers, data=json.dumps(data)) response.raise_for_status() print(response.json()) ``` -------------------------------- ### Track OpenAI Streaming Responses Source: https://context7.com/revenium/revenium-python-sdk/llms.txt Shows how to use the middleware to track streaming chat completions, ensuring accurate token usage reporting after the stream completes. ```python import os import time from dotenv import load_dotenv from openai import OpenAI load_dotenv() import revenium_middleware.openai.middleware client = OpenAI() # Basic streaming - automatically tracked print("Assistant: ", end="", flush=True) stream = client.chat.completions.create( model="gpt-4o-mini", messages=[{"role": "user", "content": "Count from 1 to 5 slowly"}], stream=True ) for chunk in stream: if chunk.choices and chunk.choices[0].delta.content: print(chunk.choices[0].delta.content, end="", flush=True) print() # Token usage automatically metered after stream completion # Streaming with metadata stream = client.chat.completions.create( model="gpt-4o-mini", messages=[{"role": "user", "content": "Write a haiku about middleware"}], stream=True, usage_metadata={ "subscriber": {"id": "streaming-user-456", "email": "poet@company.com"}, "organizationName": "AcmeCorp", "productName": "customer-chatbot", "task_type": "creative-writing", "trace_id": f"stream-{int(time.time() * 1000)}", "agent": "openai-python-streaming" } ) for chunk in stream: if chunk.choices and chunk.choices[0].delta.content: print(chunk.choices[0].delta.content, end="", flush=True) print() ``` -------------------------------- ### Track Google Gemini Requests Source: https://context7.com/revenium/revenium-python-sdk/llms.txt Uses the google-genai SDK with Revenium middleware to track model interactions. Supports passing detailed subscriber and task metadata for advanced analytics. ```python import revenium_middleware.google from google import genai client = genai.Client() response = client.models.generate_content( model="gemini-2.0-flash-001", contents="What is 2+2?", usage_metadata={"trace_id": "unique-trace-id", "organizationName": "AcmeCorp"} ) print(response.text) ``` -------------------------------- ### Track Local Ollama LLM Usage Source: https://context7.com/revenium/revenium-python-sdk/llms.txt Configures the Ollama client to work with Revenium middleware, allowing for unified metering of local model inference requests. ```python import ollama import revenium_middleware.ollama client = ollama.Client() response = ollama.chat( model='qwen2.5:0.5b', messages=[{'role': 'user', 'content': 'Please verify you are ready to assist me.'}] ) print(response['message']['content']) ``` -------------------------------- ### Retrieve Final Message and Token Usage Source: https://context7.com/revenium/revenium-python-sdk/llms.txt Demonstrates how to extract the final message object from a stream and access the associated input and output token usage metrics. ```python final_message = stream.get_final_message() print(f"Tokens: {final_message.usage.input_tokens} input + {final_message.usage.output_tokens} output") ``` -------------------------------- ### Track Google Vertex AI Requests Source: https://context7.com/revenium/revenium-python-sdk/llms.txt Integrates Revenium with Vertex AI by attaching usage metadata directly to the GenerativeModel instance for automatic tracking of enterprise deployments. ```python import revenium_middleware.google import vertexai from vertexai.generative_models import GenerativeModel vertexai.init() model = GenerativeModel("gemini-2.0-flash-001") model._revenium_usage_metadata = {"trace_id": "unique-trace-id", "organizationName": "AcmeCorp"} response = model.generate_content("What is 2+2?") print(response.text) ``` -------------------------------- ### Meter custom tools with @meter_tool Source: https://context7.com/revenium/revenium-python-sdk/llms.txt The @meter_tool decorator enables metering for arbitrary functions like web scrapers or database lookups. It captures execution duration, success status, and custom attribution metadata alongside standard LLM metering. ```python from revenium_middleware import meter_tool, configure configure( metering_url=os.getenv("REVENIUM_METERING_BASE_URL", "https://api.revenium.ai"), api_key=os.environ["REVENIUM_METERING_API_KEY"] ) @meter_tool("web-scraper", operation="scrape", agent="research-assistant") def scrape_website(url: str) -> dict: print(f"Scraping: {url}") return { "url": url, "title": "Example Page", "content": "Sample content..." } ``` -------------------------------- ### Enable selective metering with @revenium_meter Source: https://context7.com/revenium/revenium-python-sdk/llms.txt The @revenium_meter decorator allows for selective metering of API calls. When the environment variable REVENIUM_SELECTIVE_METERING is set to true, only functions decorated with this decorator will have their internal API calls metered. ```python from revenium_middleware import revenium_meter, revenium_metadata @revenium_meter() @revenium_metadata(trace_id="selective-metering-demo", task_type="premium-feature") def premium_feature(prompt: str) -> str: response = client.chat.completions.create( model="gpt-4o-mini", messages=[{"role": "user", "content": prompt}], max_tokens=100 ) return response.choices[0].message.content ``` -------------------------------- ### Inject metadata with @revenium_metadata Source: https://context7.com/revenium/revenium-python-sdk/llms.txt The @revenium_metadata decorator automatically injects specified metadata, such as trace IDs and task types, into all OpenAI API calls within the decorated function's scope. This eliminates the need to manually pass usage metadata to individual API requests. ```python import os from dotenv import load_dotenv from openai import OpenAI load_dotenv() import revenium_middleware.openai.middleware from revenium_middleware import revenium_metadata client = OpenAI() @revenium_metadata( trace_id="session-12345", task_type="customer-support", organization_name="AcmeCorp" ) def handle_customer_query(question: str) -> str: response = client.chat.completions.create( model="gpt-4o-mini", messages=[ {"role": "system", "content": "You are a helpful customer support assistant."}, {"role": "user", "content": question} ], max_tokens=100 ) return response.choices[0].message.content ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.