AI Space

AI Models & Assistants / Knowledge library

Agent Tracing

Records runs as linked spans for model calls, tools, handoffs, guardrails, and custom events

Agent observability technique

Why learn Agent Tracing?

It makes multi-step behavior debuggable, measurable, and auditable across development and production.

The problem it solves

It reveals where an agent spent time, failed, changed control, or used a tool incorrectly.

Where it came from

Agent traces capture the ordered structure and timing of a workflow rather than only its final answer. Trace design must also control sensitive inputs, outputs, credentials, and retention.

Build your understanding

Work through these exercises in order. Try the task before opening a reference.

  1. Define Agent Tracing and identify its place in an agent architecture.
  2. Choose a task where Agent Tracing improves an agent system.
  3. Diagnose one failure caused by misusing Agent Tracing.
  4. Interpret a trace or state transition involving Agent Tracing.
  5. Explain one operational tradeoff of Agent Tracing.

Documentation and sources

Connect the ideas

Explore next

Agent RunOpenTelemetry (opentelemetry)Agent Evaluation LoopObservability

Real World Example

Illustrative business scenario · AI-generated from linked documentation · Not a measured customer case study

Supply Chain Logistics Agent Observability

Logistics companies managing automated warehouse inventory and shipping coordination.

The business problem

Complex, multi-step agent workflows involving tool calls to inventory databases and external shipping APIs often fail silently, making it impossible to determine if a breakdown occurred during a database query, a guardrail check, or a model generation.

How this topic solves it

Implementing Agent Tracing allows developers to capture a hierarchical record of every event—including tool calls, guardrails, and model turns—linked together in a single trace, providing a clear audit trail for debugging asynchronous failures.

  1. Initialize the agent workflow within a trace context manager to group multiple runner calls into a single logical unit.
  2. Configure RunConfig to include or exclude specific span types like task or turn spans to balance detail with performance.
  3. Use custom_span() to wrap proprietary business logic or external API interactions that fall outside standard SDK instrumentation.
  4. Call flush_traces() after critical background tasks or long-running worker processes to ensure all buffered spans are exported to the dashboard.
  5. Set trace_include_sensitive_data to False in production environments to redact PII from stored span data.
Agent Trace Hierarchy · Process flow

Shows how a parent Trace contains nested Spans representing different stages of an agent execution.

Trace

Root container for the entire workflow

Agent Span

Represents the overall agent execution

Tool Span

Captures inventory database query

Guardrail Span

Validates shipping constraints

  • Trace → contains → Agent Span
  • Agent Span → invokes → Tool Span
  • Agent Span → triggers → Guardrail Span

Implementation example

This code demonstrates wrapping a multi-step agent process in a trace and ensuring immediate export using flush_traces, which is critical for background logistics tasks.

python · Illustrative code; not executed or production-validated.

from agents import Runner, trace, flush_traces

# Wrap the entire logistics workflow in a single trace
with trace("Logistics_Workflow"):
    try:
        # Execute agent run; spans are automatically linked
        result = Runner.run_sync(agent, "Ship order #123")
    finally:
        # Ensure traces are sent immediately after the task completes
        flush_traces()

Expected business outcome

Developers gain the ability to visualize the entire lifecycle of a logistics request, enabling rapid identification of which specific tool or guardrail caused a workflow interruption.

Limitations and checks

The default batch processing of traces may introduce a delay in visibility for long-running background workers; developers must manually invoke flush_traces() to force immediate export if real-time monitoring is required.

Sources

Generated 2026-10-18 with gemini-3.1-flash-lite. Your assessment includes two questions about this example.

Catalog source: technology-maxxing. This page reproduces the existing catalog’s learning material and source links.