Agent observability

From j4flmao/agent-skills · View interactive version

Agent observability is an AI agent skill from the GitHub repository j4flmao/agent-skills. Use when the user asks about agent observability, tracing agent reasoning, debugging an agent loop, OpenTelemetry GenAI spans for LLM calls, LLM cost tracking per agent, agent audit logs or hallucination/eval monitoring in production. Produces instrumentation config, trace schema, dashboards, alerts

How to use this skill

Skill file

Agent Observability

Purpose

This skill provides the complete knowledge required to build production-grade

observability systems for AI agent deployments. Unlike traditional software where

execution paths are deterministic and debuggable with standard tools, agent systems

exhibit non-deterministic reasoning loops, branching decision trees, variable-length

tool call chains, and stochastic output variations that demand specialized observability

infrastructure.

The skill covers every dimension of agent observability: capturing and visualizing

reasoning traces that show why an agent made each decision, integrating OpenTelemetry

to instrument every LLM call and tool invocation, propagating distributed trace context

across multi-agent chains, maintaining tamper-evident decision audit logs, profiling

performance bottlenecks in agent pipelines, detecting anomalous behavior patterns that

indicate drift or failure, tracking costs across model providers and optimizing token

usage, and analyzing latency to identify and eliminate bottlenecks in the agent execution

critical path.

Core Principles

  1. Trace Everything, Sample Intelligently: Every LLM call, tool invocation, and

decision point must be instrumentable. In production, use head-based and tail-based

sampling to control volume while guaranteeing capture of errors and anomalies.

  1. Reasoning is the Primary Signal: Traditional metrics (latency, throughput, errors)

are necessary but insufficient. The agent's reasoning trace—the chain of observations,

thoughts, and actions—is the primary debugging and auditing artifact.

  1. Correlation Across Boundaries: A single user request may traverse multiple agents,

tools, sandboxes, and external APIs. Trace context must propagate across all boundaries

to enable end-to-end visibility.

  1. Cost is a First-Class Metric: Token consumption, API call counts, and dollar costs

must be tracked with the same rigor as latency and error rates. Cost anomalies often

indicate reasoning loops or prompt inefficiencies.

  1. Detect Drift Before Failure: Anomaly detection on agent behavior distributions

(response length, tool call frequency, reasoning depth) catches degradation before

it manifests as user-visible failures.

Agent Protocol

Triggers

Input Context Required

Output Artifact

Response Formats

{
  "trace_id": "4bf92f3577b34da6a3ce929d0e0e4736",
  "span_id": "00f067aa0ba902b7",
  "agent_id": "agent-planner-01",
  "request_id": "req-7f8a9b0c",
  "reasoning_trace": {
    "steps": [
      {
        "step_id": 1,
        "type": "observation",
        "content": "User requests weather forecast for Tokyo",
        "timestamp": "2026-06-04T09:00:00.123Z",
        "tokens_in": 47,
        "tokens_out": 0
      },
      {
        "step_id": 2,
        "type": "thought",
        "content": "Need to call weather API tool with location=Tokyo",
        "timestamp": "2026-06-04T09:00:00.456Z",
        "tokens_in": 0,
        "tokens_out": 32,
        "model": "claude-sonnet-4-20250514",
        "cost_usd": 0.00048
      },
      {
        "step_id": 3,
        "type": "action",
        "tool": "weather_api",
        "input": {"location": "Tokyo", "units": "metric"},
        "output": {"temp_c": 22, "condition": "partly_cloudy"},
        "latency_ms": 340,
        "timestamp": "2026-06-04T09:00:00.796Z"
      }
    ],
    "total_steps": 5,
    "total_tokens": 847,
    "total_cost_usd": 0.00127,
    "total_latency_ms": 2340
  },
  "metrics": {
    "llm_calls": 2,
    "tool_calls": 1,
    "retries": 0,
    "cache_hits": 1,
    "reasoning_depth": 5
  }
}

Decision Matrix

