Skip to content

Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.

[Feat]: OpenTelemetry Instrumentation for OpenAI Response API and OpenAI Agents SDK (#658)

Patcherb0099ce87754
diff --git a/docs/latest/integrations/introduction.mdx b/docs/latest/integrations/introduction.mdx
index fa1e3487..6e0d9026 100644
--- a/docs/latest/integrations/introduction.mdx
+++ b/docs/latest/integrations/introduction.mdx
@@ -1891,6 +1891,18 @@ Start integrating and monitoring your applications with OpenLIT in just one line
iconType="duotone"
>
</Card>
+ <Card
+ title="OpenAI Agents"
+ href="/latest/integrations/openai-agents"
+ icon={
+ <svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
+ <title>OpenAI</title>
+ <path d="M22.2819 9.8211a5.9847 5.9847 0 0 0-.5157-4.9108 6.0462 6.0462 0 0 0-6.5098-2.9A6.0651 6.0651 0 0 0 4.9807 4.1818a5.9847 5.9847 0 0 0-3.9977 2.9 6.0462 6.0462 0 0 0 .7427 7.0966 5.98 5.98 0 0 0 .511 4.9107 6.051 6.051 0 0 0 6.5146 2.9001A5.9847 5.9847 0 0 0 13.2599 24a6.0557 6.0557 0 0 0 5.7718-4.2058 5.9894 5.9894 0 0 0 3.9977-2.9001 6.0557 6.0557 0 0 0-.7475-7.0729zm-9.022 12.6081a4.4755 4.4755 0 0 1-2.8764-1.0408l.1419-.0804 4.7783-2.7582a.7948.7948 0 0 0 .3927-.6813v-6.7369l2.02 1.1686a.071.071 0 0 1 .038.052v5.5826a4.504 4.504 0 0 1-4.4945 4.4944zm-9.6607-4.1254a4.4708 4.4708 0 0 1-.5346-3.0137l.142.0852 4.783 2.7582a.7712.7712 0 0 0 .7806 0l5.8428-3.3685v2.3324a.0804.0804 0 0 1-.0332.0615L9.74 19.9502a4.4992 4.4992 0 0 1-6.1408-1.6464zM2.3408 7.8956a4.485 4.485 0 0 1 2.3655-1.9728V11.6a.7664.7664 0 0 0 .3879.6765l5.8144 3.3543-2.0201 1.1685a.0757.0757 0 0 1-.071 0l-4.8303-2.7865A4.504 4.504 0 0 1 2.3408 7.872zm16.5963 3.8558L13.1038 8.364 15.1192 7.2a.0757.0757 0 0 1 .071 0l4.8303 2.7913a4.4944 4.4944 0 0 1-.6765 8.1042v-5.6772a.79.79 0 0 0-.407-.667zm2.0107-3.0231l-.142-.0852-4.7735-2.7818a.7759.7759 0 0 0-.7854 0L9.409 9.2297V6.8974a.0662.0662 0 0 1 .0284-.0615l4.8303-2.7866a4.4992 4.4992 0 0 1 6.6802 4.66zM8.3065 12.863l-2.02-1.1638a.0804.0804 0 0 1-.038-.0567V6.0742a4.4992 4.4992 0 0 1 7.3757-3.4537l-.142.0805L8.704 5.459a.7948.7948 0 0 0-.3927.6813zm1.0976-2.3654l2.602-1.4998 2.6069 1.4998v2.9994l-2.5974 1.4997-2.6067-1.4997Z" />
+ </svg>
+ }
+ iconType="duotone"
+ >
+ </Card>
<Card
title="LiteLLM"
href="/latest/integrations/litellm"
diff --git a/docs/latest/integrations/openai-agents.mdx b/docs/latest/integrations/openai-agents.mdx
new file mode 100644
index 00000000..945a2d9a
--- /dev/null
+++ b/docs/latest/integrations/openai-agents.mdx
@@ -0,0 +1,78 @@
+---
+title: 'Monitor OpenAI Agents'
+sidebarTitle: 'OpenAI Agents'
+---
+
+<Frame>
+ <img src="/images/docs-openai-banner.gif" />
+</Frame>
+
+OpenLIT uses OpenTelemetry Auto-Instrumentation to help you monitor AI Agents built using OpenAI Agents SDK. This includes tracking performance and operation stats.
+
+Auto-instrumentation means you don't have to set up monitoring manually for different LLMs, frameworks, or databases. By simply adding OpenLIT in your application, all the necessary monitoring configurations are automatically set up.
+
+The integration is compatible with
+- OpenAI Agents Python SDK client `>= 0.0.3`
+
+## Get Started
+
+<Steps>
+ <Step title="Install OpenLIT">
+ Open your command line or terminal and run:
+ ```shell
+ pip install openlit
+ ```
+ </Step>
+ <Step title="Initialize OpenLIT in your Application">
+ You can set up OpenLIT in your application using either function arguments directly in your code or by using environment variables.
+ <Tabs>
+ <Tab title="Setup using function arguments">
+
+ Add the following two lines to your application code:
+
+ ```python
+ import openlit
+
+ openlit.init(
+ otlp_endpoint="YOUR_OTEL_ENDPOINT",
+ )
+ ```
+
+ Replace:
+ 1. `YOUR_OTEL_ENDPOINT` with the URL of your OpenTelemetry backend, such as `http://127.0.0.1:4318` if you are using OpenLIT and a local OTel Collector.
+ </Tab>
+ <Tab title="Setup using Environment Variables">
+
+ Add the following two lines to your application code:
+
+ ```python
+ import openlit
+
+ openlit.init()
+ ```
+
+ Then, configure the your OTLP endpoint using environment variable:
+ ```shell
+ export OTEL_EXPORTER_OTLP_ENDPOINT = "YOUR_OTEL_ENDPOINT"
+ ```
+
+ Replace:
+ 1. `YOUR_OTEL_ENDPOINT` with the URL of your OpenTelemetry backend, such as `http://127.0.0.1:4318` if you are using OpenLIT and a local OTel Collector.
+ </Tab>
+ </Tabs>
+ To send metrics and traces to other Observability tools, refer to the [Connections Guide](/latest/connections/intro).
+
+ For more advanced configurations and application use cases, visit the [OpenLIT Python repository](https://github.com/openlit/openlit/tree/main/sdk/python).
+ </Step>
+</Steps>
+
+---
+
+<CardGroup cols={2}>
+<Card title="Connections" href="/latest/connections/intro" icon='link'>
+Connect to your existing Observablity Stack
+</Card>
+<Card title="SDK configuration" href="/latest/sdk-configuration" icon='file-code'>
+Documentation of the configuration options for the OpenLIT SDK.
+</Card>
+</CardGroup>
\ No newline at end of file
diff --git a/docs/mint.json b/docs/mint.json
index 8a8a1cc8..2627f01f 100644
--- a/docs/mint.json
+++ b/docs/mint.json
@@ -129,6 +129,7 @@
"version": "latest",
"pages": [
"latest/integrations/langchain",
+ "latest/integrations/openai-agents",
"latest/integrations/litellm",
"latest/integrations/crewai",
"latest/integrations/llama-index",
diff --git a/sdk/python/pyproject.toml b/sdk/python/pyproject.toml
index 1f53267c..5d40742d 100644
--- a/sdk/python/pyproject.toml
+++ b/sdk/python/pyproject.toml
@@ -1,6 +1,6 @@
[tool.poetry]
name = "openlit"
-version = "1.33.15"
+version = "1.33.16"
description = "OpenTelemetry-native Auto instrumentation library for monitoring LLM Applications and GPUs, facilitating the integration of observability into your GenAI-driven projects"
authors = ["OpenLIT"]
license = "Apache-2.0"
diff --git a/sdk/python/src/openlit/__init__.py b/sdk/python/src/openlit/__init__.py
index 4da6f21c..65cbdd67 100644
--- a/sdk/python/src/openlit/__init__.py
+++ b/sdk/python/src/openlit/__init__.py
@@ -65,6 +65,7 @@ from openlit.instrumentation.controlflow import ControlFlowInstrumentor
from openlit.instrumentation.crawl4ai import Crawl4AIInstrumentor
from openlit.instrumentation.firecrawl import FireCrawlInstrumentor
from openlit.instrumentation.letta import LettaInstrumentor
+from openlit.instrumentation.openai_agents import OpenAIAgentsInstrumentor
from openlit.instrumentation.gpu import GPUInstrumentor
import openlit.guard
import openlit.evals
@@ -293,6 +294,7 @@ def init(
"firecrawl": "firecrawl",
"letta": "letta",
"together": "together",
+ "openai-agents": "agents"
}
invalid_instrumentors = [
@@ -411,6 +413,7 @@ def init(
"firecrawl": FireCrawlInstrumentor(),
"letta": LettaInstrumentor(),
"together": TogetherInstrumentor(),
+ "openai-agents": OpenAIAgentsInstrumentor(),
}
# Initialize and instrument only the enabled instrumentors
diff --git a/sdk/python/src/openlit/instrumentation/openai/__init__.py b/sdk/python/src/openlit/instrumentation/openai/__init__.py
index 51df68fd..8aa3855a 100644
--- a/sdk/python/src/openlit/instrumentation/openai/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/openai/__init__.py
@@ -5,11 +5,11 @@ import importlib.metadata
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from wrapt import wrap_function_wrapper
-from openlit.instrumentation.openai.openai import chat_completions, embedding
+from openlit.instrumentation.openai.openai import chat_completions, embedding, responses
from openlit.instrumentation.openai.openai import image_generate, image_variatons, audio_create
from openlit.instrumentation.openai.async_openai import async_chat_completions, async_embedding
from openlit.instrumentation.openai.async_openai import async_image_generate, async_image_variatons
-from openlit.instrumentation.openai.async_openai import async_audio_create
+from openlit.instrumentation.openai.async_openai import async_audio_create, async_responses
_instruments = ("openai >= 1.1.1",)
@@ -45,6 +45,22 @@ class OpenAIInstrumentor(BaseInstrumentor):
metrics, disable_metrics),
)
+ wrap_function_wrapper(
+ "openai.resources.responses.responses",
+ "Responses.create",
+ responses(version, environment, application_name,
+ tracer, pricing_info, capture_message_content,
+ metrics, disable_metrics),
+ )
+
+ wrap_function_wrapper(
+ "openai.resources.responses.responses",
+ "AsyncResponses.create",
+ async_responses(version, environment, application_name,
+ tracer, pricing_info, capture_message_content,
+ metrics, disable_metrics),
+ )
+
wrap_function_wrapper(
"openai.resources.images",
"Images.generate",
diff --git a/sdk/python/src/openlit/instrumentation/openai/async_openai.py b/sdk/python/src/openlit/instrumentation/openai/async_openai.py
index af5d91b6..e5e979c8 100644
--- a/sdk/python/src/openlit/instrumentation/openai/async_openai.py
+++ b/sdk/python/src/openlit/instrumentation/openai/async_openai.py
@@ -13,6 +13,8 @@ from openlit.__helpers import (
get_image_model_cost,
general_tokens,
handle_exception,
+ extract_and_format_input,
+ concatenate_all_contents,
response_as_dict,
calculate_ttft,
calculate_tbt,
@@ -24,6 +26,409 @@ from openlit.semcov import SemanticConvetion
# Initialize logger for logging potential issues and operations
logger = logging.getLogger(__name__)
+def async_responses(version, environment, application_name,
+ tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+ """
+ Generates a telemetry wrapper for chat completions to collect metrics.
+
+ Args:
+ version: Version of the monitoring package.
+ environment: Deployment environment (e.g., production, staging).
+ application_name: Name of the application using the OpenAI API.
+ tracer: OpenTelemetry tracer for creating spans.
+ pricing_info: Information used for calculating the cost of OpenAI usage.
+ capture_message_content: Flag indicating whether to trace the actual content.
+
+ Returns:
+ A function that wraps the chat completions method to add telemetry.
+ """
+
+ class TracedAsyncStream:
+ """
+ Wrapper for streaming responses to collect metrics and trace data.
+ Wraps the response to collect message IDs and aggregated response.
+
+ This class implements the '__aiter__' and '__anext__' methods that
+ handle asynchronous streaming responses.
+
+ This class also implements '__aenter__' and '__aexit__' methods that
+ handle asynchronous context management protocol.
+ """
+ def __init__(
+ self,
+ wrapped,
+ span,
+ kwargs,
+ server_address,
+ server_port,
+ **args,
+ ):
+ self.__wrapped__ = wrapped
+ self._span = span
+ # Placeholder for aggregating streaming response
+ self._llmresponse = ""
+ self._response_id = ""
+ self._response_model = ""
+ self._finish_reason = ""
+ self._input_tokens = ""
+ self._output_tokens = ""
+
+ self._args = args
+ self._kwargs = kwargs
+ self._start_time = time.time()
+ self._end_time = None
+ self._timestamps = []
+ self._ttft = 0
+ self._tbt = 0
+ self._server_address = server_address
+ self._server_port = server_port
+
+ async def __aenter__(self):
+ await self.__wrapped__.__aenter__()
+ return self
+
+ async def __aexit__(self, exc_type, exc_value, traceback):
+ await self.__wrapped__.__aexit__(exc_type, exc_value, traceback)
+
+ def __aiter__(self):
+ return self
+
+ async def __getattr__(self, name):
+ """Delegate attribute access to the wrapped object."""
+ return getattr(await self.__wrapped__, name)
+
+ async def __anext__(self):
+ try:
+ chunk = await self.__wrapped__.__anext__()
+ end_time = time.time()
+ # Record the timestamp for the current chunk
+ self._timestamps.append(end_time)
+
+ if len(self._timestamps) == 1:
+ # Calculate time to first chunk
+ self._ttft = calculate_ttft(self._timestamps, self._start_time)
+
+ chunked = response_as_dict(chunk)
+ # Collect message IDs and aggregated response from events
+ if chunked.get('type') == "response.output_text.delta":
+ self._llmresponse += chunked.get('delta')
+ if chunked.get('type') == "response.completed":
+ self._response_id = chunked.get('response').get('id')
+ self._response_model = chunked.get('response').get('model')
+ self._finish_reason = chunked.get('response').get('status')
+ self._input_tokens = chunked.get('response').get('usage').get('input_tokens')
+ self._output_tokens = chunked.get('response').get('usage').get('output_tokens')
+ return chunk
+ except StopAsyncIteration:
+ # Handling exception ensure observability without disrupting operation
+ try:
+ self._end_time = time.time()
+ if len(self._timestamps) > 1:
+ self._tbt = calculate_tbt(self._timestamps)
+
+ try:
+ formatted_messages = extract_and_format_input(self._kwargs.get('input', ''))
+ prompt = concatenate_all_contents(formatted_messages)
+ except:
+ prompt = self._kwargs.get('input', '')
+
+ request_model = self._kwargs.get("model", "gpt-4o")
+
+ # Calculate cost of the operation
+ cost = get_chat_model_cost(request_model,
+ pricing_info, self._input_tokens,
+ self._output_tokens)
+
+ # Set Span attributes (OTel Semconv)
+ self._span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
+ self._span.set_attribute(SemanticConvetion.GEN_AI_OPERATION,
+ SemanticConvetion.GEN_AI_OPERATION_TYPE_CHAT)
+ self._span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM,
+ SemanticConvetion.GEN_AI_SYSTEM_OPENAI)
+ self._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MODEL,
+ request_model)
+ self._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_SEED,
+ self._kwargs.get("seed", ""))
+ self._span.set_attribute(SemanticConvetion.SERVER_PORT,
+ self._server_port)
+ self._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MAX_TOKENS,
+ self._kwargs.get("max_output_tokens", -1))
+ self._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_STOP_SEQUENCES,
+ self._kwargs.get("stop", []))
+ self._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TEMPERATURE,
+ self._kwargs.get("temperature", 1.0))
+ self._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TOP_P,
+ self._kwargs.get("top_p", 1.0))
+ self._span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_FINISH_REASON,
+ [self._finish_reason])
+ self._span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_ID,
+ self._response_id)
+ self._span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_MODEL,
+ self._response_model)
+ self._span.set_attribute(SemanticConvetion.GEN_AI_USAGE_INPUT_TOKENS,
+ self._input_tokens)
+ self._span.set_attribute(SemanticConvetion.GEN_AI_USAGE_OUTPUT_TOKENS,
+ self._output_tokens)
+ self._span.set_attribute(SemanticConvetion.SERVER_ADDRESS,
+ self._server_address)
+ if isinstance(self._llmresponse, str):
+ self._span.set_attribute(SemanticConvetion.GEN_AI_OUTPUT_TYPE,
+ "text")
+ else:
+ self._span.set_attribute(SemanticConvetion.GEN_AI_OUTPUT_TYPE,
+ "json")
+
+ # Set Span attributes (Extra)
+ self._span.set_attribute(DEPLOYMENT_ENVIRONMENT,
+ environment)
+ self._span.set_attribute(SERVICE_NAME,
+ application_name)
+ self._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_USER,
+ self._kwargs.get("user", ""))
+ self._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_IS_STREAM,
+ True)
+ self._span.set_attribute(SemanticConvetion.GEN_AI_USAGE_TOTAL_TOKENS,
+ self._input_tokens + self._output_tokens)
+ self._span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,
+ cost)
+ self._span.set_attribute(SemanticConvetion.GEN_AI_SERVER_TBT,
+ self._tbt)
+ self._span.set_attribute(SemanticConvetion.GEN_AI_SERVER_TTFT,
+ self._ttft)
+ self._span.set_attribute(SemanticConvetion.GEN_AI_SDK_VERSION,
+ version)
+
+ if capture_message_content:
+ self._span.add_event(
+ name=SemanticConvetion.GEN_AI_CONTENT_PROMPT_EVENT,
+ attributes={
+ SemanticConvetion.GEN_AI_CONTENT_PROMPT: prompt,
+ },
+ )
+ self._span.add_event(
+ name=SemanticConvetion.GEN_AI_CONTENT_COMPLETION_EVENT,
+ attributes={
+ SemanticConvetion.GEN_AI_CONTENT_COMPLETION: self._llmresponse,
+ },
+ )
+ self._span.set_status(Status(StatusCode.OK))
+
+ if disable_metrics is False:
+ attributes = create_metrics_attributes(
+ service_name=application_name,
+ deployment_environment=environment,
+ operation=SemanticConvetion.GEN_AI_OPERATION_TYPE_CHAT,
+ system=SemanticConvetion.GEN_AI_SYSTEM_OPENAI,
+ request_model=request_model,
+ server_address=self._server_address,
+ server_port=self._server_port,
+ response_model=self._response_model,
+ )
+
+ metrics["genai_client_usage_tokens"].record(
+ self._input_tokens + self._output_tokens, attributes
+ )
+ metrics["genai_client_operation_duration"].record(
+ self._end_time - self._start_time, attributes
+ )
+ metrics["genai_server_tbt"].record(
+ self._tbt, attributes
+ )
+ metrics["genai_server_ttft"].record(
+ self._ttft, attributes
+ )
+ metrics["genai_requests"].add(1, attributes)
+ metrics["genai_completion_tokens"].add(self._output_tokens, attributes)
+ metrics["genai_prompt_tokens"].add(self._input_tokens, attributes)
+ metrics["genai_cost"].record(cost, attributes)
+
+ except Exception as e:
+ handle_exception(self._span, e)
+ logger.error("Error in trace creation: %s", e)
+ finally:
+ self._span.end()
+ raise
+
+ async def wrapper(wrapped, instance, args, kwargs):
+ """
+ Wraps the 'chat.completions' API call to add telemetry.
+
+ This collects metrics such as execution time, cost, and token usage, and handles errors
+ gracefully, adding details to the trace for observability.
+
+ Args:
+ wrapped: The original 'chat.completions' method to be wrapped.
+ instance: The instance of the class where the original method is defined.
+ args: Positional arguments for the 'chat.completions' method.
+ kwargs: Keyword arguments for the 'chat.completions' method.
+
+ Returns:
+ The response from the original 'chat.completions' method.
+ """
+
+ # Check if streaming is enabled for the API call
+ streaming = kwargs.get("stream", False)
+ server_address, server_port = set_server_address_and_port(instance, "api.openai.com", 443)
+ request_model = kwargs.get("model", "gpt-4o")
+
+ span_name = f"{SemanticConvetion.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
+
+ # pylint: disable=no-else-return
+ if streaming:
+ # Special handling for streaming response to accommodate the nature of data flow
+ awaited_wrapped = await wrapped(*args, **kwargs)
+ span = tracer.start_span(span_name, kind=SpanKind.CLIENT)
+
+ return TracedAsyncStream(awaited_wrapped, span, kwargs, server_address, server_port)
+
+ # Handling for non-streaming responses
+ else:
+ with tracer.start_as_current_span(span_name, kind= SpanKind.CLIENT) as span:
+ start_time = time.time()
+ response = await wrapped(*args, **kwargs)
+ end_time = time.time()
+
+ response_dict = response_as_dict(response)
+
+ try:
+ try:
+ formatted_messages = extract_and_format_input(kwargs.get('input', ''))
+ prompt = concatenate_all_contents(formatted_messages)
+ except:
+ prompt = kwargs.get('input', '')
+
+ input_tokens = response_dict.get('usage').get('input_tokens')
+ output_tokens = response_dict.get('usage').get('output_tokens')
+
+ # Calculate cost of the operation
+ cost = get_chat_model_cost(request_model,
+ pricing_info, input_tokens,
+ output_tokens)
+
+ # Set base span attribues (OTel Semconv)
+ span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
+ span.set_attribute(SemanticConvetion.GEN_AI_OPERATION,
+ SemanticConvetion.GEN_AI_OPERATION_TYPE_CHAT)
+ span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM,
+ SemanticConvetion.GEN_AI_SYSTEM_OPENAI)
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MODEL,
+ request_model)
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_SEED,
+ kwargs.get("seed", ""))
+ span.set_attribute(SemanticConvetion.SERVER_PORT,
+ server_port)
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MAX_TOKENS,
+ kwargs.get("max_output_tokens", -1))
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_STOP_SEQUENCES,
+ kwargs.get("stop", []))
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TEMPERATURE,
+ str(response_dict.get("temperature", 1.0)))
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TOP_P,
+ str(response_dict.get("top_p", 1.0)))
+ span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_ID,
+ response_dict.get("id"))
+ span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_MODEL,
+ response_dict.get('model'))
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_INPUT_TOKENS,
+ input_tokens)
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_OUTPUT_TOKENS,
+ output_tokens)
+ span.set_attribute(SemanticConvetion.SERVER_ADDRESS,
+ server_address)
+
+ # Set base span attribues (Extras)
+ span.set_attribute(DEPLOYMENT_ENVIRONMENT,
+ environment)
+ span.set_attribute(SERVICE_NAME,
+ application_name)
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_USER,
+ kwargs.get("user", ""))
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_IS_STREAM,
+ False)
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_TOTAL_TOKENS,
+ input_tokens + output_tokens)
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,
+ cost)
+ span.set_attribute(SemanticConvetion.GEN_AI_SERVER_TTFT,
+ end_time - start_time)
+ span.set_attribute(SemanticConvetion.GEN_AI_SDK_VERSION,
+ version)
+
+ if capture_message_content:
+ span.add_event(
+ name=SemanticConvetion.GEN_AI_CONTENT_PROMPT_EVENT,
+ attributes={
+ SemanticConvetion.GEN_AI_CONTENT_PROMPT: prompt,
+ },
+ )
+
+ for i in range(kwargs.get('n',1)):
+ span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_FINISH_REASON,
+ [response_dict.get('status')])
+ try:
+ llm_response = str(response_dict.get('output')[i].get('content')[0].get('text',''))
+ except:
+ llm_response = ''
+
+ if capture_message_content:
+ span.add_event(
+ name=SemanticConvetion.GEN_AI_CONTENT_COMPLETION_EVENT,
+ attributes={
+ # pylint: disable=line-too-long
+ SemanticConvetion.GEN_AI_CONTENT_COMPLETION: llm_response,
+ },
+ )
+ if kwargs.get('tools'):
+ span.set_attribute(SemanticConvetion.GEN_AI_TOOL_CALLS,
+ str(response_dict.get('tools')))
+
+ if isinstance(llm_response, str):
+ span.set_attribute(SemanticConvetion.GEN_AI_OUTPUT_TYPE,
+ "text")
+ elif llm_response is not None:
+ span.set_attribute(SemanticConvetion.GEN_AI_OUTPUT_TYPE,
+ "json")
+
+ span.set_status(Status(StatusCode.OK))
+
+ if disable_metrics is False:
+ attributes = create_metrics_attributes(
+ service_name=application_name,
+ deployment_environment=environment,
+ operation=SemanticConvetion.GEN_AI_OPERATION_TYPE_CHAT,
+ system=SemanticConvetion.GEN_AI_SYSTEM_OPENAI,
+ request_model=request_model,
+ server_address=server_address,
+ server_port=server_port,
+ response_model=response_dict.get('model'),
+ )
+
+ metrics["genai_client_usage_tokens"].record(
+ input_tokens + output_tokens, attributes
+ )
+ metrics["genai_client_operation_duration"].record(
+ end_time - start_time, attributes
+ )
+ metrics["genai_server_ttft"].record(
+ end_time - start_time, attributes
+ )
+ metrics["genai_requests"].add(1, attributes)
+ metrics["genai_completion_tokens"].add(output_tokens, attributes)
+ metrics["genai_prompt_tokens"].add(input_tokens, attributes)
+ metrics["genai_cost"].record(cost, attributes)
+
+ # Return original response
+ return response
+
+ except Exception as e:
+ handle_exception(span, e)
+ logger.error("Error in trace creation: %s", e)
+
+ # Return original response
+ return response
+
+ return wrapper
+
def async_chat_completions(version, environment, application_name,
tracer, pricing_info, capture_message_content, metrics, disable_metrics):
"""
diff --git a/sdk/python/src/openlit/instrumentation/openai/openai.py b/sdk/python/src/openlit/instrumentation/openai/openai.py
index 69f608d6..d7b0fa6e 100644
--- a/sdk/python/src/openlit/instrumentation/openai/openai.py
+++ b/sdk/python/src/openlit/instrumentation/openai/openai.py
@@ -13,6 +13,8 @@ from openlit.__helpers import (
get_image_model_cost,
general_tokens,
handle_exception,
+ extract_and_format_input,
+ concatenate_all_contents,
response_as_dict,
calculate_ttft,
calculate_tbt,
@@ -24,6 +26,409 @@ from openlit.semcov import SemanticConvetion
# Initialize logger for logging potential issues and operations
logger = logging.getLogger(__name__)
+def responses(version, environment, application_name,
+ tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+ """
+ Generates a telemetry wrapper for chat completions to collect metrics.
+
+ Args:
+ version: Version of the monitoring package.
+ environment: Deployment environment (e.g., production, staging).
+ application_name: Name of the application using the OpenAI API.
+ tracer: OpenTelemetry tracer for creating spans.
+ pricing_info: Information used for calculating the cost of OpenAI usage.
+ capture_message_content: Flag indicating whether to trace the actual content.
+
+ Returns:
+ A function that wraps the chat completions method to add telemetry.
+ """
+
+ class TracedSyncStream:
+ """
+ Wrapper for streaming responses to collect metrics and trace data.
+ Wraps the response to collect message IDs and aggregated response.
+
+ This class implements the '__aiter__' and '__anext__' methods that
+ handle asynchronous streaming responses.
+
+ This class also implements '__aenter__' and '__aexit__' methods that
+ handle asynchronous context management protocol.
+ """
+ def __init__(
+ self,
+ wrapped,
+ span,
+ kwargs,
+ server_address,
+ server_port,
+ **args,
+ ):
+ self.__wrapped__ = wrapped
+ self._span = span
+ # Placeholder for aggregating streaming response
+ self._llmresponse = ""
+ self._response_id = ""
+ self._response_model = ""
+ self._finish_reason = ""
+ self._input_tokens = ""
+ self._output_tokens = ""
+
+ self._args = args
+ self._kwargs = kwargs
+ self._start_time = time.time()
+ self._end_time = None
+ self._timestamps = []
+ self._ttft = 0
+ self._tbt = 0
+ self._server_address = server_address
+ self._server_port = server_port
+
+ def __enter__(self):
+ self.__wrapped__.__enter__()
+ return self
+
+ def __exit__(self, exc_type, exc_value, traceback):
+ self.__wrapped__.__exit__(exc_type, exc_value, traceback)
+
+ def __iter__(self):
+ return self
+
+ def __getattr__(self, name):
+ """Delegate attribute access to the wrapped object."""
+ return getattr(self.__wrapped__, name)
+
+ def __next__(self):
+ try:
+ chunk = self.__wrapped__.__next__()
+ end_time = time.time()
+ # Record the timestamp for the current chunk
+ self._timestamps.append(end_time)
+
+ if len(self._timestamps) == 1:
+ # Calculate time to first chunk
+ self._ttft = calculate_ttft(self._timestamps, self._start_time)
+
+ chunked = response_as_dict(chunk)
+ # Collect message IDs and aggregated response from events
+ if chunked.get('type') == "response.output_text.delta":
+ self._llmresponse += chunked.get('delta')
+ if chunked.get('type') == "response.completed":
+ self._response_id = chunked.get('response').get('id')
+ self._response_model = chunked.get('response').get('model')
+ self._finish_reason = chunked.get('response').get('status')
+ self._input_tokens = chunked.get('response').get('usage').get('input_tokens')
+ self._output_tokens = chunked.get('response').get('usage').get('output_tokens')
+ return chunk
+ except StopIteration:
+ # Handling exception ensure observability without disrupting operation
+ try:
+ self._end_time = time.time()
+ if len(self._timestamps) > 1:
+ self._tbt = calculate_tbt(self._timestamps)
+
+ try:
+ formatted_messages = extract_and_format_input(self._kwargs.get('input', ''))
+ prompt = concatenate_all_contents(formatted_messages)
+ except:
+ prompt = self._kwargs.get('input', '')
+
+ request_model = self._kwargs.get("model", "gpt-4o")
+
+ # Calculate cost of the operation
+ cost = get_chat_model_cost(request_model,
+ pricing_info, self._input_tokens,
+ self._output_tokens)
+
+ # Set Span attributes (OTel Semconv)
+ self._span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
+ self._span.set_attribute(SemanticConvetion.GEN_AI_OPERATION,
+ SemanticConvetion.GEN_AI_OPERATION_TYPE_CHAT)
+ self._span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM,
+ SemanticConvetion.GEN_AI_SYSTEM_OPENAI)
+ self._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MODEL,
+ request_model)
+ self._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_SEED,
+ self._kwargs.get("seed", ""))
+ self._span.set_attribute(SemanticConvetion.SERVER_PORT,
+ self._server_port)
+ self._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MAX_TOKENS,
+ self._kwargs.get("max_output_tokens", -1))
+ self._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_STOP_SEQUENCES,
+ self._kwargs.get("stop", []))
+ self._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TEMPERATURE,
+ self._kwargs.get("temperature", 1.0))
+ self._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TOP_P,
+ self._kwargs.get("top_p", 1.0))
+ self._span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_FINISH_REASON,
+ [self._finish_reason])
+ self._span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_ID,
+ self._response_id)
+ self._span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_MODEL,
+ self._response_model)
+ self._span.set_attribute(SemanticConvetion.GEN_AI_USAGE_INPUT_TOKENS,
+ self._input_tokens)
+ self._span.set_attribute(SemanticConvetion.GEN_AI_USAGE_OUTPUT_TOKENS,
+ self._output_tokens)
+ self._span.set_attribute(SemanticConvetion.SERVER_ADDRESS,
+ self._server_address)
+ if isinstance(self._llmresponse, str):
+ self._span.set_attribute(SemanticConvetion.GEN_AI_OUTPUT_TYPE,
+ "text")
+ else:
+ self._span.set_attribute(SemanticConvetion.GEN_AI_OUTPUT_TYPE,
+ "json")
+
+ # Set Span attributes (Extra)
+ self._span.set_attribute(DEPLOYMENT_ENVIRONMENT,
+ environment)
+ self._span.set_attribute(SERVICE_NAME,
+ application_name)
+ self._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_USER,
+ self._kwargs.get("user", ""))
+ self._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_IS_STREAM,
+ True)
+ self._span.set_attribute(SemanticConvetion.GEN_AI_USAGE_TOTAL_TOKENS,
+ self._input_tokens + self._output_tokens)
+ self._span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,
+ cost)
+ self._span.set_attribute(SemanticConvetion.GEN_AI_SERVER_TBT,
+ self._tbt)
+ self._span.set_attribute(SemanticConvetion.GEN_AI_SERVER_TTFT,
+ self._ttft)
+ self._span.set_attribute(SemanticConvetion.GEN_AI_SDK_VERSION,
+ version)
+
+ if capture_message_content:
+ self._span.add_event(
+ name=SemanticConvetion.GEN_AI_CONTENT_PROMPT_EVENT,
+ attributes={
+ SemanticConvetion.GEN_AI_CONTENT_PROMPT: prompt,
+ },
+ )
+ self._span.add_event(
+ name=SemanticConvetion.GEN_AI_CONTENT_COMPLETION_EVENT,
+ attributes={
+ SemanticConvetion.GEN_AI_CONTENT_COMPLETION: self._llmresponse,
+ },
+ )
+ self._span.set_status(Status(StatusCode.OK))
+
+ if disable_metrics is False:
+ attributes = create_metrics_attributes(
+ service_name=application_name,
+ deployment_environment=environment,
+ operation=SemanticConvetion.GEN_AI_OPERATION_TYPE_CHAT,
+ system=SemanticConvetion.GEN_AI_SYSTEM_OPENAI,
+ request_model=request_model,
+ server_address=self._server_address,
+ server_port=self._server_port,
+ response_model=self._response_model,
+ )
+
+ metrics["genai_client_usage_tokens"].record(
+ self._input_tokens + self._output_tokens, attributes
+ )
+ metrics["genai_client_operation_duration"].record(
+ self._end_time - self._start_time, attributes
+ )
+ metrics["genai_server_tbt"].record(
+ self._tbt, attributes
+ )
+ metrics["genai_server_ttft"].record(
+ self._ttft, attributes
+ )
+ metrics["genai_requests"].add(1, attributes)
+ metrics["genai_completion_tokens"].add(self._output_tokens, attributes)
+ metrics["genai_prompt_tokens"].add(self._input_tokens, attributes)
+ metrics["genai_cost"].record(cost, attributes)
+
+ except Exception as e:
+ handle_exception(self._span, e)
+ logger.error("Error in trace creation: %s", e)
+ finally:
+ self._span.end()
+ raise
+
+ def wrapper(wrapped, instance, args, kwargs):
+ """
+ Wraps the 'chat.completions' API call to add telemetry.
+
+ This collects metrics such as execution time, cost, and token usage, and handles errors
+ gracefully, adding details to the trace for observability.
+
+ Args:
+ wrapped: The original 'chat.completions' method to be wrapped.
+ instance: The instance of the class where the original method is defined.
+ args: Positional arguments for the 'chat.completions' method.
+ kwargs: Keyword arguments for the 'chat.completions' method.
+
+ Returns:
+ The response from the original 'chat.completions' method.
+ """
+
+ # Check if streaming is enabled for the API call
+ streaming = kwargs.get("stream", False)
+ server_address, server_port = set_server_address_and_port(instance, "api.openai.com", 443)
+ request_model = kwargs.get("model", "gpt-4o")
+
+ span_name = f"{SemanticConvetion.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
+
+ # pylint: disable=no-else-return
+ if streaming:
+ # Special handling for streaming response to accommodate the nature of data flow
+ awaited_wrapped = wrapped(*args, **kwargs)
+ span = tracer.start_span(span_name, kind=SpanKind.CLIENT)
+
+ return TracedSyncStream(awaited_wrapped, span, kwargs, server_address, server_port)
+
+ # Handling for non-streaming responses
+ else:
+ with tracer.start_as_current_span(span_name, kind= SpanKind.CLIENT) as span:
+ start_time = time.time()
+ response = wrapped(*args, **kwargs)
+ end_time = time.time()
+
+ response_dict = response_as_dict(response)
+
+ try:
+ try:
+ formatted_messages = extract_and_format_input(kwargs.get('input', ''))
+ prompt = concatenate_all_contents(formatted_messages)
+ except:
+ prompt = kwargs.get('input', '')
+
+ input_tokens = response_dict.get('usage').get('input_tokens')
+ output_tokens = response_dict.get('usage').get('output_tokens')
+
+ # Calculate cost of the operation
+ cost = get_chat_model_cost(request_model,
+ pricing_info, input_tokens,
+ output_tokens)
+
+ # Set base span attribues (OTel Semconv)
+ span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
+ span.set_attribute(SemanticConvetion.GEN_AI_OPERATION,
+ SemanticConvetion.GEN_AI_OPERATION_TYPE_CHAT)
+ span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM,
+ SemanticConvetion.GEN_AI_SYSTEM_OPENAI)
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MODEL,
+ request_model)
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_SEED,
+ kwargs.get("seed", ""))
+ span.set_attribute(SemanticConvetion.SERVER_PORT,
+ server_port)
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MAX_TOKENS,
+ kwargs.get("max_output_tokens", -1))
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_STOP_SEQUENCES,
+ kwargs.get("stop", []))
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TEMPERATURE,
+ str(response_dict.get("temperature", 1.0)))
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TOP_P,
+ str(response_dict.get("top_p", 1.0)))
+ span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_ID,
+ response_dict.get("id"))
+ span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_MODEL,
+ response_dict.get('model'))
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_INPUT_TOKENS,
+ input_tokens)
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_OUTPUT_TOKENS,
+ output_tokens)
+ span.set_attribute(SemanticConvetion.SERVER_ADDRESS,
+ server_address)
+
+ # Set base span attribues (Extras)
+ span.set_attribute(DEPLOYMENT_ENVIRONMENT,
+ environment)
+ span.set_attribute(SERVICE_NAME,
+ application_name)
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_USER,
+ kwargs.get("user", ""))
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_IS_STREAM,
+ False)
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_TOTAL_TOKENS,
+ input_tokens + output_tokens)
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,
+ cost)
+ span.set_attribute(SemanticConvetion.GEN_AI_SERVER_TTFT,
+ end_time - start_time)
+ span.set_attribute(SemanticConvetion.GEN_AI_SDK_VERSION,
+ version)
+
+ if capture_message_content:
+ span.add_event(
+ name=SemanticConvetion.GEN_AI_CONTENT_PROMPT_EVENT,
+ attributes={
+ SemanticConvetion.GEN_AI_CONTENT_PROMPT: prompt,
+ },
+ )
+
+ for i in range(kwargs.get('n',1)):
+ span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_FINISH_REASON,
+ [response_dict.get('status')])
+ try:
+ llm_response = response_dict.get('output')[i].get('content')[0].get('text','')
+ except:
+ llm_response = ''
+
+ if capture_message_content:
+ span.add_event(
+ name=SemanticConvetion.GEN_AI_CONTENT_COMPLETION_EVENT,
+ attributes={
+ # pylint: disable=line-too-long
+ SemanticConvetion.GEN_AI_CONTENT_COMPLETION: llm_response,
+ },
+ )
+ if kwargs.get('tools'):
+ span.set_attribute(SemanticConvetion.GEN_AI_TOOL_CALLS,
+ str(response_dict.get('tools')))
+
+ if isinstance(llm_response, str):
+ span.set_attribute(SemanticConvetion.GEN_AI_OUTPUT_TYPE,
+ "text")
+ elif llm_response is not None:
+ span.set_attribute(SemanticConvetion.GEN_AI_OUTPUT_TYPE,
+ "json")
+
+ span.set_status(Status(StatusCode.OK))
+
+ if disable_metrics is False:
+ attributes = create_metrics_attributes(
+ service_name=application_name,
+ deployment_environment=environment,
+ operation=SemanticConvetion.GEN_AI_OPERATION_TYPE_CHAT,
+ system=SemanticConvetion.GEN_AI_SYSTEM_OPENAI,
+ request_model=request_model,
+ server_address=server_address,
+ server_port=server_port,
+ response_model=response_dict.get('model'),
+ )
+
+ metrics["genai_client_usage_tokens"].record(
+ input_tokens + output_tokens, attributes
+ )
+ metrics["genai_client_operation_duration"].record(
+ end_time - start_time, attributes
+ )
+ metrics["genai_server_ttft"].record(
+ end_time - start_time, attributes
+ )
+ metrics["genai_requests"].add(1, attributes)
+ metrics["genai_completion_tokens"].add(output_tokens, attributes)
+ metrics["genai_prompt_tokens"].add(input_tokens, attributes)
+ metrics["genai_cost"].record(cost, attributes)
+
+ # Return original response
+ return response
+
+ except Exception as e:
+ handle_exception(span, e)
+ logger.error("Error in trace creation: %s", e)
+
+ # Return original response
+ return response
+
+ return wrapper
+
def chat_completions(version, environment, application_name,
tracer, pricing_info, capture_message_content, metrics, disable_metrics):
"""
diff --git a/sdk/python/src/openlit/instrumentation/openai_agents/__init__.py b/sdk/python/src/openlit/instrumentation/openai_agents/__init__.py
new file mode 100644
index 00000000..fe58cc35
--- /dev/null
+++ b/sdk/python/src/openlit/instrumentation/openai_agents/__init__.py
@@ -0,0 +1,42 @@
+"""Initializer of Auto Instrumentation of OpenAI Agents Functions"""
+
+from typing import Collection
+import importlib.metadata
+from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
+from wrapt import wrap_function_wrapper
+
+from openlit.instrumentation.openai_agents.openai_agents import (
+ create_agent
+)
+
+_instruments = ('openai-agents >= 0.0.3',)
+
+class OpenAIAgentsInstrumentor(BaseInstrumentor):
+ """
+ An instrumentor for openai-agents's client library.
+ """
+
+ def instrumentation_dependencies(self) -> Collection[str]:
+ return _instruments
+
+ def _instrument(self, **kwargs):
+ application_name = kwargs.get('application_name', 'default')
+ environment = kwargs.get('environment', 'default')
+ tracer = kwargs.get('tracer')
+ event_provider = kwargs.get('event_provider')
+ metrics = kwargs.get('metrics_dict')
+ pricing_info = kwargs.get('pricing_info', {})
+ capture_message_content = kwargs.get('capture_message_content', False)
+ disable_metrics = kwargs.get('disable_metrics')
+ version = importlib.metadata.version('openai-agents')
+
+ wrap_function_wrapper(
+ 'agents.agent',
+ 'Agent.__init__',
+ create_agent(version, environment, application_name,
+ tracer, event_provider, pricing_info, capture_message_content, metrics, disable_metrics),
+ )
+
+ def _uninstrument(self, **kwargs):
+ # Proper uninstrumentation logic to revert patched methods
+ pass
diff --git a/sdk/python/src/openlit/instrumentation/openai_agents/openai_agents.py b/sdk/python/src/openlit/instrumentation/openai_agents/openai_agents.py
new file mode 100644
index 00000000..33dd0372
--- /dev/null
+++ b/sdk/python/src/openlit/instrumentation/openai_agents/openai_agents.py
@@ -0,0 +1,65 @@
+"""
+Module for monitoring AG2 API calls.
+"""
+
+import logging
+from opentelemetry.trace import SpanKind, Status, StatusCode
+from opentelemetry.sdk.resources import SERVICE_NAME, TELEMETRY_SDK_NAME, DEPLOYMENT_ENVIRONMENT
+from openlit.__helpers import (
+ handle_exception,
+)
+from openlit.semcov import SemanticConvetion
+
+# Initialize logger for logging potential issues and operations
+logger = logging.getLogger(__name__)
+
+def set_span_attributes(span, version, operation_name, environment,
+ application_name, server_address, server_port, request_model):
+ """
+ Set common attributes for the span.
+ """
+
+ # Set Span attributes (OTel Semconv)
+ span.set_attribute(TELEMETRY_SDK_NAME, 'openlit')
+ span.set_attribute(SemanticConvetion.GEN_AI_OPERATION, operation_name)
+ span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM, SemanticConvetion.GEN_AI_SYSTEM_AG2)
+ span.set_attribute(SemanticConvetion.SERVER_ADDRESS, server_address)
+ span.set_attribute(SemanticConvetion.SERVER_PORT, server_port)
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MODEL, request_model)
+
+ # Set Span attributes (Extras)
+ span.set_attribute(DEPLOYMENT_ENVIRONMENT, environment)
+ span.set_attribute(SERVICE_NAME, application_name)
+ span.set_attribute(SemanticConvetion.GEN_AI_SDK_VERSION, version)
+
+def create_agent(version, environment, application_name,
+ tracer, event_provider, pricing_info, capture_message_content, metrics, disable_metrics):
+ """
+ Generates a telemetry wrapper for GenAI function call
+ """
+ def wrapper(wrapped, instance, args, kwargs):
+ server_address, server_port = '127.0.0.1', 80
+
+ agent_name = kwargs.get('name', 'openai_agent')
+ span_name = f'{SemanticConvetion.GEN_AI_OPERATION_TYPE_CREATE_AGENT} {agent_name}'
+
+ with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
+ try:
+ response = wrapped(*args, **kwargs)
+
+ set_span_attributes(span, version, SemanticConvetion.GEN_AI_OPERATION_TYPE_CREATE_AGENT,
+ environment, application_name, server_address, server_port, kwargs.get('model', 'gpt-4o'))
+ span.set_attribute(SemanticConvetion.GEN_AI_AGENT_NAME, agent_name)
+
+ span.set_attribute(SemanticConvetion.GEN_AI_AGENT_DESCRIPTION, kwargs.get('instructions', ''))
+
+ span.set_status(Status(StatusCode.OK))
+
+ return response
+
+ except Exception as e:
+ handle_exception(span, e)
+ logger.error('Error in trace creation: %s', e)
+ return response
+
+ return wrapper