ai-agents-observability 1.0.0-rc.15

Observability and tracing for AI Agents framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::{Path, PathBuf};

use crate::ObservabilityError;

/// Top-level YAML and Rust configuration for metrics, privacy, aggregation, cost, and export behavior.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ObservabilityConfig {
    /// Enables all observability collection when true.
    #[serde(default)]
    pub enabled: bool,
    /// Controls which latency events are recorded.
    #[serde(default)]
    pub latency: LatencyConfig,
    /// Controls token counting and estimation behavior.
    #[serde(default)]
    pub tokens: TokenConfig,
    /// Controls cost estimation and pricing lookup behavior.
    #[serde(default)]
    pub cost: CostConfig,
    /// Controls how the language dimension is resolved from runtime context.
    #[serde(default)]
    pub language: LanguageConfig,
    /// Controls the configured aggregate metrics table.
    #[serde(default)]
    pub aggregation: AggregationConfig,
    /// Controls raw text retention, hashing, truncation, and redaction.
    #[serde(default)]
    pub privacy: PrivacyConfig,
    /// Controls file export formats and paths.
    #[serde(default)]
    pub export: ExportConfig,
    /// Controls event queue and raw event buffer limits.
    #[serde(default)]
    pub buffer: BufferConfig,
}

impl Default for ObservabilityConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            latency: LatencyConfig::default(),
            tokens: TokenConfig::default(),
            cost: CostConfig::default(),
            language: LanguageConfig::default(),
            aggregation: AggregationConfig::default(),
            privacy: PrivacyConfig::default(),
            export: ExportConfig::default(),
            buffer: BufferConfig::default(),
        }
    }
}

impl ObservabilityConfig {
    /// Validates bounds that would otherwise make aggregation or buffering unusable.
    pub fn validate(&self) -> Result<(), ObservabilityError> {
        if self.aggregation.window_size == 0 {
            return Err(ObservabilityError::Config(
                "observability.aggregation.window_size must be greater than zero".to_string(),
            ));
        }
        if self.buffer.event_buffer == 0 {
            return Err(ObservabilityError::Config(
                "observability.buffer.event_buffer must be greater than zero".to_string(),
            ));
        }
        if self.buffer.pending_branch_event_limit == 0 {
            return Err(ObservabilityError::Config(
                "observability.buffer.pending_branch_event_limit must be greater than zero"
                    .to_string(),
            ));
        }
        for percentile in &self.aggregation.percentiles {
            if !(0.0..=1.0).contains(percentile) {
                return Err(ObservabilityError::Config(format!(
                    "observability.aggregation.percentiles value {} is outside 0.0..=1.0",
                    percentile
                )));
            }
        }
        Ok(())
    }

    /// Loads cost.pricing_file and merges it with inline pricing.
    pub fn with_pricing_file_loaded(
        mut self,
        base_dir: Option<&Path>,
    ) -> Result<Self, ObservabilityError> {
        let Some(path) = self.cost.pricing_file.clone() else {
            return Ok(self);
        };
        let resolved = resolve_pricing_path(&path, base_dir);
        let content = std::fs::read_to_string(&resolved).map_err(ObservabilityError::Io)?;
        let mut file_pricing = parse_pricing_file(&resolved, &content)?;
        let inline_pricing = std::mem::take(&mut self.cost.pricing);
        for (key, value) in inline_pricing {
            file_pricing.insert(key.to_lowercase(), value);
        }
        self.cost.pricing = file_pricing;
        Ok(self)
    }
}

/// Latency switches for categories that can produce duration events.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LatencyConfig {
    #[serde(default = "default_true")]
    pub track_llm: bool,
    #[serde(default = "default_true")]
    pub track_tools: bool,
    #[serde(default = "default_true")]
    pub track_skills: bool,
    #[serde(default = "default_true")]
    pub track_orchestration: bool,
    #[serde(default = "default_true")]
    pub track_hitl: bool,
    #[serde(default)]
    pub detailed_breakdown: bool,
}

