AI & Machine Learning

Why Traditional Logs Are Not Enough

Python LLMs RAG AI Agents Authentication Classification
1,541 words Includes Code
Key Takeaway: AI agents execute tool loops, make multiple model calls, and change state across iterations. Traditional application logs that record "task completed" are insufficient. Agent observability captures traces (what happened), metrics (how much it cost and how long it took), and events (what went wrong) — giving you complete visibility into agent behavior.

You deploy an AI coding agent. It runs overnight. In the morning, you see: "Task completed successfully."

But what did it actually do? How many model calls did it make? Which tools did it use? Did any fail and recover? How much did it cost? How long did each step take?

"Task completed" tells you nothing. Agent observability tells you everything.

Why Traditional Logs Are Not Enough

Traditional Log:
2026-08-29 10:00:01 INFO Agent completed task
→ Missing: tokens, cost, tool calls, errors, latency breakdown

Agent Trace:
trace_id: a1b2c3d4
goal: "Fix authentication bug"
spans: 6
model_calls: 4 (tokens: 1630, cost: $0.0049)
tool_calls: 5 (search: 1, read: 1, edit: 2, test: 2)
errors: 0
duration: 652ms
→ Complete visibility into what happened and why

Traditional application logs record events as flat text lines. They work for request/response systems where each request is independent. But agents are stateful, iterative, and multi-step. A single task might involve 10 model calls, 5 tool executions, 3 state changes, and 2 error recoveries. A flat log cannot capture this.

AspectTraditional LoggingAgent Observability
StructureFlat text linesHierarchical traces with spans
Cause/effectNot trackedParent-child relationships
Token trackingNot trackedPer-call and aggregate
Cost trackingNot trackedPer-trace and per-session
Tool callsMaybe loggedFull args, results, latency
Error contextError message onlyFull trace of what led to error
StateNot trackedSnapshots at each step

The Agent Observability Architecture

Observability Pipeline:

Agent ──→ Trace Collector ──→ Metrics Aggregator ──→ Dashboard
  │                                                          │
  ├─ model_call ──→ tokens, latency, cost                 ├─ token_usage
  ├─ tool_call ───→ tool, args, result, latency            ├─ cost_per_task
  ├─ state_change → before, after                          ├─ error_rate
  └─ error ───────→ error_type, context                   └─ latency_p95

The 8 Components of Agent Observability

1. Traces

A trace is a complete record of one agent task — from goal to completion. It contains ordered spans, each representing one step in the agent loop.

# Trace structure trace = { "trace_id": "a1b2c3d4", "goal": "Fix authentication bug", "spans": [ {"name": "plan", "duration_ms": 320}, {"name": "tool:file_search", "duration_ms": 45}, {"name": "tool:read_file", "duration_ms": 12}, {"name": "tool:edit_file", "duration_ms": 28}, {"name": "tool:run_tests", "duration_ms": 2300}, ] }

2. Tool Calls

Each tool call is recorded with its arguments, result, latency, and success status. This is the most granular level of observability.

FieldExample
toolfile_search
args{"pattern": "*.py"}
result"Found 5 Python files"
latency_ms45
successtrue

3. Tokens

Track tokens consumed per model call and in aggregate. This directly affects cost and latency.

# Token tracking per model call model_call_1: 450 tokens (planning) model_call_2: 680 tokens (reasoning) model_call_3: 320 tokens (analysis) model_call_4: 180 tokens (completion) # Total: 1,630 tokens

4. Latency

Measure latency at multiple levels: per model call, per tool call, and per trace. Report percentiles, not just averages.

LevelWhat to MeasureTypical Range
Model callLLM inference time100-2000ms
Tool callTool execution time5-5000ms
TraceTotal task duration1-60s

5. Errors

Record every error with full context: what tool was called, what arguments were used, what the error was, and what happened next.

⚠️ Critical: An agent that silently recovers from errors is harder to debug than one that fails loudly. Always log errors, even if the agent recovers.

6. State

Snapshots of agent state at key points: what file is being edited, what the current plan is, what tests have run. This helps reconstruct the agent's reasoning.

7. Cost

Calculate cost per trace based on token usage and pricing. Aggregate across sessions for budget tracking.

# Cost calculation tokens_per_trace = 1630 price_per_1k = 0.003 cost_per_trace = (tokens / 1000) * price_per_1k # → $0.0049 # Budget alert daily_budget = 5.00 if daily_cost > daily_budget * 0.8: alert("Approaching daily budget limit")

8. Model Calls

Count and characterize each model call: what was it for (planning, reasoning, analysis), how many tokens did it consume, what was the latency.

Building an Observability Collector

The Python demo implements a complete observability collector. Here is the core pattern:

