|
| 1 | +--- |
| 2 | +title: 'How to enable tracing in Azure AI Agents' |
| 3 | +titleSuffix: Azure AI services |
| 4 | +description: Learn how to trace your AI agent's executions for debugging and evaluation. |
| 5 | +services: cognitive-services |
| 6 | +manager: nitinme |
| 7 | +ms.service: azure |
| 8 | +ms.topic: conceptual |
| 9 | +ms.date: 12/03/2024 |
| 10 | +author: aahill |
| 11 | +ms.author: aahi |
| 12 | +--- |
| 13 | + |
| 14 | +# Tracing using Application Insights |
| 15 | + |
| 16 | +Determining the reasoning behind your agent's executions is important for troubleshooting and debugging. However, it can be difficult for complex agents for a number of reasons: |
| 17 | +* There could be a high number of steps involved in generating a response, making it hard to keep track of all of them. |
| 18 | +* The sequence of steps might vary based on user input. |
| 19 | +* The inputs/outputs at each stage might be long and deserve more detailed inspection. |
| 20 | +* Each step of an agent's runtime might also involve nesting. For example, an agent might invoke a tool, which uses another process, which then invokes another tool. If you notice strange or incorrect output from a top-level agent run, it might be difficult to determine exactly where in the execution the issue was introduced. |
| 21 | + |
| 22 | +Tracing solves this by allowing you to clearly see the inputs and outputs of each primitive involved in a particular agent run, in the order in which they were invoked. |
| 23 | + |
| 24 | +## Creating an Application Insights resource |
| 25 | + |
| 26 | +Tracing lets you analyze your agent's performance and behavior by using OpenTelemetry and adding an Application Insights resource to your Azure AI Foundry project. |
| 27 | + |
| 28 | +To add an Application Insights resource, navigate to the **Tracing** tab in the [AI Foundry portal](https://ai.azure.com/), and create a new resource if you don't already have one. |
| 29 | + |
| 30 | +:::image type="content" source="../media/ai-foundry-tracing.png" alt-text="A screenshot of the tracing screen in the AI Foundry portal." lightbox="../media/ai-foundry-tracing.png"::: |
| 31 | + |
| 32 | +Once created, you can get an Application Insights connection string, configure your agents, and observe the full execution path of your agent through Azure Monitor. Typically you want to enable tracing before you create an agent. |
| 33 | + |
| 34 | +## Trace an agent |
| 35 | + |
| 36 | +First, use `pip install` to install OpenTelemetry and the Azure SDK tracing plugin. |
| 37 | + |
| 38 | +```bash |
| 39 | +pip install opentelemetry |
| 40 | +pip install azure-core-tracing-opentelemetry |
| 41 | +``` |
| 42 | + |
| 43 | +You will also need an exporter to send results to your observability backend. You can print traces to the console or use a local viewer such as [Aspire Dashboard](/dotnet/aspire/fundamentals/dashboard/standalone?tabs=bash). To connect to Aspire Dashboard or another OpenTelemetry compatible backend, install the OpenTelemetry Protocol (OTLP) exporter. |
| 44 | + |
| 45 | +```bash |
| 46 | +pip install opentelemetry-exporter-otlp |
| 47 | +``` |
| 48 | + |
| 49 | +Once you have the packages installed, you can use one the following Python samples to implement tracing with your agents. Samples that use console tracing display the results locally in the console. Samples that use Azure Monitor send the traces to the Azure Monitor in the [AI Foundry portal](https://ai.azure.com/), in the **Tracing** tab in the left navigation menu for the portal. |
| 50 | + |
| 51 | +> [!NOTE] |
| 52 | +> There is a known bug in the agents tracing functionality. The bug will cause the agent's function tool to call related info (function names and parameter values, which could contain sensitive information) to be included in the traces even when content recording is not enabled. |
| 53 | +
|
| 54 | + |
| 55 | +**Using Azure Monitor** |
| 56 | +* [Basic agent example](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-projects/samples/agents/sample_agents_basics_with_azure_monitor_tracing.py) |
| 57 | +* [Agent example with function calling](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-projects/samples/agents/sample_agents_functions_with_azure_monitor_tracing.py) |
| 58 | +* [Example with a stream event handler](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-projects/samples/agents/sample_agents_stream_eventhandler_with_azure_monitor_tracing.py) |
| 59 | + |
| 60 | +**Using console tracing** |
| 61 | +* [Basic agent example](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-projects/samples/agents/sample_agents_basics_with_console_tracing.py) |
| 62 | +* [Agent example with function calling](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-projects/samples/agents/sample_agents_functions_with_console_tracing.py) |
| 63 | +* [Example with a stream event handler](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-projects/samples/agents/sample_agents_stream_eventhandler_with_console_tracing.py) |
| 64 | + |
| 65 | + |
0 commit comments