impl Default for LatencyConfig {
    fn default() -> Self {
        Self {
            track_llm: true,
            track_tools: true,
            track_skills: true,
            track_orchestration: true,
            track_hitl: true,
            detailed_breakdown: false,
        }
    }
}

/// Token counting settings for provider usage and fallback estimation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TokenConfig {
    #[serde(default = "default_true")]
    pub count_input: bool,
    #[serde(default = "default_true")]
    pub count_output: bool,
    #[serde(default = "default_true")]
    pub estimate_when_missing: bool,
    #[serde(default)]
    pub breakdown_by_component: bool,
}

impl Default for TokenConfig {
    fn default() -> Self {
        Self {
            count_input: true,
            count_output: true,
            estimate_when_missing: true,
            breakdown_by_component: false,
        }
    }
}

/// Cost estimation settings and model pricing sources.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CostConfig {
    /// Enables cost estimation when token usage is available.
    #[serde(default = "default_true")]
    pub enabled: bool,
    /// Inline pricing keyed by model or provider/model.
    #[serde(default)]
    pub pricing: HashMap<String, ModelPricing>,
    /// Optional JSON or YAML pricing file loaded by the runtime builder or Rust helper.
    #[serde(default)]
    pub pricing_file: Option<String>,
    /// Controls how unknown model prices are represented.
    #[serde(default)]
    pub unknown_price_policy: UnknownPricePolicy,
}

impl Default for CostConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            pricing: HashMap::new(),
            pricing_file: None,
            unknown_price_policy: UnknownPricePolicy::Omit,
        }
    }
}

/// Per-thousand-token price for one model.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct ModelPricing {
    /// Price for one thousand input tokens in USD.
    pub input_per_1k: f64,
    /// Price for one thousand output tokens in USD.
    pub output_per_1k: f64,
}

/// Behavior when token usage exists but no configured price matches the model.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum UnknownPricePolicy {
    #[default]
    Omit,
    Zero,
    Error,
}

/// Language dimension lookup rules for reports and aggregations.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LanguageConfig {
    #[serde(default = "default_language_paths")]
    pub paths: Vec<String>,
    #[serde(default = "default_unknown")]
    pub fallback: String,
}

impl Default for LanguageConfig {
    fn default() -> Self {
        Self {
            paths: default_language_paths(),
            fallback: default_unknown(),
        }
    }
}

/// Grouping and rolling-window settings for aggregate metrics.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AggregationConfig {
    #[serde(default = "default_dimensions")]
    pub dimensions: Vec<AggregationDimension>,
    #[serde(default = "default_percentiles")]
    pub percentiles: Vec<f64>,
    #[serde(default = "default_window_size")]
    pub window_size: usize,
}

impl Default for AggregationConfig {
    fn default() -> Self {
        Self {
            dimensions: default_dimensions(),
            percentiles: default_percentiles(),
            window_size: default_window_size(),
        }
    }
}

/// Supported fields that can be used as aggregation dimensions.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[serde(rename_all = "snake_case")]
pub enum AggregationDimension {
    Agent,
    Actor,
    Model,
    Provider,
    Alias,
    Purpose,
    Language,
    State,
    Tool,
    Skill,
    OrchestrationPattern,
    Status,
    BranchStatus,
    RuntimeOptimization,
    CommitBehavior,
    Speculative,
    Background,
    Custom(String),
}

impl AggregationDimension {
    /// Returns the stable dimension key used in reports and CSV output.
    pub fn key(&self) -> String {
        match self {
            Self::Agent => "agent".to_string(),
            Self::Actor => "actor".to_string(),
            Self::Model => "model".to_string(),
            Self::Provider => "provider".to_string(),
            Self::Alias => "alias".to_string(),
            Self::Purpose => "purpose".to_string(),
            Self::Language => "language".to_string(),
            Self::State => "state".to_string(),
            Self::Tool => "tool".to_string(),
            Self::Skill => "skill".to_string(),
            Self::OrchestrationPattern => "orchestration_pattern".to_string(),
            Self::Status => "status".to_string(),
            Self::BranchStatus => "branch_status".to_string(),
            Self::RuntimeOptimization => "optimization".to_string(),
            Self::CommitBehavior => "commit_behavior".to_string(),
            Self::Speculative => "speculative".to_string(),
            Self::Background => "background".to_string(),
            Self::Custom(name) => format!("custom:{}", name),
        }
    }
}