START: Observability requirement identified
│
├─ What is the primary goal?
│  ├─ DEBUGGING → Focus on reasoning trace capture
│  │  ├─ Single agent? → Instrument with local span collection
│  │  └─ Multi-agent? → Deploy distributed tracing with context propagation
│  │
│  ├─ COMPLIANCE → Focus on decision audit logging
│  │  ├─ Tamper-evident required? → Use append-only log with Merkle tree
│  │  └─ Standard audit? → Structured JSON logs with retention policy
│  │
│  ├─ COST CONTROL → Focus on cost tracking & optimization
│  │  ├─ Per-request attribution? → Tag spans with cost metadata
│  │  └─ Aggregate trends? → Build cost dashboards with model breakdown
│  │
│  └─ PERFORMANCE → Focus on latency analysis
│     ├─ Identify bottleneck first → Capture critical path analysis
│     ├─ LLM latency dominant? → Optimize prompts, enable caching
│     └─ Tool latency dominant? → Parallelize tool calls, add timeouts
│
├─ What sampling strategy?
│  ├─ Development → 100% sampling (capture everything)
│  ├─ Staging → Head-based 10% + tail-based on errors
│  └─ Production → Head-based 1% + tail-based on errors/anomalies
│
└─ What alerting is needed?
   ├─ Latency P99 > threshold → PagerDuty critical alert
   ├─ Error rate > 5% over 5min → Slack warning + auto-investigation
   ├─ Cost per request > 2x baseline → Cost alert + reasoning review
   └─ Anomaly score > 3σ → Anomaly alert + full trace capture

Detailed Architectural Overview

┌───────────────────────────────────────────────────────────────────┐
│                       AGENT RUNTIME                               │
│  ┌──────────────────────────────────────────────────────────────┐ │
│  │                 INSTRUMENTATION LAYER                         │ │
│  │  ┌────────────┐  ┌────────────┐  ┌────────────────────────┐  │ │
│  │  │ LLM Call   │  │ Tool Call  │  │ Reasoning Step         │  │ │
│  │  │ Interceptor│  │ Interceptor│  │ Recorder               │  │ │
│  │  └─────┬──────┘  └─────┬──────┘  └──────────┬─────────────┘  │ │
│  │        │               │                     │                │ │
│  │        ▼               ▼                     ▼                │ │
│  │  ┌──────────────────────────────────────────────────────────┐ │ │
│  │  │          OPENTELEMETRY SDK (Traces + Metrics)            │ │ │
│  │  │  ┌──────────┐  ┌───────────┐  ┌───────────────────────┐ │ │ │
│  │  │  │ Tracer   │  │ Meter     │  │ Context Propagator    │ │ │ │
│  │  │  │ Provider │  │ Provider  │  │ (W3C TraceContext)    │ │ │ │
│  │  │  └──────────┘  └───────────┘  └───────────────────────┘ │ │ │
│  │  └──────────────────────┬───────────────────────────────────┘ │ │
│  └─────────────────────────┼────────────────────────────────────┘ │
│                            │                                      │
└────────────────────────────┼──────────────────────────────────────┘
                             │
              ┌──────────────┼──────────────┐
              ▼              ▼              ▼
┌──────────────────┐ ┌─────────────┐ ┌──────────────────┐
│  TRACE BACKEND   │ │  METRICS    │ │  LOG AGGREGATOR  │
│  ┌────────────┐  │ │  BACKEND    │ │  ┌────────────┐  │
│  │ Jaeger /   │  │ │ ┌─────────┐│ │  │ Loki /     │  │
│  │ Tempo /    │  │ │ │Prometheus││ │  │ Elastic /  │  │
│  │ Honeycomb  │  │ │ │/ Mimir  ││ │  │ CloudWatch │  │
│  └────────────┘  │ │ └─────────┘│ │  └────────────┘  │
└──────────────────┘ └─────────────┘ └──────────────────┘
              │              │              │
              ▼              ▼              ▼
┌─────────────────────────────────────────────────────────┐
│                   ANALYSIS LAYER                         │
│  ┌─────────────┐  ┌──────────────┐  ┌────────────────┐ │
│  │ Reasoning   │  │ Anomaly      │  │ Cost           │ │
│  │ Trace       │  │ Detection    │  │ Attribution    │ │
│  │ Visualizer  │  │ Engine       │  │ Engine         │ │
│  └─────────────┘  └──────────────┘  └────────────────┘ │
│  ┌─────────────┐  ┌──────────────┐  ┌────────────────┐ │
│  │ Latency     │  │ Decision     │  │ Alert          │ │
│  │ Critical    │  │ Audit        │  │ Manager        │ │
│  │ Path        │  │ Explorer     │  │ (PagerDuty)    │ │
│  └─────────────┘  └──────────────┘  └────────────────┘ │
└─────────────────────────────────────────────────────────┘

