An AI agent can select the correct tool, execute it successfully, and still produce an incorrect final answer.
I'm exploring another important aspect of production-ready AI systems: agent observability and tracing.
1. Why Traditional Logging Is Not Enough
Traditional applications often follow predictable execution paths.
AI agents, however, can dynamically select tools, repeat operations, change execution paths, and delegate tasks to other agents.
A traditional application log might tell us:
INFO: Agent started
INFO: Tool executed successfully
INFO: Response generated
But it doesn't necessarily explain:
- Why a particular tool was selected.
- Whether the supplied arguments were correct.
- Which operation caused the delay.
- Whether an unnecessary tool was called.
- Where incorrect information entered the workflow.
This is where structured tracing becomes valuable.
2. Understanding Traces and Spans
A trace represents the execution journey of a request across the system.
A span represents an individual operation within that journey.
For example:
Agent Request (Trace)
│
├── LLM: Tool Selection
├── Tool Authorization
├── Tool Execution
│ └── Database Query
├── Response Validation
└── LLM: Final Response
Each span can capture information such as:
- Operation name
- Execution duration
- Success or failure
- Parent operation
- Error information
- Relevant execution metadata
Instead of investigating disconnected logs, engineers can inspect the relationships between operations.
3. A Practical Failure Scenario
Imagine a customer support agent receives this request:
Where is my order?
The agent selects an order lookup tool and retrieves the requested information.
However, the final response reports an incorrect delivery status.
An illustrative trace might look like this:
| Operation | Status | Duration |
|---|---|---|
| Intent identification | Success | 200 ms |
| Tool selection | Success | 350 ms |
| Authorization | Success | 20 ms |
| Order lookup | Success | 450 ms |
| Response validation | Skipped | — |
| Final generation | Success | 600 ms |
The execution technically succeeded, but response validation was skipped.
This doesn't prove that validation caused the incorrect answer. However, it identifies a missing safeguard that engineers should investigate.
Successful execution does not guarantee a correct outcome.
4. Implementing Basic Agent Tracing in Python
OpenTelemetry provides a framework-independent approach to instrumenting application operations.
Install the required packages:
pip install opentelemetry-api opentelemetry-sdk
A simplified implementation:
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import (
SimpleSpanProcessor,
ConsoleSpanExporter,
)
provider = TracerProvider()
provider.add_span_processor(
SimpleSpanProcessor(ConsoleSpanExporter())
)
trace.set_tracer_provider(provider)
tracer = trace.get_tracer(__name__)
def execute_tool(tool_name):
with tracer.start_as_current_span(
f"execute_tool {tool_name}"
) as span:
span.set_attribute(
"gen_ai.tool.name", tool_name
)
# Simulated tool response
return {"status": "shipped"}
def run_agent():
with tracer.start_as_current_span(
"agent_workflow"
):
with tracer.start_as_current_span(
"tool_selection"
):
tool_name = "get_order_status"
result = execute_tool(tool_name)
with tracer.start_as_current_span(
"response_validation"
):
if result.get("status") not in {
"pending", "shipped", "delivered"
}:
raise ValueError("Invalid status")
return result
print(run_agent())
This example creates a parent span for the agent workflow and child spans for individual operations.
The console exporter allows us to inspect the generated telemetry locally.
In production, telemetry can be exported to an observability platform for further analysis.
5. What Should We Monitor in AI Agents?
Observability should extend beyond whether the application is running.
| Metric | Purpose |
|---|---|
| End-to-end latency | Measure overall response time |
| Tool execution latency | Identify slow tools |
| Tool failure rate | Detect unreliable integrations |
| Token consumption | Understand model usage |
| Retry frequency | Identify repeated failures |
| Tool call count | Detect unnecessary operations |
| Validation failure rate | Identify invalid responses |
| Task success rate | Evaluate actual outcomes |
Task success and semantic correctness usually require additional evaluation rather than tracing alone.
6. Observability Is Not Evaluation
These concepts are closely related, but they solve different problems.
Evaluation asks:
Did the agent perform the task correctly?
Observability asks:
What happened during execution, and where should we investigate?
Tracing provides:
A structured record of the operations performed during a request.
Combining these approaches helps engineers identify failure patterns and improve agent reliability.
7. The Security Side of Observability
Tracing introduces another engineering responsibility: protecting sensitive information.
Agent traces may contain:
- User prompts
- Tool arguments
- Database responses
- Personal information
- Authentication credentials
Not everything should be recorded.
Production systems should apply data redaction, access controls, appropriate retention policies, and selective telemetry collection.
An observability system should improve visibility without becoming a new source of data exposure.
8. Available Observability Tools
Several tools support agent observability:
- OpenTelemetry: Vendor-neutral telemetry instrumentation.
- LangSmith: LLM and agent tracing, debugging, monitoring, and evaluation.
- OpenAI Agents SDK Tracing: Integrated tracing for supported agent operations.
The appropriate choice depends on the framework, infrastructure, privacy requirements, and monitoring needs of the application.
Key Takeaway
Building a functional AI agent is only the beginning.
Building a reliable AI agent requires understanding what happens between the user's request and the final response.
An agent that produces an answer is functional.
An agent whose behavior can be inspected, evaluated, and improved is much closer to being production-ready.
If we cannot observe agent behavior, debugging becomes guesswork.
Research & References
- OpenTelemetry — Generative AI Agent Spans
- OpenTelemetry — Tool Execution Semantic Conventions
- OpenAI Agents SDK — Tracing
- LangSmith — Agent and LLM Observability
Discussion: How are you currently tracing and debugging your AI agents?
Top comments (0)