/// Privacy controls for raw payload retention, hashes, truncation, and redaction.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PrivacyConfig {
    #[serde(default)]
    pub include_prompts: bool,
    #[serde(default)]
    pub include_responses: bool,
    #[serde(default)]
    pub include_tool_args: bool,
    #[serde(default)]
    pub include_tool_outputs: bool,
    #[serde(default)]
    pub max_text_chars: usize,
    #[serde(default = "default_true")]
    pub hash_inputs: bool,
    #[serde(default = "default_redact_keys")]
    pub redact_keys: Vec<String>,
    #[serde(default = "default_redact_paths")]
    pub redact_paths: Vec<String>,
}

impl Default for PrivacyConfig {
    fn default() -> Self {
        Self {
            include_prompts: false,
            include_responses: false,
            include_tool_args: false,
            include_tool_outputs: false,
            max_text_chars: 0,
            hash_inputs: true,
            redact_keys: default_redact_keys(),
            redact_paths: default_redact_paths(),
        }
    }
}

/// File export settings for reports, aggregates, raw events, and Prometheus metrics.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExportConfig {
    #[serde(default = "default_export_formats")]
    pub formats: Vec<ExportFormat>,
    #[serde(default = "default_export_path")]
    pub path: String,
    #[serde(default)]
    pub write_raw_events: bool,
    #[serde(default = "default_true")]
    pub write_report: bool,
    #[serde(default)]
    pub raw_events_format: RawEventsFormat,
}

impl Default for ExportConfig {
    fn default() -> Self {
        Self {
            formats: default_export_formats(),
            path: default_export_path(),
            write_raw_events: false,
            write_report: true,
            raw_events_format: RawEventsFormat::Jsonl,
        }
    }
}

/// Export formats supported by ObservabilityManager::export.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum ExportFormat {
    #[default]
    Json,
    Csv,
    Jsonl,
    Prometheus,
}

/// Raw event file shape used when raw event export is enabled.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum RawEventsFormat {
    #[default]
    Jsonl,
    Json,
}

/// Bounded queue and raw event retention limits.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BufferConfig {
    #[serde(default = "default_event_buffer")]
    pub event_buffer: usize,
    #[serde(default = "default_raw_event_limit")]
    pub raw_event_limit: usize,
    #[serde(default = "default_pending_branch_event_limit")]
    pub pending_branch_event_limit: usize,
    #[serde(default = "default_true")]
    pub drop_on_full: bool,
}

impl Default for BufferConfig {
    fn default() -> Self {
        Self {
            event_buffer: default_event_buffer(),
            raw_event_limit: default_raw_event_limit(),
            pending_branch_event_limit: default_pending_branch_event_limit(),
            drop_on_full: true,
        }
    }
}

pub fn default_true() -> bool {
    true
}

fn default_unknown() -> String {
    "unknown".to_string()
}

fn default_language_paths() -> Vec<String> {
    vec![
        "detected_language".to_string(),
        "input.language".to_string(),
        "user.language".to_string(),
        "context.user.language".to_string(),
    ]
}

fn default_dimensions() -> Vec<AggregationDimension> {
    vec![AggregationDimension::Model, AggregationDimension::Purpose]
}

fn default_percentiles() -> Vec<f64> {
    vec![0.5, 0.9, 0.95, 0.99]
}

fn default_window_size() -> usize {
    1000
}

fn default_redact_keys() -> Vec<String> {
    vec![
        "api_key".to_string(),
        "authorization".to_string(),
        "token".to_string(),
        "password".to_string(),
        "secret".to_string(),
    ]
}