Observability Data Flow

  LLM Call ──► Span Created ──► Attributes Added ──► Span Exported
      │              │                │                    │
      ▼              ▼                ▼                    ▼
  Token Count    Trace Context   Model, Tokens,       OTLP Collector
  Recorded       Propagated     Cost Metadata          │
                                                       ├──► Traces → Jaeger
                                                       ├──► Metrics → Prometheus
                                                       └──► Logs → Loki

Workflow Steps

Phase 1: Instrumentation Setup

  1. Install OpenTelemetry SDK and configure tracer/meter providers for the agent runtime.
  2. Implement LLM call interceptors that capture model, tokens, latency, and cost per call.
  3. Implement tool call interceptors that capture tool name, input/output, and latency.
  4. Configure W3C TraceContext propagation for cross-agent and cross-service boundaries.

Phase 2: Reasoning Trace Capture

  1. Define the reasoning step schema (observation, thought, action, result) with unique step IDs.
  2. Instrument the agent's reasoning loop to emit structured trace events at each step.
  3. Attach reasoning metadata (confidence scores, alternative paths considered) to trace spans.
  4. Configure trace sampling strategy appropriate to the deployment environment.

Phase 3: Metrics & Dashboards

  1. Define key metrics: LLM call latency (P50/P95/P99), tool call success rate, tokens per request, cost per request, reasoning depth distribution.
  2. Configure metric exporters to the chosen backend (Prometheus, Datadog, CloudWatch).
  3. Build dashboards showing real-time agent health, cost trends, and performance distributions.
  4. Set up SLO-based monitoring with error budget tracking and burn-rate alerts.

Phase 4: Decision Audit System

  1. Design the audit log schema with request context, agent identity, decision rationale, and outcome.
  2. Implement append-only audit log storage with cryptographic integrity verification.
  3. Build an audit log query interface for compliance investigators and debugging workflows.
  4. Configure retention policies aligned with regulatory requirements (30 days to 7 years).

Phase 5: Anomaly Detection

  1. Establish behavioral baselines for key agent metrics (response length, tool call frequency, reasoning depth, cost per request).
  2. Deploy statistical anomaly detection (z-score, IQR, isolation forest) on rolling windows.
  3. Configure anomaly-triggered actions: increase sampling rate, capture full traces, alert on-call.
  4. Build feedback loops where confirmed anomalies update detection thresholds.

Phase 6: Cost & Latency Optimization

  1. Implement per-request cost attribution by tagging each LLM span with model pricing metadata.
  2. Build cost breakdown reports by agent, model, tool, and customer/tenant.
  3. Perform critical path analysis on latency traces to identify sequential bottlenecks.
  4. Implement optimization recommendations: prompt caching, parallel tool calls, model downgrades.

Extended Troubleshooting Guide

| Symptom | Primary Cause | Mitigation Action |

|---------|---------------|-------------------|

| Traces missing for some agent steps | Async context lost in callback chains | Use OpenTelemetry context propagation utilities; ensure async hooks are instrumented |

| High cardinality metric explosion | Unique trace/request IDs used as metric labels | Use bounded label sets; move unique IDs to trace attributes, not metric labels |

| Reasoning trace shows infinite loop | Agent re-evaluating same observation without progress | Add loop detection middleware; set max_reasoning_steps per request |

| Cost tracking shows $0 for all requests | Model pricing metadata not configured | Configure per-model token pricing in the cost attribution engine |

| Latency P99 10x higher than P50 | Tail latency from cold LLM inference or rate limiting | Implement request hedging; add LLM response caching; monitor rate limit headers |

| Audit log gaps during high traffic | Log buffer overflow dropping entries | Use durable queue (Kafka) between agent and audit log; increase buffer capacity |

| Anomaly detector fires too many false positives | Baselines computed during non-representative period | Retrain baselines on 7+ days of production data; implement adaptive thresholds |

| Distributed trace context not propagating | Missing W3C TraceContext headers in HTTP/gRPC calls | Verify auto-instrumentation covers all HTTP clients; add manual propagation for custom protocols |

Complete Execution Scenario

User Request: "Summarize the Q3 financial report and compare to Q2"
│
▼
┌──────────────────────────────────────────────────────────────┐
│ 1. TRACE INITIATED                                            │
│    trace_id: 4bf92f3577b34da6                                 │
│    root_span: "user_request" (agent-planner-01)               │
│    Sampling decision: RECORD (matches 1% head-based sample)   │
└─────────────────────┬────────────────────────────────────────┘
                      ▼
