Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
Keep track of token counts in steps table (#412)
Kerem Yilmaz04c6e55848e9
diff --git a/skyvern/forge/sdk/api/llm/api_handler_factory.py b/skyvern/forge/sdk/api/llm/api_handler_factory.pyindex 0c5f68b11..99dea1800 100644--- a/skyvern/forge/sdk/api/llm/api_handler_factory.py+++ b/skyvern/forge/sdk/api/llm/api_handler_factory.py@@ -115,11 +115,15 @@ class LLMAPIHandlerFactory:data=response.model_dump_json(indent=2).encode("utf-8"),)llm_cost = litellm.completion_cost(completion_response=response)+ prompt_tokens = response.get("usage", {}).get("prompt_tokens", 0)+ completion_tokens = response.get("usage", {}).get("completion_tokens", 0)await app.DATABASE.update_step(task_id=step.task_id,step_id=step.step_id,organization_id=step.organization_id,incremental_cost=llm_cost,+ incremental_input_tokens=prompt_tokens if prompt_tokens > 0 else None,+ incremental_output_tokens=completion_tokens if completion_tokens > 0 else None,)parsed_response = parse_api_response(response, llm_config.add_assistant_prefix)if step:@@ -206,11 +210,15 @@ class LLMAPIHandlerFactory:data=response.model_dump_json(indent=2).encode("utf-8"),)llm_cost = litellm.completion_cost(completion_response=response)+ prompt_tokens = response.get("usage", {}).get("prompt_tokens", 0)+ completion_tokens = response.get("usage", {}).get("completion_tokens", 0)await app.DATABASE.update_step(task_id=step.task_id,step_id=step.step_id,organization_id=step.organization_id,incremental_cost=llm_cost,+ incremental_input_tokens=prompt_tokens if prompt_tokens > 0 else None,+ incremental_output_tokens=completion_tokens if completion_tokens > 0 else None,)parsed_response = parse_api_response(response, llm_config.add_assistant_prefix)if step:diff --git a/skyvern/forge/sdk/db/client.py b/skyvern/forge/sdk/db/client.pyindex b4880a759..52632fa14 100644--- a/skyvern/forge/sdk/db/client.py+++ b/skyvern/forge/sdk/db/client.py@@ -293,6 +293,8 @@ class AgentDB:retry_index: int | None = None,organization_id: str | None = None,incremental_cost: float | None = None,+ incremental_input_tokens: int | None = None,+ incremental_output_tokens: int | None = None,) -> Step:try:async with self.Session() as session:@@ -314,6 +316,10 @@ class AgentDB:step.retry_index = retry_indexif incremental_cost is not None:step.step_cost = incremental_cost + float(step.step_cost or 0)+ if incremental_input_tokens is not None:+ step.input_token_count = incremental_input_tokens + (step.input_token_count or 0)+ if incremental_output_tokens is not None:+ step.output_token_count = incremental_output_tokens + (step.output_token_count or 0)await session.commit()updated_step = await self.get_step(task_id, step_id, organization_id)