Skip to main content
Items marked (preview) in this article are currently in public preview. This preview is provided without a service-level agreement, and we don’t recommend it for production workloads. Certain features might not be supported or might have constrained capabilities. For more information, see Supplemental Terms of Use for Microsoft Azure Previews.
Microsoft Foundry automatically captures server-side traces for agents running in the portal. Client-side tracing extends that visibility into your own application code. By instrumenting your agent application with OpenTelemetry, you can capture spans for model calls, tool invocations, and custom logic — then export them to Azure Monitor Application Insights, the console, or any observability backend that supports the OpenTelemetry protocol (OTLP), such as Datadog, Grafana Tempo, Jaeger, or Honeycomb. In this article, you learn how to:
  • Install the required OpenTelemetry tracing packages.
  • Enable GenAI tracing instrumentation for agent applications.
  • Export traces to Azure Monitor, the console, or an OTLP-compatible backend.
  • Enable content recording to capture message contents.
  • Enable trace context propagation for distributed tracing (Python).
  • Trace custom functions.

Prerequisites

Language-specific prerequisites

  • Python 3.9 or later.
  • The azure-ai-projects package version 2.0.0 or later.

Install tracing packages

Install the Microsoft Foundry SDK, OpenTelemetry, and the Azure Monitor exporter:
For console-only or OTLP export (for example, Aspire Dashboard), install the OTLP exporter:

Enable GenAI tracing

GenAI tracing instrumentation is an experimental preview feature. Spans, attributes, and events might change in future versions. You must explicitly opt in before tracing is active.
Set the AZURE_EXPERIMENTAL_ENABLE_GENAI_TRACING environment variable to true before calling AIProjectInstrumentor().instrument():
If the variable isn’t set or isn’t true (case-insensitive), tracing instrumentation isn’t enabled and a warning is logged.

Export traces to Azure Monitor

Send traces to Azure Application Insights so they appear in the Foundry portal’s Traces view and in Azure Monitor.
Reference: AIProjectClient, DefaultAzureCredential, configure_azure_monitor
To correlate traces with a specific agent in the Foundry portal, include the agent_reference with both name and id in your responses.create() call (as shown in the Python sample above). Traces typically appear within 2-5 minutes.

Export traces to the console

Console export is useful for local debugging. Traces print directly to standard output.
You can also use Aspire Dashboard as a local OTLP-compatible viewer. Install the OTLP exporter (pip install opentelemetry-exporter-otlp) and configure it as the exporter instead of ConsoleSpanExporter.Reference: AIProjectInstrumentor, ConsoleSpanExporter

Enable content recording

Content recording captures message contents and tool call arguments in traces. This data might include sensitive user information.
Content recording captures user messages, tool call arguments, and model outputs. Only enable this setting in development environments. Don’t enable content recording in production unless your compliance and privacy requirements allow it.
Set the environment variable before instrumenting:
This variable controls recording only for built-in traces. When you use the @trace_function decorator on your own functions, all parameters and return values are always traced regardless of this setting.

Disable automatic instrumentation (Python)

The Python SDK automatically instruments OpenAI Responses and Conversations API calls. To disable this auto-instrumentation, set AZURE_TRACING_GEN_AI_INSTRUMENT_RESPONSES_API to false before calling AIProjectInstrumentor().instrument(). When disabled, only explicit custom spans are recorded.

Trace binary data (Python)

When content recording is enabled, the SDK traces file IDs and filenames by default. To include full image URLs (including base64 data URIs) and file data in spans, set AZURE_TRACING_GEN_AI_INCLUDE_BINARY_DATA to true.
Enabling AZURE_TRACING_GEN_AI_INCLUDE_BINARY_DATA can significantly increase trace payload size. Some tracing backends have limitations on the maximum size of span data. Verify that your observability backend supports the expected payload sizes before enabling this setting.

Enable trace context propagation (Python)

Trace context propagation allows client-side spans to correlate with server-side spans from Azure OpenAI and other Azure services. When enabled, the SDK automatically injects W3C Trace Context headers (traceparent and tracestate) into HTTP requests made by OpenAI clients obtained via get_openai_client(). Trace context propagation is enabled by default when tracing is enabled. To disable it, set the AZURE_TRACING_GEN_AI_ENABLE_TRACE_CONTEXT_PROPAGATION environment variable to false, or pass the parameter directly:
Changes to this setting only affect OpenAI clients obtained via get_openai_client() after the change. Previously acquired clients aren’t affected.

Control baggage propagation (Python)

By default, only traceparent and tracestate headers are propagated. To also include the baggage header, set AZURE_TRACING_GEN_AI_TRACE_CONTEXT_PROPAGATION_INCLUDE_BAGGAGE to true.
The baggage header can contain arbitrary key-value pairs, including user identifiers, session information, or other potentially sensitive data. Before enabling baggage propagation:
  • Audit what data your application and third-party libraries add to OpenTelemetry baggage.
  • Understand that baggage is sent to Azure OpenAI and might be logged by Azure services.
  • Never add sensitive information to baggage when propagation is enabled.
The C# SDK relies on standard .NET System.Diagnostics.Activity propagation. Explicit per-request trace context injection isn’t exposed as a separate SDK feature.

Trace custom functions

Python — use the @trace_function decorator

The trace_function decorator creates an OpenTelemetry span for each call to your function. Parameters are recorded as code.function.parameter.<name> and the return value as code.function.return.value.
To use a custom span name instead of the function name, pass it as a parameter:
The decorator records:
  • Parameters as code.function.parameter.<name> span attributes.
  • Return values as code.function.return.value.
  • Supported types: str, int, float, bool, and collections (list, dict, tuple, set). Object types are omitted.
The OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT environment variable doesn’t affect custom function tracing. The @trace_function decorator always traces parameters and return values.

C# — use ActivitySource manually

The C# SDK doesn’t include a tracing decorator. Use the standard .NET ActivitySource to instrument your own functions:
Register your custom source alongside the SDK source in your tracer provider:

Configure instrumentation programmatically (Python)

As an alternative to environment variables, pass configuration parameters directly to AIProjectInstrumentor().instrument():
* Default is True when tracing is enabled. When both a parameter and its corresponding environment variable are set, the parameter value takes priority.

Add custom attributes to spans (Python)

Create a custom SpanProcessor to inject metadata like session IDs into every span:
Register the processor with the global tracer provider:

Control tracing behavior with environment variables

The following table lists all environment variables you can use to configure tracing behavior: * Default is true when tracing is enabled. For the full list of environment variables and their behavior, see Tracing in the Azure AI Projects SDK README.

Security and privacy

Client-side tracing can capture sensitive information. Follow these practices to reduce risk:
  • Content recording: Captures user inputs, model responses, and tool call arguments. Disable in production unless required.
  • Baggage propagation: Can expose PII and session data. Disabled by default.
  • Trace context propagation: Sends trace IDs to Azure services. If compliance requirements prohibit sharing trace identifiers, disable it.
  • Secrets: Don’t store secrets, credentials, or tokens in prompts, tool arguments, or span attributes.
  • Access control: Treat trace data as production telemetry. Apply the same access controls and retention policies you use for logs and metrics.

Troubleshooting