┌──────────────────────────────────────────────────────────────┐
│ 2. REASONING TRACE CAPTURED                                   │
│    Step 1: [observation] Parse user intent → "compare Q3/Q2"  │
│    Step 2: [thought] Need to retrieve both reports first      │
│    Step 3: [action] tool=doc_retriever input={q: "Q3 report"} │
│    Step 4: [action] tool=doc_retriever input={q: "Q2 report"} │
│    Step 5: [thought] Both retrieved, now analyze differences  │
│    Step 6: [action] tool=llm_analyze input={docs: [q3, q2]}   │
│    Step 7: [result] Summary generated with comparison table   │
└─────────────────────┬────────────────────────────────────────┘
                      ▼
┌──────────────────────────────────────────────────────────────┐
│ 3. METRICS EMITTED                                            │
│    llm_call_count: 3 | tool_call_count: 2                     │
│    total_tokens: 12,847 | total_cost: $0.0193                 │
│    total_latency: 4,230ms | reasoning_depth: 7                │
│    Critical path: LLM analyze (2,100ms) → doc_retriever (890ms)│
└─────────────────────┬────────────────────────────────────────┘
                      ▼
┌──────────────────────────────────────────────────────────────┐
│ 4. AUDIT LOG ENTRY                                            │
│    request_id: req-7f8a9b0c | agent: agent-planner-01        │
│    decision: "Retrieved Q3+Q2 reports, performed comparative  │
│    analysis using claude-sonnet-4-20250514"                             │
│    outcome: SUCCESS | confidence: 0.92                        │
│    hash_chain: sha256(prev_entry + this_entry)                │
└─────────────────────┬────────────────────────────────────────┘
                      ▼
┌──────────────────────────────────────────────────────────────┐
│ 5. ANOMALY CHECK                                              │
│    Reasoning depth 7: within 1σ of baseline (μ=6.2, σ=2.1)  │
│    Cost $0.0193: within normal range                          │
│    Latency 4,230ms: slightly above P90 (3,800ms) → MONITOR   │
│    No anomaly alert triggered                                 │
└──────────────────────────────────────────────────────────────┘

Rules and Guidelines

  1. Never log raw prompts or completions in metrics: Prompts and completions contain

sensitive data and have unbounded cardinality. Store them in traces only after

redacting secrets and PII, behind access controls and a retention limit, never as

metric labels.

  1. Propagate trace context through every boundary: Every HTTP call, gRPC call, queue

message, and sandbox invocation must carry W3C TraceContext headers. Broken trace

context creates observability blind spots.

  1. Cost attribution must be near real-time: Runaway loops burn budget in minutes, so

compute cost at span-completion time from versioned pricing config and alert on

per-run budgets rather than waiting for the provider invoice.

  1. Audit logs are append-only and immutable: Once written, audit entries must never be

modified or deleted within the retention period. Use hash chains plus signed or

externally anchored checkpoints (or WORM storage) to detect tampering; an unkeyed hash

chain alone can be recomputed by whoever can rewrite the log.

  1. Alert on absence, not just presence: Missing traces, gaps in metric streams, and

silent agents are often more dangerous than explicit errors. Monitor heartbeats and

expected event rates.

Reference Guides

Handoff

<!-- COMPRESSION: agent-observability | reasoning-traces + otel + distributed-tracing + audit-logs + anomaly-detection + cost-tracking | v2.0.0 -->

Implementation Patterns

Agent Trace Collector

from __future__ import annotations

import secrets
from dataclasses import dataclass
from datetime import datetime, timezone
from typing import Any


def _now() -> str:
    return datetime.now(timezone.utc).isoformat()


