@@ -267,8 +267,6 @@ def total_runs(team_id: strawberry.ID) -> int:
267267
268268 @staticmethod
269269 def aggregate_team_tokens (team_id : strawberry .ID ) -> dict [str , int ]:
270- from alphatrion import envs
271-
272270 if os .getenv (envs .ENABLE_TRACING , "false" ).lower () != "true" :
273271 return {"total_tokens" : 0 , "input_tokens" : 0 , "output_tokens" : 0 }
274272
@@ -283,8 +281,6 @@ def aggregate_team_tokens(team_id: strawberry.ID) -> dict[str, int]:
283281 def aggregate_model_distributions (
284282 team_id : strawberry .ID ,
285283 ) -> list [ModelDistribution ]:
286- from alphatrion import envs
287-
288284 if os .getenv (envs .ENABLE_TRACING , "false" ).lower () != "true" :
289285 return []
290286
@@ -456,7 +452,6 @@ async def get_artifact_content(
456452 @staticmethod
457453 def aggregate_run_tokens (run_id : strawberry .ID ) -> dict [str , int ]:
458454 """Aggregate token usage from all traces for a run."""
459- from alphatrion import envs
460455
461456 if os .getenv (envs .ENABLE_TRACING , "false" ).lower () != "true" :
462457 return {"total_tokens" : 0 , "input_tokens" : 0 , "output_tokens" : 0 }
@@ -578,7 +573,6 @@ def get_experiment_usage(experiment_id: strawberry.ID):
578573 @staticmethod
579574 def list_spans (run_id : strawberry .ID ) -> list [Span ]:
580575 """List all spans for a specific run."""
581- from alphatrion import envs
582576
583577 # Check if tracing is enabled
584578 if os .getenv (envs .ENABLE_TRACING , "false" ).lower () != "true" :
@@ -661,7 +655,6 @@ def get_daily_token_usage(
661655 team_id : strawberry .ID , days : int = 7
662656 ) -> list [DailyTokenUsage ]:
663657 """Get daily token usage from LLM calls for a team."""
664- from alphatrion import envs
665658
666659 # Check if tracing is enabled
667660 if os .getenv (envs .ENABLE_TRACING , "false" ).lower () != "true" :
@@ -689,6 +682,28 @@ def get_daily_token_usage(
689682 print (f"Failed to fetch daily token usage: { e } " )
690683 return []
691684
685+ @staticmethod
686+ def get_experiment_trace_stats (experiment_id : strawberry .ID ) -> dict [str , int ]:
687+ """Get trace statistics (success/error counts) for an experiment."""
688+
689+ # Check if tracing is enabled
690+ if os .getenv (envs .ENABLE_TRACING , "false" ).lower () != "true" :
691+ return {"total_spans" : 0 , "success_spans" : 0 , "error_spans" : 0 }
692+
693+ try :
694+ trace_store = runtime .storage_runtime ().tracestore
695+ stats = trace_store .get_trace_stats_by_exp_id (exp_id = experiment_id )
696+ # Don't close - it's a shared singleton connection
697+ return stats
698+ except Exception as e :
699+ # Log error and return zeros - don't fail the GraphQL query
700+ import logging
701+
702+ logging .error (
703+ f"Failed to get trace stats for experiment { experiment_id } : { e } "
704+ )
705+ return {"total_spans" : 0 , "success_spans" : 0 , "error_spans" : 0 }
706+
692707 @staticmethod
693708 def list_datasets (
694709 team_id : strawberry .ID ,
0 commit comments