Retrieved article excerpt
Open article · Retrieved 2026-11-16T08:21:03.037095+00:00
# AI Agent Observability with OpenTelemetry
Last Updated: October 19, 202618 min read
AI agents don't just call a model and return its response. Instead, they orchestrate entire workflows that involve selecting tools for LLMs to use, evaluating guardrails, routing between LLM calls, and managing context across conversation turns. This means that a single user request can spawn dozens of internal steps, each one a potential point of failure, latency, or unexpected cost.
AI agent observability is the practice of capturing these execution flows so you can understand *what* the agent did, *why* it chose that path, and *how* it performed. In practice, this means tracing an agent run's execution path, capturing inputs and outputs at each step, and tracking cost, token usage, latency, and error rates.
AI agent tracing builds the foundation for AI agent observability. Without tracing, debugging an AI agent becomes a game of trying to guess which event triggered what decision in the agent workflow, and why.
## Why do AI agents need observability?
A request can return successfully while the agent gives an incorrect answer, takes too long on a particular step, or triggers duplicate LLM or tool calls. Agent observability gives engineers the evidence to investigate these problems and compare behavior across runs.
Let's look at some of these problems in more detail.
### LLM non-determinism compounds across agent steps
A single LLM call is already non-deterministic: the same prompt can produce different outputs across runs. Agents amplify this because they chain multiple LLM calls together, where the output of one step determines the input of the next.
This means variance doesn't just exist at one point. Rather, it compounds across the workflow. A slightly different model response in step one can cause the agent to select a completely different tool in step two, which changes the context passed to step three, and so on. Reproducing a specific agent run becomes nearly impossible without capturing the full execution path.
### Agents can silently produce incorrect results
Traditional software can also return incorrect results without throwing an error. With agents, these failures can be harder to catch because each run can take a different path: the workflow completes without any error, but the agent selected the wrong tool, retrieved stale context, or returned a response that is confidently incorrect.
Without observability, these silent failures only surface when a user complains or when downstream systems consume bad output. Tracing each decision in the agentic loop, such as which tool was selected, what context was available, what the model actually returned, is the only reliable way to evaluate whether an agent's "successful" run was actually logically correct.
### Cost and latency are unpredictable by design
Agents decide at runtime how many tools to call and how many LLM roundtrips to make. A simple user query might resolve in one LLM call, while a similar query might trigger several tool calls and model invocations depending on the agent's reasoning.
This makes cost and latency inherently unpredictable. Observability lets engineers track token usage, tool call frequency, and per-step latency across runs to identify expensive patterns early. For example, if two models with different pricing tiers perform comparably for a given workload, engineers can switch to the cheaper option — but only if they have the data to make that comparison.
## How does OpenTelemetry enable AI agent observability?
[OpenTelemetry](https://signoz.io/opentelemetry/) (OTel) is a CNCF graduated project for instrumenting applications to emit telemetry data (primarily [traces, metrics, and logs](https://signoz.io/blog/o11y/)) in a vendor-neutral format. What makes it uniquely suited for agentic applications is that your visibility extends beyond the AI agent to the entire request: what endpoint did the user call, what DB queries ran, were any background jobs enqueued, did the application make any downstream API calls?
OpenTelemetry enables developers to instrument entire applications, including their AI agents.
*OpenTelemetry enables developers to instrument entire applications, including their AI agents.*
## Setting up AI agent observability with OpenTelemetry
### Prerequisites
- Python 3.12 or newer. Download the [latest version](https://www.python.org/downloads/).
- A [SigNoz Cloud account](https://signoz.io/teams/) for visualizing telemetry data.
- An [OpenAI API key](https://platform.openai.com/api-keys) to run the agent.
- An API client like [Bruno](https://www.usebruno.com/) or [Postman](https://www.postman.com/) to test endpoints.
### Setting up SigNoz Cloud
SigNoz Cloud is an OpenTelemetry-native observability platform that unifies traces, metrics, and logs in a single interface.
- [Sign up](https://signoz.io/teams/) for a free SigNoz Cloud account.
- [Follow the documentation](https://signoz.io/docs/ingestion/signoz-cloud/keys/) to generate an ingestion key.
- Keep your workspace region (e.g., `us`, `in`, `eu`) and ingestion key handy for the application configuration.
### Running the Demo Application
Clone and navigate to the demo application:
Copy
```
git clone --filter=blob:none --sparse https://github.com/SigNoz/examples.git
git -C examples sparse-checkout set python/otel-ai-agent-observability-demo
cd examples/python/otel-ai-agent-observability-demo
```
Create and activate a Python virtual environment:
Copy
```
python3.12 -m venv .venv
source .venv/bin/activate
```
Install the application and [OpenTelemetry Python](https://signoz.io/opentelemetry/python/) dependencies:
Copy
```
pip install -r requirements.txt
```
The [`requirements.txt` file](https://github.com/SigNoz/examples/blob/main/python/otel-ai-agent-observability-demo/requirements.txt) pins the following OTel packages:
- `opentelemetry-distro`: Provides a convenient mechanism to configure some of the more common options for Python auto-instrumentation.
- `opentelemetry-exporter-otlp`: Installs [the OTLP exporters](https://signoz.io/blog/what-is-otlp/) needed to deliver telemetry data to any [OpenTelemetry-compatible backend](https://signoz.io/blog/opentelemetry-backend/).
- `opentelemetry-instrumentation-genai-openai` and `opentelemetry-instrumentation-genai-openai-agents`: Modern OpenAI Agents and OpenAI Python API instrumentation packages from the dedicated [OpenTelemetry GenAI repository](https://github.com/open-telemetry/opentelemetry-python-genai).
Next, run `opentelemetry-bootstrap` to detect widely used Python libraries (like the [FastAPI web framework](https://signoz.io/blog/opentelemetry-fastapi/) and HTTPX client library) and install their corresponding instrumentation packages:
Copy
```
opentelemetry-bootstrap --action=install
```
Dangling OpenAI instrumentation package
`opentelemetry-bootstrap` currently installs a deprecated `opentelemetry-instrumentation-openai-v2` package by default.
However, because our `requirements.txt` explicitly installs the modern `opentelemetry-instrumentation-genai-*` libraries, OpenTelemetry resolves to the newer packages at runtime.
You can safely leave the v2 package alone or uninstall it.
Start the application using `opentelemetry-instrument` to automatically instrument our application, including the AI agent workflow:
Copy
```
OPENAI_API_KEY="<your-openai-api-key>" \
OTEL_EXPORTER_OTLP_ENDPOINT="https://ingest.<your-region>.signoz.cloud:443" \
OTEL_EXPORTER_OTLP_HEADERS="signoz-ingestion-key=<your-ingestion-key>" \
OTEL_SERVICE_NAME="otel-ai-agent-observability-demo" \
OTEL_RESOURCE_ATTRIBUTES="service.version=1.0.0,deployment.environment=dev" \
OTEL_EXPORTER_OTLP_PROTOCOL="http/protobuf" \
OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=span_only \
opentelemetry-instrument fastapi run --port 8085 --workers 1
```
Here is what these [OpenTelemetry environment variables](https://signoz.io/blog/otel-environment-variables/) configure:
- `OTEL_SERVICE_NAME="otel-ai-agent-observability-demo"`: Uniquely identifies the application in SigNoz Cloud.
- `OTEL_RESOURCE_ATTRIBUTES`: Attaches metadata (version `1.0.0`, environment `dev`) to all exported telemetry. This is used to organize and filter telemetry in the OpenTelemetry backend.
- `OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=span_only`: Captures model prompts, completions, and tool definitions as span attributes on LLM model spans.
Correlating logs to traces
Application logs are automatically correlated with traces by the `opentelemetry-bootstrap`-installed logging instrumentation. This eliminates the need to set the `OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED` environment variable, which has been deprecated in the more recent OpenTelemetry releases.
The FastAPI server starts on port 8085. Calling `/` returns a quick health check response:
Copy
```
(.venv) ❯ curl "http://127.0.0.1:8085/"
{"message":"OpenTelemetry NBA agent demo is running"}
```
The demo is now running. Before exploring the agent code, let’s look at the OpenTelemetry conventions and instrumentation packages used to capture AI agent workflows.
### Understanding OpenTelemetry’s evolving GenAI conventions
While early instrumentation discussions focused solely on "LLM tracing", OpenTelemetry standardizes AI telemetry under the broader umbrella of `Generative AI` (GenAI), using [semantic conventions prefixed with `gen_ai.*`](https://github.com/open-telemetry/semantic-conventions-genai/blob/4f85037ef86e92c510d2ef881a58f1076f6fc0e4/docs/gen-ai/README.md).
Because GenAI patterns and agentic harnesses evolve far more rapidly than traditional HTTP or database libraries, OpenTelemetry created a dedicated repository, [`open-telemetry/opentelemetry-python-genai`](https://github.com/open-telemetry/opentelemetry-python-genai), to enable maintainers to independently develop and publish these libraries.
This migration introduced a cleaner, **split-scope architecture**:
1. **`opentelemetry.instrumentation.genai.openai_agents`**: Instruments the agent orchestration harness, emitting spans for workflow execution (`invoke_workflow`) and agent routing (`invoke_agent`).
2. **`opentelemetry.instrumentation.genai.openai`**: A companion package that instruments the underlying model calls (`chat`), capturing token usage (`gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`), model parameters, and prompt context.
The [earlier Agents instrumentation](https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation-genai/opentelemetry-instrumentation-openai-agents-v2) combined workflow and model-call tracing in a single package.
Additionally, because `opentelemetry-bootstrap` has not yet added these dedicated GenAI packages to its automated discovery list, you must install and track them separately through your `requirements.txt` or `pyproject.toml` files.
Developers must install the latest GenAI instrumentation libraries manually as they aren't yet registered with opentelemetry-bootstrap.
*Developers must install the latest GenAI instrumentation libraries manually as they aren't yet registered with opentelemetry-bootstrap.*
Varying maturity across GenAI instrumentation packages
Coverage varies across GenAI instrumentation packages, especially for agent features such as handoffs and guardrails. It's recommended that you pin your instrumentation packages explicitly and check their changelogs when upgrading dependencies.
### How the NBA agent uses tools, guardrails, and sessions
Now that we've seen how OpenTelemetry separates agent orchestration from model calls, let's look at the code behind the NBA agent's workflow. [Skip to the next section](https://signoz.io/blog/opentelemetry-ai-agent-observability/#visualizing-ai-agent-workflows-with-opentelemetry) if you want to jump directly to generating and inspecting traces.
FastAPI serves our HTTP endpoints, the [OpenAI Agents SDK](https://openai.github.i