fn default_redact_paths() -> Vec<String> {
    vec![
        "actor_facts".to_string(),
        "relationship_memory".to_string(),
        "persona.secrets".to_string(),
    ]
}

fn default_export_formats() -> Vec<ExportFormat> {
    vec![ExportFormat::Json]
}

fn default_export_path() -> String {
    "./observability_data/".to_string()
}

fn default_event_buffer() -> usize {
    4096
}

fn default_raw_event_limit() -> usize {
    10_000
}

fn default_pending_branch_event_limit() -> usize {
    1024
}

fn resolve_pricing_path(path: &str, base_dir: Option<&Path>) -> PathBuf {
    let path = PathBuf::from(path);
    if path.is_absolute() {
        path
    } else if let Some(base_dir) = base_dir {
        base_dir.join(path)
    } else {
        path
    }
}

fn parse_pricing_file(
    path: &Path,
    content: &str,
) -> Result<HashMap<String, ModelPricing>, ObservabilityError> {
    let parsed: HashMap<String, ModelPricing> = match path.extension().and_then(|ext| ext.to_str())
    {
        Some("json") => serde_json::from_str(content).map_err(ObservabilityError::Serialization)?,
        Some("yaml") | Some("yml") | None => serde_yaml::from_str(content).map_err(|error| {
            ObservabilityError::Config(format!(
                "failed to parse observability.cost.pricing_file '{}': {}",
                path.display(),
                error
            ))
        })?,
        Some(other) => {
            return Err(ObservabilityError::Config(format!(
                "unsupported observability.cost.pricing_file extension '{}': {}",
                other,
                path.display()
            )));
        }
    };
    Ok(parsed
        .into_iter()
        .map(|(key, value)| (key.to_lowercase(), value))
        .collect())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn defaults_are_privacy_safe() {
        let config = ObservabilityConfig::default();
        assert!(!config.enabled);
        assert!(!config.privacy.include_prompts);
        assert!(!config.privacy.include_responses);
        assert!(!config.privacy.include_tool_args);
        assert_eq!(config.privacy.max_text_chars, 0);
    }

    #[test]
    fn deserializes_minimal_enabled_config() {
        let yaml = r#"
observability:
  enabled: true
  aggregation:
    dimensions: [agent, model, purpose, language]
"#;
        #[derive(Deserialize)]
        struct Wrapper {
            observability: ObservabilityConfig,
        }
        let parsed: Wrapper = serde_yaml::from_str(yaml).unwrap();
        assert!(parsed.observability.enabled);
        assert_eq!(parsed.observability.aggregation.dimensions.len(), 4);
    }

    #[test]
    fn validation_rejects_bad_percentile() {
        let mut config = ObservabilityConfig::default();
        config.aggregation.percentiles = vec![1.2];
        assert!(config.validate().is_err());
    }

    #[test]
    fn pricing_file_loads_and_inline_overrides() {
        let dir = std::env::temp_dir().join(format!(
            "ai_agents_observability_pricing_{}",
            uuid::Uuid::new_v4()
        ));
        std::fs::create_dir_all(&dir).unwrap();
        std::fs::write(
            dir.join("pricing.yaml"),
            "openai/test:\n  input_per_1k: 0.1\n  output_per_1k: 0.2\nopenai/other:\n  input_per_1k: 1.0\n  output_per_1k: 2.0\n",
        )
        .unwrap();

        let mut config = ObservabilityConfig::default();
        config.cost.pricing_file = Some("pricing.yaml".to_string());
        config.cost.pricing.insert(
            "openai/test".to_string(),
            ModelPricing {
                input_per_1k: 0.3,
                output_per_1k: 0.4,
            },
        );

        let loaded = config.with_pricing_file_loaded(Some(&dir)).unwrap();
        let overridden = loaded.cost.pricing.get("openai/test").unwrap();
        assert_eq!(overridden.input_per_1k, 0.3);
        assert_eq!(overridden.output_per_1k, 0.4);
        assert!(loaded.cost.pricing.contains_key("openai/other"));

        let _ = std::fs::remove_dir_all(dir);
    }
}