241241_tracker : AgentTracker | None = None
242242
243243
244+ def _get_tracker (operation : str = "this operation" ) -> AgentTracker :
245+ """Return the global tracker, raising if the SDK is not initialized.
246+
247+ Centralises the guard clause that was previously copy-pasted in every
248+ module-level convenience function.
249+
250+ Args:
251+ operation: Name shown in the error message (e.g. ``"track"``).
252+
253+ Raises:
254+ RuntimeError: If :func:`init` has not been called yet.
255+ """
256+ if _tracker is None :
257+ raise RuntimeError (f"Call agentlens.init() before { operation } ()" )
258+ return _tracker
259+
260+
244261def init (api_key : str = "default" , endpoint : str = "http://localhost:3000" ) -> AgentTracker :
245262 """Initialize the AgentLens SDK.
246263
@@ -279,16 +296,12 @@ def start_session(agent_name: str = "default-agent", metadata: dict | None = Non
279296 Returns:
280297 A Session object.
281298 """
282- if _tracker is None :
283- raise RuntimeError ("Call agentlens.init() before start_session()" )
284- return _tracker .start_session (agent_name = agent_name , metadata = metadata )
299+ return _get_tracker ("start_session" ).start_session (agent_name = agent_name , metadata = metadata )
285300
286301
287302def end_session (session_id : str | None = None ) -> None :
288303 """End the current or specified session and flush pending events."""
289- if _tracker is None :
290- raise RuntimeError ("Call agentlens.init() before end_session()" )
291- _tracker .end_session (session_id = session_id )
304+ _get_tracker ("end_session" ).end_session (session_id = session_id )
292305
293306
294307def track (
@@ -309,9 +322,7 @@ def track(
309322 Returns:
310323 The created AgentEvent.
311324 """
312- if _tracker is None :
313- raise RuntimeError ("Call agentlens.init() before track()" )
314- return _tracker .track (
325+ return _get_tracker ("track" ).track (
315326 event_type = event_type ,
316327 input_data = input_data ,
317328 output_data = output_data ,
@@ -332,9 +343,7 @@ def explain(session_id: str | None = None) -> str:
332343 Returns:
333344 A string explanation.
334345 """
335- if _tracker is None :
336- raise RuntimeError ("Call agentlens.init() before explain()" )
337- return _tracker .explain (session_id = session_id )
346+ return _get_tracker ("explain" ).explain (session_id = session_id )
338347
339348
340349def export_session (session_id : str | None = None , format : str = "json" ):
@@ -352,9 +361,7 @@ def export_session(session_id: str | None = None, format: str = "json"):
352361 A dict (for JSON) or a string (for CSV) with session data, events,
353362 and summary statistics.
354363 """
355- if _tracker is None :
356- raise RuntimeError ("Call agentlens.init() before export_session()" )
357- return _tracker .export_session (session_id = session_id , format = format )
364+ return _get_tracker ("export_session" ).export_session (session_id = session_id , format = format )
358365
359366
360367def compare_sessions (session_a : str , session_b : str ) -> dict :
@@ -371,9 +378,7 @@ def compare_sessions(session_a: str, session_b: str) -> dict:
371378 A dict with ``session_a`` metrics, ``session_b`` metrics,
372379 ``deltas``, and ``shared`` breakdowns.
373380 """
374- if _tracker is None :
375- raise RuntimeError ("Call agentlens.init() before compare_sessions()" )
376- return _tracker .compare_sessions (session_a = session_a , session_b = session_b )
381+ return _get_tracker ("compare_sessions" ).compare_sessions (session_a = session_a , session_b = session_b )
377382
378383
379384def get_costs (session_id : str | None = None ) -> dict :
@@ -388,9 +393,7 @@ def get_costs(session_id: str | None = None) -> dict:
388393 A dict with ``total_cost``, ``total_input_cost``, ``total_output_cost``,
389394 ``model_costs``, ``event_costs``, ``currency``, and ``unmatched_models``.
390395 """
391- if _tracker is None :
392- raise RuntimeError ("Call agentlens.init() before get_costs()" )
393- return _tracker .get_costs (session_id = session_id )
396+ return _get_tracker ("get_costs" ).get_costs (session_id = session_id )
394397
395398
396399def get_pricing () -> dict :
@@ -399,9 +402,7 @@ def get_pricing() -> dict:
399402 Returns:
400403 A dict with ``pricing`` (current prices) and ``defaults`` (built-in defaults).
401404 """
402- if _tracker is None :
403- raise RuntimeError ("Call agentlens.init() before get_pricing()" )
404- return _tracker .get_pricing ()
405+ return _get_tracker ("get_pricing" ).get_pricing ()
405406
406407
407408def set_pricing (pricing : dict ) -> dict :
@@ -414,6 +415,4 @@ def set_pricing(pricing: dict) -> dict:
414415 Returns:
415416 A dict with ``status`` and ``updated`` count.
416417 """
417- if _tracker is None :
418- raise RuntimeError ("Call agentlens.init() before set_pricing()" )
419- return _tracker .set_pricing (pricing = pricing )
418+ return _get_tracker ("set_pricing" ).set_pricing (pricing = pricing )
0 commit comments