Instrument LLM applications with OpenTelemetry GenAI Utils
When your Generative AI (GenAI) applications use custom frameworks or multi-language architectures, the ARMS probe may not automatically collect telemetry data from Large Language Model (LLM), Agent, or Tool calls. OpenTelemetry GenAI Utils (GenAI Utils) provides a manual instrumentation solution that generates standard Spans conforming to GenAI Semantic Conventions and reports them to ARMS Tracing OpenTelemetry Edition. This guide uses Python to walk you through the integration process, from environment setup to data verification.
Use cases
As LLM and AI Agent applications grow, observability becomes critical. You need a unified way to collect and analyze multi-turn interactions between Agents and Model Context Protocol (MCP) tools, scheduling traces of inference engines like vLLM and SGLang, and metrics like token usage and Time to First Token (TTFT). ARMS Application Monitoring provides out-of-the-box probes for common GenAI scenarios. The end-to-end trace based on the MCP protocol shows how the Python probe instruments the call chain of LangChain Agents and MCP Servers with zero code changes. The guide to observability for vLLM/SGLang inference engines covers request-level Spans, gen_ai.latency.* attributes, and metrics like TTFT and TPOT. For custom frameworks, multi-language architectures, or scenarios requiring fine-grained Span control, you must manually instrument LLM calls, Agent orchestration, and Tool execution in your application code. GenAI Utils addresses these scenarios.
GenAI Utils complements Application Monitoring probes rather than replacing them. Probes automatically instrument known frameworks (LangChain, vLLM, MCP) with a no-code or low-code approach, ideal for standardized deployments. GenAI Utils requires explicit Handler API calls in your application code, making it suitable for custom Agent frameworks, custom Span attributes, or integrations not yet covered by probes. Both produce telemetry that follows the same GenAI Semantic Conventions and can be analyzed together in the ARMS console or any OpenTelemetry-compatible backend.
How it works
GenAI Utils is a shared telemetry library for generative AI operations. It maps operations such as LLM inference, Agent calls, embedding, Tool execution, and retrieval to standard Spans, Events, and metrics that follow the OpenTelemetry GenAI Semantic Conventions. It also provides unified lifecycle APIs such as start, stop, and fail.
|
Problem |
Solution |
|
Frameworks define trace fields inconsistently, making cross-platform analysis difficult. |
Unifies data using standard attributes such as |
|
Manual Span creation can miss key information like token usage and TTFT. |
Provides the |
|
Lack of unified conventions for Agent, Tool, and Retrieval scenarios. |
Extends support for operations such as |
|
Balancing message content collection with privacy compliance. |
Provides flexible control through environment variables such as |
The OpenTelemetry community first released a Python reference implementation, opentelemetry-util-genai. Equivalent implementations for Java, Go, and Node.js followed, each using the same API design and Semantic Conventions.
A typical data flow is as follows:
Application code + GenAI Utils (generates Spans)
↓
OpenTelemetry SDK / Exporter (configures endpoint and authentication)
↓
ARMS Tracing OpenTelemetry Edition
↓
ARMS console Trace Explorer
Prerequisites
GenAI Utils generates Spans and metrics that follow Semantic Conventions, but reporting this data to ARMS Tracing OpenTelemetry Edition requires a configured OpenTelemetry SDK and Exporter. Complete the following prerequisites before writing your application code:
-
Activate the ARMS Tracing OpenTelemetry Edition service.
-
Obtain the endpoint domain, port, and authentication token for your region.
-
Configure the OpenTelemetry SDK or Collector to report trace data to ARMS over a protocol such as OTLP.
For integration details, regional endpoints, port and protocol mappings, and SDK or Collector configuration, see the integration guide. It covers:
-
Prerequisites: How to obtain endpoints, ports, and authentication tokens.
-
OpenTelemetry integration: How to configure data reporting with the OpenTelemetry SDK or Collector.
-
Other client integration methods (Jaeger, Zipkin, SkyWalking, etc.).
If your application is deployed outside Alibaba Cloud, you must also open the required ports and grant domain access permissions as described in the integration guide.
Quick start (Python example)
Python was the first language to feature GenAI Utils, and its API design serves as a reference for other languages. The following example uses TelemetryHandler to collect a standard GenAI Span for an LLM call.
Install GenAI Utils and its dependent OpenTelemetry SDK and Exporter packages:
pip install opentelemetry-util-genai
Before you run the following code, you must initialize and configure the OpenTelemetry TracerProvider and Exporter as described in the Prerequisites section. This ensures that Span data can be reported to ARMS.
from opentelemetry.util.genai.handler import get_telemetry_handler
from opentelemetry.util.genai.types import InputMessage, LLMInvocation, OutputMessage, Text
handler = get_telemetry_handler()
invocation = LLMInvocation(
provider="openai",
request_model="gpt-4o-mini",
input_messages=[
InputMessage(role="user", parts=[Text(content="Explain OpenTelemetry in one sentence.")]),
],
)
with handler.llm(invocation) as inv:
# Call the actual model API here
inv.output_messages = [
OutputMessage(
role="assistant",
parts=[Text(content="OpenTelemetry is an open standard and implementation for observability.")],
finish_reason="stop",
)
]
inv.input_tokens = 10
inv.output_tokens = 20
The integration steps are: install dependencies → initialize the OpenTelemetry Provider → create a handler → wrap the model API call with with handler.llm(...). For additional operation types or message content collection policies, see the documentation for your programming language.
Verify data reporting
After you run the example code, log on to the ARMS console. On the Trace Explorer page, view the reported GenAI Span. The Span should contain standard attributes such as gen_ai.system, gen_ai.request.model, and gen_ai.usage.*, which correspond to the provider, request_model, and token usage values set in your code.
GenAI Utils documentation
All language implementations follow the same Semantic Conventions and Handler API design. For installation, API references, and code examples, see the documentation for your language:
|
Language |
Documentation |
|
Java |
|
|
Python |
|
|
Go |
|
|
Node.js |