From 0099bc4bbd514f89423fa104cfd7743d73aca12f Mon Sep 17 00:00:00 2001 From: NikitolProject Date: Thu, 26 Mar 2026 13:31:32 +0300 Subject: [PATCH] use dedicated span per conversation_post for token usage tracking --- ai_core_api_client/api/ai_api.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/ai_core_api_client/api/ai_api.py b/ai_core_api_client/api/ai_api.py index b97d001..2c7003e 100644 --- a/ai_core_api_client/api/ai_api.py +++ b/ai_core_api_client/api/ai_api.py @@ -27,22 +27,21 @@ from ai_core_api_client.rest import RESTResponseType def _record_usage_to_span(response) -> None: - """Record token usage from ai-core-api response as OTel span attributes.""" + """Record token usage from ai-core-api response as a dedicated OTel span.""" try: usage = getattr(response, 'usage', None) if usage is None: return from opentelemetry import trace - span = trace.get_current_span() - if not span.is_recording(): - return - model = getattr(usage, 'model', None) - if model: - span.set_attribute("gen_ai.usage.model", model) - for attr in ('prompt_tokens', 'completion_tokens', 'total_tokens', 'cached_tokens'): - val = getattr(usage, attr, None) - if val is not None: - span.set_attribute(f"gen_ai.usage.{attr}", val) + tracer = trace.get_tracer("ai-core-api-client") + with tracer.start_as_current_span("ai_core_api_usage") as span: + model = getattr(usage, 'model', None) + if model: + span.set_attribute("gen_ai.usage.model", model) + for attr in ('prompt_tokens', 'completion_tokens', 'total_tokens', 'cached_tokens'): + val = getattr(usage, attr, None) + if val is not None: + span.set_attribute(f"gen_ai.usage.{attr}", val) except Exception: pass