5757logger = get_logger (__name__ )
5858
5959
60+ async def _enrich_with_usage (
61+ summary : ProviderHealthSummary ,
62+ app_state : AppState ,
63+ name : str ,
64+ ) -> ProviderHealthSummary :
65+ """Enrich a health summary with token/cost data from CostTracker.
66+
67+ Args:
68+ summary: Base health summary from the health tracker.
69+ app_state: Application state.
70+ name: Provider name.
71+
72+ Returns:
73+ Enriched summary (or unchanged if enrichment is unavailable).
74+ """
75+ if not app_state .has_cost_tracker :
76+ return summary
77+ try :
78+ now = datetime .now (UTC )
79+ usage = await app_state .cost_tracker .get_provider_usage (
80+ name ,
81+ start = now - timedelta (hours = 24 ),
82+ end = now ,
83+ )
84+ return summary .model_copy (
85+ update = {
86+ "total_tokens_24h" : usage .total_tokens ,
87+ "total_cost_24h" : usage .total_cost ,
88+ },
89+ )
90+ except MemoryError , RecursionError :
91+ raise
92+ except Exception as exc :
93+ logger .warning (
94+ API_PROVIDER_USAGE_ENRICHMENT_FAILED ,
95+ provider = name ,
96+ error = str (exc ),
97+ error_type = type (exc ).__qualname__ ,
98+ )
99+ return summary
100+
101+
60102class ProviderController (Controller ):
61103 """LLM provider management: CRUD, test, and presets."""
62104
@@ -245,7 +287,9 @@ async def get_provider_health(
245287 """Get provider health summary.
246288
247289 Returns health status, error rate, average response time,
248- and call count for the last 24 hours.
290+ call count, total tokens, and total cost for the last 24
291+ hours. Token and cost totals are enriched from the cost
292+ tracker when available.
249293
250294 Args:
251295 state: Application state.
@@ -264,29 +308,7 @@ async def get_provider_health(
264308 logger .warning (API_RESOURCE_NOT_FOUND , resource = "provider" , name = name )
265309 raise NotFoundError (msg )
266310 summary = await app_state .provider_health_tracker .get_summary (name )
267- if app_state .has_cost_tracker :
268- try :
269- now = datetime .now (UTC )
270- usage = await app_state .cost_tracker .get_provider_usage (
271- name ,
272- start = now - timedelta (hours = 24 ),
273- end = now ,
274- )
275- summary = summary .model_copy (
276- update = {
277- "total_tokens_24h" : usage .total_tokens ,
278- "total_cost_24h" : usage .total_cost ,
279- },
280- )
281- except MemoryError , RecursionError :
282- raise
283- except Exception as exc :
284- logger .warning (
285- API_PROVIDER_USAGE_ENRICHMENT_FAILED ,
286- provider = name ,
287- error = str (exc ),
288- error_type = type (exc ).__qualname__ ,
289- )
311+ summary = await _enrich_with_usage (summary , app_state , name )
290312 logger .debug (
291313 API_PROVIDER_HEALTH_QUERIED ,
292314 provider = name ,
0 commit comments