|
| 1 | +import logging |
| 2 | +from typing import Any |
| 3 | + |
| 4 | +import sentry_sdk |
| 5 | + |
| 6 | +from sentry.http import safe_urlopen |
| 7 | +from sentry.relay.config.ai_model_costs import ( |
| 8 | + AI_MODEL_COSTS_CACHE_KEY, |
| 9 | + AI_MODEL_COSTS_CACHE_TTL, |
| 10 | + AIModelCosts, |
| 11 | + AIModelCostV2, |
| 12 | + ModelId, |
| 13 | +) |
| 14 | +from sentry.tasks.base import instrumented_task |
| 15 | +from sentry.taskworker.config import TaskworkerConfig |
| 16 | +from sentry.taskworker.namespaces import ai_agent_monitoring_tasks |
| 17 | +from sentry.utils.cache import cache |
| 18 | + |
| 19 | +logger = logging.getLogger(__name__) |
| 20 | + |
| 21 | + |
| 22 | +# OpenRouter API endpoint |
| 23 | +OPENROUTER_MODELS_API_URL = "https://openrouter.ai/api/v1/models" |
| 24 | + |
| 25 | + |
| 26 | +@instrumented_task( |
| 27 | + name="sentry.tasks.ai_agent_monitoring.fetch_ai_model_costs", |
| 28 | + queue="ai_agent_monitoring", |
| 29 | + default_retry_delay=5, |
| 30 | + max_retries=3, |
| 31 | + soft_time_limit=30, # 30 seconds |
| 32 | + time_limit=35, # 35 seconds |
| 33 | + taskworker_config=TaskworkerConfig( |
| 34 | + namespace=ai_agent_monitoring_tasks, |
| 35 | + processing_deadline_duration=35, |
| 36 | + expires=30, |
| 37 | + ), |
| 38 | +) |
| 39 | +def fetch_ai_model_costs() -> None: |
| 40 | + """ |
| 41 | + Fetch AI model costs from OpenRouter API and store them in cache. |
| 42 | +
|
| 43 | + This task fetches model pricing data from OpenRouter and converts it to |
| 44 | + the AIModelCostV2 format for use by Sentry's LLM cost tracking. |
| 45 | + """ |
| 46 | + |
| 47 | + # Fetch data from OpenRouter API |
| 48 | + response = safe_urlopen( |
| 49 | + OPENROUTER_MODELS_API_URL, |
| 50 | + ) |
| 51 | + response.raise_for_status() |
| 52 | + |
| 53 | + # Parse the response |
| 54 | + data = response.json() |
| 55 | + |
| 56 | + if not isinstance(data, dict) or "data" not in data: |
| 57 | + logger.error( |
| 58 | + "fetch_ai_model_costs.invalid_response_format", |
| 59 | + extra={"response_keys": list(data.keys()) if isinstance(data, dict) else "not_dict"}, |
| 60 | + ) |
| 61 | + return |
| 62 | + |
| 63 | + models_data = data["data"] |
| 64 | + if not isinstance(models_data, list): |
| 65 | + logger.error( |
| 66 | + "fetch_ai_model_costs.invalid_models_data_format", |
| 67 | + extra={"type": type(models_data).__name__}, |
| 68 | + ) |
| 69 | + return |
| 70 | + |
| 71 | + # Convert to AIModelCostV2 format |
| 72 | + models_dict: dict[ModelId, AIModelCostV2] = {} |
| 73 | + |
| 74 | + for model_data in models_data: |
| 75 | + if not isinstance(model_data, dict): |
| 76 | + continue |
| 77 | + |
| 78 | + model_id = model_data.get("id") |
| 79 | + if not model_id: |
| 80 | + continue |
| 81 | + |
| 82 | + # OpenRouter includes provider name in the model ID, e.g. openai/gpt-4o-mini |
| 83 | + # We need to extract the model name, since our SDKs only send the model name |
| 84 | + # (e.g. gpt-4o-mini) |
| 85 | + if "/" in model_id: |
| 86 | + model_id = model_id.split("/", maxsplit=1)[1] |
| 87 | + |
| 88 | + pricing = model_data.get("pricing", {}) |
| 89 | + |
| 90 | + # Convert pricing data to AIModelCostV2 format |
| 91 | + # OpenRouter provides costs as strings, we need to convert to float |
| 92 | + try: |
| 93 | + ai_model_cost = AIModelCostV2( |
| 94 | + inputPerToken=safe_float_conversion(pricing.get("prompt")), |
| 95 | + outputPerToken=safe_float_conversion(pricing.get("completion")), |
| 96 | + outputReasoningPerToken=safe_float_conversion(pricing.get("internal_reasoning")), |
| 97 | + inputCachedPerToken=safe_float_conversion(pricing.get("input_cache_read")), |
| 98 | + ) |
| 99 | + |
| 100 | + models_dict[model_id] = ai_model_cost |
| 101 | + |
| 102 | + except (ValueError, TypeError) as e: |
| 103 | + sentry_sdk.capture_exception(e) |
| 104 | + continue |
| 105 | + |
| 106 | + ai_model_costs: AIModelCosts = {"version": 2, "models": models_dict} |
| 107 | + cache.set(AI_MODEL_COSTS_CACHE_KEY, ai_model_costs, AI_MODEL_COSTS_CACHE_TTL) |
| 108 | + |
| 109 | + |
| 110 | +def safe_float_conversion(value: Any) -> float: |
| 111 | + """ |
| 112 | + Safely convert a value to float, handling string inputs and None values. |
| 113 | +
|
| 114 | + Args: |
| 115 | + value: The value to convert (could be string, float, int, or None) |
| 116 | +
|
| 117 | + Returns: |
| 118 | + The float value, or 0.0 if the value is None or cannot be converted |
| 119 | + """ |
| 120 | + if value is None: |
| 121 | + return 0.0 |
| 122 | + |
| 123 | + if isinstance(value, (int, float)): |
| 124 | + return float(value) |
| 125 | + |
| 126 | + if isinstance(value, str): |
| 127 | + try: |
| 128 | + return float(value) |
| 129 | + except ValueError: |
| 130 | + return 0.0 |
| 131 | + |
| 132 | + return 0.0 |
0 commit comments