Introduction
Observability is critical for agentic AI systems - without it, you're flying blind. This guide compares different observability architectures and patterns to help you build systems you can understand, debug, and optimize.
The Three Pillars of Observability
1. Logs (What Happened)
Structured or unstructured records of events. For agentic AI, logs capture agent decisions, tool calls, LLM prompts/responses, and errors.
What to Log:
- Agent reasoning steps
- LLM prompts and responses (sanitized of PII)
- Tool calls with parameters and results
- Errors and exceptions
- User inputs and final outputs
2. Metrics (How Much)
Numerical measurements over time. Track performance, cost, quality, and usage patterns.
Critical Metrics:
- Latency (P50, P95, P99)
- Throughput (requests/second)
- Error rate (%)
- LLM token usage (cost)
- Agent success rate (%)
- Tool execution time
3. Traces (How It Flowed)
End-to-end journey of a request through your system. Essential for understanding complex multi-step agent workflows.
What to Trace:
- User request → Agent → LLM → Tools → Response
- Time spent in each component
- Data passed between steps
- Error propagation
Observability Architecture Patterns
Pattern 1: Centralized Logging
All logs flow to a central log aggregation system (Elasticsearch, Splunk, CloudWatch Logs).
Architecture:
- Agents emit structured JSON logs
- Log shipper (Fluentd, Filebeat) sends to central system
- Central system indexes and stores logs
- Query logs via web UI or API
Advantages:
- Single source of truth for all logs
- Full-text search across all logs
- Historical analysis
- Compliance and audit trails
Disadvantages:
- Expensive at scale (GB/day)
- Search can be slow
- Requires careful index management
Pattern 2: Distributed Tracing
Track requests as they flow through distributed system. Each component adds trace data.
Architecture (OpenTelemetry):
- Instrument code with OpenTelemetry SDK
- Generate trace ID and span IDs
- Each component adds span with timing/metadata
- Export to tracing backend (Jaeger, Tempo, Honeycomb)
Advantages:
- Visualize entire request flow
- Identify bottlenecks instantly
- Understand dependencies
- Debug complex multi-step workflows
Disadvantages:
- Requires code instrumentation
- Performance overhead (sampling helps)
- Learning curve for teams
Pattern 3: Metrics-Based Monitoring
Aggregate numerical metrics and alert on anomalies. Focus on system health, not individual requests.
Architecture (Prometheus):
- Agents expose metrics endpoint (/metrics)
- Prometheus scrapes metrics every 15s
- Store time-series data
- Grafana for visualization
- Alertmanager for alerts
Advantages:
- Low overhead
- Excellent for alerting
- Great for capacity planning
- Affordable at any scale
Disadvantages:
- No request-level detail
- Can't debug individual failures
- Metrics explosion with high cardinality
Agentic AI-Specific Observability Challenges
Challenge 1: Non-Deterministic Behavior
LLMs are non-deterministic - same prompt may yield different responses. Traditional debugging techniques don't work.
Solution:
- Log full LLM prompts and responses
- Track temperature and sampling parameters
- Store model version and configuration
- A/B test different prompt templates
Challenge 2: Long Execution Chains
Agents may take 10+ steps to complete a task. Hard to understand where things went wrong.
Solution:
- Hierarchical tracing - span per reasoning step
- Agent decision log (why it chose each action)
- Replay functionality to reproduce behavior
Challenge 3: Cost Visibility
LLM API costs can spiral quickly. Need fine-grained cost tracking per user, per feature.
Solution:
- Track tokens used per request
- Tag requests with user/tenant/feature
- Real-time cost dashboards
- Budget alerts and rate limiting
Recommended Architecture (Hybrid Approach)
Components
- OpenTelemetry for Tracing:
- Instrument all agent code
- Capture full request flow
- Export to Jaeger for debugging
- Prometheus for Metrics:
- System health metrics
- Alerting on anomalies
- Capacity planning
- Structured Logging to S3:
- Log full LLM interactions
- Query with Athena for analysis
- Cheap long-term storage
- LLM Observability Platform:
- LangSmith, Weights & Biases, or Arize
- Purpose-built for LLM debugging
- Prompt evaluation and testing
Data Flow
┌─────────┐
│ Agent │
└────┬────┘
│
├──► OpenTelemetry ──► Jaeger (Distributed Tracing)
│
├──► Prometheus ──► Grafana (Metrics & Dashboards)
│
├──► JSON Logs ──► S3 ──► Athena (Historical Analysis)
│
└──► LangSmith (LLM Debugging)Implementation Example
Instrumented Agent Code
import { trace, context } from '@opentelemetry/api';
import prometheus from 'prom-client';
const tracer = trace.getTracer('ai-agent');
const requestDuration = new prometheus.Histogram({
name: 'agent_request_duration_seconds',
help: 'Agent request duration',
labelNames: ['outcome']
});
const tokenCounter = new prometheus.Counter({
name: 'llm_tokens_used_total',
help: 'Total LLM tokens consumed',
labelNames: ['model']
});
async function processRequest(userPrompt) {
const span = tracer.startSpan('agent.process');
const endTimer = requestDuration.startTimer();
try {
// Log initial request
logger.info('Agent request started', {
traceId: span.spanContext().traceId,
prompt: sanitize(userPrompt)
});
// Reasoning step
const planSpan = tracer.startSpan('agent.plan', { parent: span });
const plan = await agent.createPlan(userPrompt);
planSpan.end();
logger.info('Agent plan created', { plan, traceId: span.spanContext().traceId });
// Execute plan
const results = [];
for (const step of plan.steps) {
const stepSpan = tracer.startSpan(`agent.step.${step.type}`, { parent: span });
const result = await executeStep(step);
results.push(result);
// Track LLM token usage
if (step.type === 'llm') {
tokenCounter.inc({ model: step.model }, result.tokensUsed);
}
stepSpan.setAttribute('step.result', result.status);
stepSpan.end();
}
// Generate response
const response = await agent.generateResponse(results);
logger.info('Agent request completed', {
traceId: span.spanContext().traceId,
response: sanitize(response),
tokensUsed: results.reduce((sum, r) => sum + (r.tokensUsed || 0), 0)
});
span.setStatus({ code: 0 }); // Success
endTimer({ outcome: 'success' });
return response;
} catch (error) {
logger.error('Agent request failed', {
traceId: span.spanContext().traceId,
error: error.message,
stack: error.stack
});
span.setStatus({ code: 2, message: error.message }); // Error
endTimer({ outcome: 'error' });
throw error;
} finally {
span.end();
}
}Alerting Strategy
Critical Alerts (PagerDuty)
- Error rate > 5% for 5 minutes
- P95 latency > 10 seconds
- Zero successful requests in 10 minutes
- LLM API failures > 20% for 3 minutes
Warning Alerts (Slack)
- LLM cost > $500/hour
- Error rate > 2% for 15 minutes
- P95 latency > 5 seconds
- Memory usage > 80%
Informational (Dashboard Only)
- Daily active users
- Most common agent tasks
- Average conversation length
- Feature usage breakdown
Comparison Table
| Pattern | Best For | Cost | Complexity | Debug Power |
|---|---|---|---|---|
| Centralized Logging | Compliance, audit trails | High | Low | Medium |
| Distributed Tracing | Debugging complex flows | Medium | High | Very High |
| Metrics Monitoring | Alerting, system health | Low | Low | Low |
| LLM Observability Platform | LLM-specific debugging | Medium | Low | High (for LLMs) |
Best Practices
1. Sample Intelligently
- Trace 100% of errors
- Trace 10% of slow requests (>2s)
- Trace 1% of successful requests
- Always trace specific users for debugging
2. Add Context
- User ID, tenant ID, feature flag states
- Model versions and configurations
- Request metadata (browser, location, etc.)
- Business context (transaction amount, category)
3. Protect Privacy
- Sanitize PII before logging
- Hash user IDs
- Use separate logging tiers for sensitive data
- Implement log retention policies
Conclusion
A robust observability architecture is non-negotiable for production agentic AI systems. Combine distributed tracing for debugging, metrics for alerting, and LLM-specific tools for prompt optimization. Invest in observability early - the cost of debugging without it far exceeds the infrastructure investment.
