Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
Track OpenRouter LLM token counts and cost in get_call_stats (#6427)
Cindy Lid2e9caf5c5c2
diff --git a/skyvern/forge/sdk/api/llm/api_handler_factory.py b/skyvern/forge/sdk/api/llm/api_handler_factory.pyindex acf0a8827..e0c6b8dc7 100644--- a/skyvern/forge/sdk/api/llm/api_handler_factory.py+++ b/skyvern/forge/sdk/api/llm/api_handler_factory.py@@ -513,6 +513,26 @@ class LLMAPIHandlerFactory:return input_tokens, output_tokens, reasoning_tokens, cached_tokens+ @staticmethod+ def _extract_reported_usage_cost(response: ModelResponse | CustomStreamWrapper) -> float | None:+ """Return provider-reported cost from response.usage.cost when present."""+ if not hasattr(response, "usage") or not response.usage:+ return None++ usage = response.usage+ cost = getattr(usage, "cost", None)+ if cost is None and isinstance(usage, dict):+ cost = usage.get("cost")+ if cost is None:+ return None++ try:+ cost_value = float(cost)+ except (TypeError, ValueError):+ return None++ return cost_value if cost_value >= 0 else None+@staticmethoddef _apply_thinking_budget_optimization(parameters: dict[str, Any], new_budget: int, llm_config: LLMConfig | LLMRouterConfig, prompt_name: str@@ -2875,8 +2895,6 @@ class LLMCaller:self, response: ModelResponse | CustomStreamWrapper | AnthropicMessage | UITarsResponse) -> LLMCallStats:empty_call_stats = LLMCallStats()- if self.original_llm_key.startswith("openrouter/"):- return empty_call_stats# Handle OPENAI_COMPATIBLE provider GitHub Copilotif self.original_llm_key == "OPENAI_COMPATIBLE" and isinstance(response, (ModelResponse, CustomStreamWrapper)):@@ -2917,14 +2935,30 @@ class LLMCaller:reasoning_tokens=0,)elif isinstance(response, (ModelResponse, CustomStreamWrapper)):- try:- llm_cost = litellm.completion_cost(completion_response=response)- except Exception as e:- LOG.debug("Failed to calculate LLM cost", error=str(e), exc_info=True)- llm_cost = 0input_tokens, output_tokens, reasoning_tokens, cached_tokens = LLMAPIHandlerFactory._extract_token_counts(response)+ llm_cost = 0.0+ if self.original_llm_key.startswith("openrouter/"):+ reported_cost = LLMAPIHandlerFactory._extract_reported_usage_cost(response)+ if reported_cost is not None:+ llm_cost = reported_cost+ else:+ LOG.warning(+ "OpenRouter response missing usage.cost; cost will be reported as 0",+ llm_key=self.original_llm_key,+ response_id=getattr(response, "id", None),+ )+ else:+ try:+ llm_cost = litellm.completion_cost(completion_response=response)+ except Exception as e:+ LOG.debug(+ "Failed to calculate LLM cost",+ error=str(e),+ exc_info=True,+ )+ llm_cost = 0return LLMCallStats(llm_cost=llm_cost,input_tokens=input_tokens,