Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.
TypeScript agent-harnessagent-harness-engineeringai-engineeringai-observabilityamd-gpuclaude-codeclickhousecodexcoding-agentcoding-agentscursorgenaigpu-monitoringllm-observabilityllmopsnvidia-smiobservabilityopen-sourceopenaiopentelemetry
[Feat]: Added ability to customize the pricing JSON (#239)
Pratik Singh Chauhanbaf3b58ffabe
diff --git a/sdk/python/README.md b/sdk/python/README.mdindex 62243127..3d1f8ba3 100644--- a/sdk/python/README.md+++ b/sdk/python/README.md@@ -140,6 +140,7 @@ Below is a detailed overview of the configuration options available, allowing yo| `trace_content` | Enables tracing of content for deeper insights. | `True` | No || `disabled_instrumentors`| List of instrumentors to disable. Choices: `["openai", "anthropic", "langchain", "cohere", "mistral", "transformers", "chroma", "pinecone"]`. | `None` | No || `disable_metrics` | If set, disables the collection of metrics. | `False` | No |+| `pricing_json` | URL or file path of the pricing JSON file. | `https://github.com/openlit/openlit/blob/main/assets/pricing.json` | No |## 🌱 Contributingdiff --git a/sdk/python/src/openlit/__helpers.py b/sdk/python/src/openlit/__helpers.pyindex 53ebb766..da89e658 100644--- a/sdk/python/src/openlit/__helpers.py+++ b/sdk/python/src/openlit/__helpers.py@@ -2,8 +2,9 @@"""This module has functions to calculate model costs based on tokens and to fetch pricing information."""-+import jsonimport logging+from urllib.parse import urlparseimport requestsimport tiktokenfrom opentelemetry.trace import Status, StatusCode@@ -122,11 +123,35 @@ def get_audio_model_cost(model, pricing_info, prompt):cost = 0return cost-def fetch_pricing_info():- """Fetches pricing information from a specified URL."""- pricing_url = "https://raw.githubusercontent.com/openlit/openlit/main/assets/pricing.json"+def fetch_pricing_info(pricing_json=None):+ """+ Fetches pricing information from a specified URL or File Path.++ Args:+ pricing_json(str): path or url to the pricing json file++ Returns:+ dict: The pricing json+ """+ if pricing_json:+ is_url = urlparse(pricing_json).scheme != ""+ if is_url:+ pricing_url = pricing_json+ else:+ try:+ f = open(pricing_json)+ return json.load(f)+ except FileNotFoundError:+ logger.error("Pricing information file not found: %s", pricing_json)+ except json.JSONDecodeError:+ logger.error("Error decoding JSON from file: %s", pricing_json)+ except Exception as file_err:+ logger.error("Unexpected error occurred while reading file: %s", file_err)+ return {}+ else:+ pricing_url = "https://raw.githubusercontent.com/openlit/openlit/main/assets/pricing.json"try:- # Set a timeout of 10 seconds for both the connection and the read+ # Set a timeout of 10 seconds for both the connection and the readresponse = requests.get(pricing_url, timeout=20)response.raise_for_status()return response.json()diff --git a/sdk/python/src/openlit/__init__.py b/sdk/python/src/openlit/__init__.pyindex 42b939ed..aa803206 100644--- a/sdk/python/src/openlit/__init__.py+++ b/sdk/python/src/openlit/__init__.py@@ -75,7 +75,8 @@ class OpenlitConfig:@classmethoddef update_config(cls, environment, application_name, tracer, otlp_endpoint,- otlp_headers, disable_batch, trace_content, metrics_dict, disable_metrics):+ otlp_headers, disable_batch, trace_content, metrics_dict,+ disable_metrics, pricing_json):"""Updates the configuration based on provided parameters.@@ -88,10 +89,11 @@ class OpenlitConfig:otlp_headers (Dict[str, str]): OTLP headers.disable_batch (bool): Disable batch span processing flag.trace_content (bool): Enable or disable content tracing.+ pricing_json(str): path or url to the pricing json file"""cls.environment = environmentcls.application_name = application_name- cls.pricing_info = fetch_pricing_info()+ cls.pricing_info = fetch_pricing_info(pricing_json)cls.tracer = tracercls.metrics_dict = metrics_dictcls.otlp_endpoint = otlp_endpoint@@ -126,7 +128,7 @@ def instrument_if_available(instrumentor_name, instrumentor_instance, config,def init(environment="default", application_name="default", tracer=None, otlp_endpoint=None,otlp_headers=None, disable_batch=False, trace_content=True, disabled_instrumentors=None,- meter=None, disable_metrics=False):+ meter=None, disable_metrics=False, pricing_json=None):"""Initializes the openLIT configuration and setups tracing.@@ -144,6 +146,7 @@ def init(environment="default", application_name="default", tracer=None, otlp_entrace_content (bool): Flag to trace content (Optional).disabled_instrumentors (List[str]): Optional. List of instrumentor names to disable.disable_metrics (bool): Flag to disable metrics (Optional)+ pricing_json(str): path or url to the pricing json file e.g. https://someexample.com/pricing.json or /home/user/pricing.json"""disabled_instrumentors = disabled_instrumentors if disabled_instrumentors else []# Check for invalid instrumentor names@@ -199,7 +202,7 @@ def init(environment="default", application_name="default", tracer=None, otlp_en# Update global configuration with the provided settings.config.update_config(environment, application_name, tracer, otlp_endpoint,otlp_headers, disable_batch, trace_content,- metrics_dict, disable_metrics)+ metrics_dict, disable_metrics, pricing_json)# Map instrumentor names to their instancesinstrumentor_instances = {