class ObservabilityCollector: def start_trace(self, goal): # Create trace with unique ID trace = AgentTrace(trace_id=uuid4(), goal=goal) self.traces.append(trace) return trace def record_model_call(self, tokens, latency_ms): # Track tokens, cost, and count self.current_trace.model_calls += 1 self.current_trace.total_tokens += tokens self.current_trace.total_cost_usd += (tokens / 1000) * self.cost_per_1k def record_tool_call(self, tool, args, result, latency_ms, success): # Record tool execution with full context span = TraceSpan(name=f"tool:{tool}", ...) self.current_trace.tool_calls += 1 if not success: self.current_trace.errors += 1 def get_all_metrics(self): # Aggregate across all traces return { "total_traces": len(self.traces), "total_tokens": sum(t.total_tokens for t in self.traces), "total_cost_usd": sum(t.total_cost_usd for t in self.traces), "error_rate": total_errors / total_tool_calls, }
💡 Try it yourself: Save the demo as demo.py and run python demo.py to see the full observability pipeline in action. Run python demo.py --test to verify all 15 test cases.

The Dashboard View

Aggregated metrics across all agent traces give you a production-level view:

Agent Observability Dashboard:

┌──────────────────┬──────────────────┬──────────────────┐
│ Token Usage │ Cost │ Tool Calls │
│ 1,630 per trace │ $0.0049 per task │ 5 per trace avg │
│ 4 model calls │ $0.0147/session │ search, read, │
│ │ │ edit, test │
├──────────────────┼──────────────────┼──────────────────┤
│ Errors │ Latency │ Success Rate │
│ 0 per trace │ 652ms avg │ 95.2% │
│ 1 recovery │ 2.3s p95 │ 4.8% retry │
└──────────────────┴──────────────────┴──────────────────┘

What to Alert On

MetricAlert ThresholdWhy
Error rate> 10% of tool callsAgent is failing repeatedly
Cost per trace> 3× averageRunaway token usage
Trace duration> 60 secondsAgent may be stuck in loop
Token usage> 10,000 per traceContext overflow risk
Model calls> 20 per traceAgent not converging
Retry count> 3 per toolPersistent failure pattern

Common Observability Mistakes

MistakeProblemFix
Logging only completionNo visibility into stepsRecord every span in the trace
Not tracking tokensCannot estimate costCount tokens per model call
Averaging latencyHides tail latencyReport p50, p95, p99
Ignoring errorsSilent failures in productionLog every error with context
No cost trackingBudget overrunsCalculate cost per trace
Flat logs onlyCannot reconstruct agent reasoningUse structured traces with spans

Practical Exercises

Exercise 1: Trace a Task

Run the demo and examine the trace for "Fix authentication bug." How many spans does it have? What is the most expensive step (by tokens)? What is the slowest step (by latency)?

Exercise 2: Design a Dashboard

If you were building a dashboard for agent observability, what 5 charts would you include? What time ranges would you use? What alerts would you set?

Exercise 3: Cost Analysis

If each agent task costs $0.005 and you run 1,000 tasks per day, what is the daily cost? Monthly? At what point would you need to optimize token usage?

Exercise 4: Error Investigation

Run the demo and examine the failed task trace. The agent encountered an error and recovered. How would you use the trace to understand what happened and whether the recovery was appropriate?

✅ Agent Observability Checklist

  • ☐ Every agent task produces a trace with unique ID
  • ☐ Every model call records tokens, latency, and cost
  • ☐ Every tool call records args, result, latency, and success
  • ☐ Errors are logged with full context (not just error message)
  • ☐ State snapshots at key decision points
  • ☐ Cost calculated per trace and per session
  • ☐ Latency reported as p50/p95/p99, not just average
  • ☐ Metrics aggregated for dashboard view
  • ☐ Alerts configured for error rate, cost, and duration
  • ☐ Traces retained for debugging (with privacy protections)

FAQ

Q: How much overhead does observability add?
A: Minimal. Recording a trace adds microseconds per step. The dominant cost is storage for trace retention, which can be managed with sampling and retention policies.

Q: Should I trace every agent task?
A: In development, yes. In production, use sampling (e.g., 10-20% of traces) for cost control, but trace 100% of tasks that encounter errors or exceed latency thresholds.

Q: What is the difference between observability and monitoring?
A: Monitoring tells you something is wrong. Observability tells you why it is wrong. Monitoring is dashboards and alerts. Observability is traces, logs, and metrics that let you reconstruct what happened.

Q: Can I use OpenTelemetry for agent traces?
A: Yes. OpenTelemetry is the standard for distributed tracing. Its span model maps naturally to agent steps. See the LLM Observability article for OpenTelemetry examples.

Q: How do I handle privacy in agent traces?
A: Traces may contain user data, code snippets, and file contents. Apply data classification, redact PII, set retention limits, and restrict access. Never log API keys or secrets.

Further Reading

Continue Learning: Explore LLM observability, understand audit trails, learn how agent loops work, and see multi-agent observability.

Discuss this topic on BestWordz Community.