class AgentTrace:
    """Minimal in-process span recorder. In production prefer the OpenTelemetry SDK;
    IDs here follow the W3C Trace Context sizes (16-byte trace id, 8-byte span id)."""

    def __init__(self, agent_id: str, session_id: str) -> None:
        self.trace_id = secrets.token_hex(16)  # 32 hex chars
        self.agent_id = agent_id
        self.session_id = session_id
        self.spans: dict[str, dict[str, Any]] = {}
        self.start_time = datetime.now(timezone.utc)

    def start_span(self, name: str, parent_span_id: str | None = None) -> str:
        span_id = secrets.token_hex(8)  # 16 hex chars
        self.spans[span_id] = {
            "span_id": span_id,
            "trace_id": self.trace_id,
            "parent_span_id": parent_span_id,
            "name": name,
            "start_time": _now(),
            "attributes": {},
            "events": [],
            "status": "unset",
        }
        return span_id

    def end_span(self, span_id: str, status: str = "ok", attributes: dict[str, Any] | None = None) -> None:
        span = self.spans.get(span_id)
        if span is None:
            raise KeyError(f"end_span: unknown span_id={span_id} in trace={self.trace_id}")
        span["end_time"] = _now()
        span["status"] = status
        span["attributes"].update(attributes or {})

    def add_event(self, span_id: str, name: str, attributes: dict[str, Any] | None = None) -> None:
        span = self.spans.get(span_id)
        if span is None:
            raise KeyError(f"add_event: unknown span_id={span_id} in trace={self.trace_id}")
        span["events"].append({"name": name, "timestamp": _now(), "attributes": attributes or {}})

    def to_dict(self) -> dict[str, Any]:
        elapsed = datetime.now(timezone.utc) - self.start_time
        return {
            "trace_id": self.trace_id,
            "agent_id": self.agent_id,
            "session_id": self.session_id,
            "duration_ms": elapsed.total_seconds() * 1000,
            "spans": list(self.spans.values()),
        }


@dataclass(frozen=True, slots=True)
class ModelPrice:
    input_per_mtok: float   # USD per 1M input tokens
    output_per_mtok: float  # USD per 1M output tokens


class CostTracker:
    """Prices change often: load them from config (env/JSON) that is versioned and
    reviewed, never hardcode them. Unknown models fail loudly instead of guessing."""

    def __init__(self, pricing: dict[str, ModelPrice]) -> None:
        self.pricing = pricing
        self.entries: list[dict[str, Any]] = []

    def track_call(self, model: str, input_tokens: int, output_tokens: int, agent_id: str) -> float:
        price = self.pricing.get(model)
        if price is None:
            raise KeyError(f"No price configured for model={model} (agent={agent_id}); update pricing config")
        cost = (input_tokens * price.input_per_mtok + output_tokens * price.output_per_mtok) / 1_000_000
        self.entries.append({
            "timestamp": _now(), "agent_id": agent_id, "model": model,
            "input_tokens": input_tokens, "output_tokens": output_tokens, "cost_usd": cost,
        })
        return cost

    def total(self, agent_id: str | None = None) -> float:
        return sum(e["cost_usd"] for e in self.entries if agent_id in (None, e["agent_id"]))

    def by_model(self) -> dict[str, float]:
        out: dict[str, float] = {}
        for e in self.entries:
            out[e["model"]] = out.get(e["model"], 0.0) + e["cost_usd"]
        return dict(sorted(out.items(), key=lambda kv: -kv[1]))

When exporting through OpenTelemetry, use the GenAI semantic conventions for span attributes

(gen_ai.operation.name, gen_ai.request.model, gen_ai.usage.input_tokens,

gen_ai.usage.output_tokens). They are still marked experimental, so pin the semconv

version you emit and check the spec before upgrading.

Architecture Decision Trees

Observability Signal Selection for Agents

What aspect of agent behavior to observe?
├── Reasoning path (what the agent thought)
│   └── Reasoning traces: span per reasoning step with input/output
│       ├── Debug incorrect decisions
│       ├── Validate safety constraints
│       └── Optimize prompt chains
│
├── Tool usage (what tools were called)
│   └── Tool call spans: arguments, results, duration
│       ├── Track tool success/failure rates
│       ├── Detect tool misuse patterns
│       └── Measure tool latency impact
│
├── Cost (how much each interaction costs)
│   └── Token tracking per model call
│       ├── Attribution to user/agent/session
│       ├── Budget enforcement
│       └── Cost optimization
│
└── Safety (did the agent violate policies)
    └── Audit events: policy checks, guardrail triggers, violations
        ├── Immutable audit log
        ├── Violation rate trending
        └── Root cause investigation

Anti-Patterns

| Anti-Pattern | Why It Fails | Correct Approach |

|---|---|---|

| Not tracing agent reasoning | Can't debug why agent made wrong decision | Trace every LLM call with input/output |

| Cost tracking with stale pricing | Budget reports are inaccurate | Update pricing table on model/version change |

| No audit log immutability | Tampered logs can't be used for compliance | Cryptographic hash chain on audit entries |

| Alerting on absence without baseline | Too many false positives | Establish baseline metrics, alert on deviation |

Performance Optimization

References

Shared References

Additional reference material for this skill, stored outside its own references/ folder.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.