agents-runtime 0.0.30

Async runtime orchestration for Rust deep agents.
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
//! Token tracking middleware for monitoring LLM usage and costs
//!
//! This middleware intercepts LLM requests and responses to track token usage,
//! costs, and other usage metrics across different providers.

use crate::middleware::{AgentMiddleware, MiddlewareContext};
use agents_core::events::{AgentEvent, EventMetadata, TokenUsage, TokenUsageEvent};
use agents_core::llm::{LanguageModel, LlmRequest, LlmResponse};
use agents_core::messaging::AgentMessage;
use async_trait::async_trait;
use futures::StreamExt;
use serde::{Deserialize, Serialize};
use std::sync::{Arc, RwLock};
use std::time::Instant;

/// Configuration for token tracking middleware
#[derive(Debug, Clone)]
pub struct TokenTrackingConfig {
    /// Whether to track token usage
    pub enabled: bool,
    /// Whether to emit token usage events
    pub emit_events: bool,
    /// Whether to log token usage to console
    pub log_usage: bool,
    /// Custom cost per token (overrides provider defaults)
    pub custom_costs: Option<TokenCosts>,
}

impl Default for TokenTrackingConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            emit_events: true,
            log_usage: true,
            custom_costs: None,
        }
    }
}

/// Token cost configuration for different providers
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TokenCosts {
    /// Cost per input token (in USD)
    pub input_cost_per_token: f64,
    /// Cost per output token (in USD)
    pub output_cost_per_token: f64,
    /// Provider name for reference
    pub provider: String,
    /// Model name for reference
    pub model: String,
}

impl TokenCosts {
    pub fn new(
        provider: impl Into<String>,
        model: impl Into<String>,
        input_cost: f64,
        output_cost: f64,
    ) -> Self {
        Self {
            provider: provider.into(),
            model: model.into(),
            input_cost_per_token: input_cost,
            output_cost_per_token: output_cost,
        }
    }

    /// Predefined costs for common models
    pub fn openai_gpt4o_mini() -> Self {
        Self::new("openai", "gpt-4o-mini", 0.00015 / 1000.0, 0.0006 / 1000.0)
    }

    pub fn openai_gpt4o() -> Self {
        Self::new("openai", "gpt-4o", 0.005 / 1000.0, 0.015 / 1000.0)
    }

    pub fn anthropic_claude_sonnet() -> Self {
        Self::new(
            "anthropic",
            "claude-3-5-sonnet-20241022",
            0.003 / 1000.0,
            0.015 / 1000.0,
        )
    }

    pub fn gemini_flash() -> Self {
        Self::new(
            "gemini",
            "gemini-2.0-flash-exp",
            0.000075 / 1000.0,
            0.0003 / 1000.0,
        )
    }
}

// TokenUsage is now defined in agents_core::events

// TokenUsageEvent is now defined in agents_core::events

/// Token tracking middleware that wraps an LLM to monitor usage
pub struct TokenTrackingMiddleware {
    config: TokenTrackingConfig,
    inner_model: Arc<dyn LanguageModel>,
    event_dispatcher: Option<Arc<agents_core::events::EventDispatcher>>,
    usage_stats: Arc<RwLock<Vec<TokenUsage>>>,
}

impl TokenTrackingMiddleware {
    pub fn new(
        config: TokenTrackingConfig,
        inner_model: Arc<dyn LanguageModel>,
        event_dispatcher: Option<Arc<agents_core::events::EventDispatcher>>,
    ) -> Self {
        Self {
            config,
            inner_model,
            event_dispatcher,
            usage_stats: Arc::new(RwLock::new(Vec::new())),
        }
    }

    /// Get accumulated usage statistics
    pub fn get_usage_stats(&self) -> Vec<TokenUsage> {
        self.usage_stats.read().unwrap().clone()
    }

    /// Get total usage summary
    pub fn get_total_usage(&self) -> TokenUsageSummary {
        let stats = self.get_usage_stats();
        let mut total_input = 0;
        let mut total_output = 0;
        let mut total_cost = 0.0;
        let mut total_duration = 0;

        for usage in &stats {
            total_input += usage.input_tokens;
            total_output += usage.output_tokens;
            total_cost += usage.estimated_cost;
            total_duration += usage.duration_ms;
        }

        TokenUsageSummary {
            total_input_tokens: total_input,
            total_output_tokens: total_output,
            total_tokens: total_input + total_output,
            total_cost,
            total_duration_ms: total_duration,
            request_count: stats.len(),
        }
    }

    /// Clear usage statistics
    pub fn clear_stats(&self) {
        self.usage_stats.write().unwrap().clear();
    }

    fn emit_token_event(&self, usage: TokenUsage) {
        if self.config.emit_events {
            if let Some(dispatcher) = &self.event_dispatcher {
                let event = AgentEvent::TokenUsage(TokenUsageEvent {
                    metadata: EventMetadata::new(
                        "default".to_string(),
                        uuid::Uuid::new_v4().to_string(),
                        None,
                    ),
                    usage,
                });

                let dispatcher_clone = dispatcher.clone();
                tokio::spawn(async move {
                    dispatcher_clone.dispatch(event).await;
                });
            }
        }
    }

    fn log_usage(&self, usage: &TokenUsage) {
        if self.config.log_usage {
            tracing::info!(
                provider = %usage.provider,
                model = %usage.model,
                input_tokens = usage.input_tokens,
                output_tokens = usage.output_tokens,
                total_tokens = usage.total_tokens,
                estimated_cost = usage.estimated_cost,
                duration_ms = usage.duration_ms,
                "🔢 Token usage tracked"
            );
        }
    }

    fn extract_token_usage(&self, request: &LlmRequest, response: &LlmResponse) -> TokenUsage {
        let start_time = Instant::now();

        // Estimate tokens based on text length (rough approximation)
        let input_tokens = self.estimate_tokens(&request.system_prompt)
            + request
                .messages
                .iter()
                .map(|msg| self.estimate_tokens(&self.message_to_text(msg)))
                .sum::<u32>();

        let output_tokens = self.estimate_tokens(&self.message_to_text(&response.message));
        let duration_ms = start_time.elapsed().as_millis() as u64;

        // Try to determine provider and model from the inner model
        let (provider, model) = self.detect_provider_model();

        // Calculate estimated cost
        let estimated_cost = if let Some(costs) = &self.config.custom_costs {
            (input_tokens as f64 * costs.input_cost_per_token)
                + (output_tokens as f64 * costs.output_cost_per_token)
        } else {
            0.0 // Unknown cost
        };

        TokenUsage::new(
            input_tokens,
            output_tokens,
            provider,
            model,
            duration_ms,
            estimated_cost,
        )
    }

    fn estimate_tokens(&self, text: &str) -> u32 {
        // Rough estimation: ~4 characters per token for English text
        // This is a simplified approximation - real tokenization varies by provider
        (text.len() as f32 / 4.0).ceil() as u32
    }

    fn message_to_text(&self, message: &AgentMessage) -> String {
        match &message.content {
            agents_core::messaging::MessageContent::Text(text) => text.clone(),
            agents_core::messaging::MessageContent::Json(json) => json.to_string(),
        }
    }

    fn detect_provider_model(&self) -> (String, String) {
        // Try to detect provider/model from the inner model
        // This is a simplified approach - in practice, you might want to store
        // this information in the model wrapper or use type information
        ("unknown".to_string(), "unknown".to_string())
    }
}

#[async_trait]
impl LanguageModel for TokenTrackingMiddleware {
    async fn generate(&self, request: LlmRequest) -> anyhow::Result<LlmResponse> {
        if !self.config.enabled {
            return self.inner_model.generate(request).await;
        }

        let response = self.inner_model.generate(request.clone()).await?;

        let usage = self.extract_token_usage(&request, &response);

        // Store usage statistics
        {
            let mut stats = self.usage_stats.write().unwrap();
            stats.push(usage.clone());
        }

        // Emit event and log
        self.emit_token_event(usage.clone());
        self.log_usage(&usage);

        Ok(response)
    }

    async fn generate_stream(
        &self,
        request: LlmRequest,
    ) -> anyhow::Result<agents_core::llm::ChunkStream> {
        if !self.config.enabled {
            return self.inner_model.generate_stream(request).await;
        }

        // For streaming, we'll track usage when the stream completes
        // This is a simplified implementation - in practice, you might want
        // to track partial usage as chunks arrive
        let response = self.inner_model.generate_stream(request).await?;

        // Wrap the stream to track usage when it completes
        let config = self.config.clone();
        let usage_stats = self.usage_stats.clone();
        let event_dispatcher = self.event_dispatcher.clone();

        Ok(Box::pin(futures::stream::unfold(
            (response, Instant::now()),
            move |(mut stream, start_time)| {
                let config = config.clone();
                let usage_stats = usage_stats.clone();
                let event_dispatcher = event_dispatcher.clone();
                async move {
                    match stream.next().await {
                        Some(Ok(chunk)) => {
                            match chunk {
                                agents_core::llm::StreamChunk::Done { ref message } => {
                                    // Stream completed - track usage
                                    let _response = LlmResponse {
                                        message: message.clone(),
                                    };
                                    let duration_ms = start_time.elapsed().as_millis() as u64;

                                    // Calculate estimated cost (simplified)
                                    let input_tokens = 100; // Simplified estimation
                                    let output_tokens = 50; // Simplified estimation

                                    let estimated_cost = if let Some(costs) = &config.custom_costs {
                                        (input_tokens as f64 * costs.input_cost_per_token)
                                            + (output_tokens as f64 * costs.output_cost_per_token)
                                    } else {
                                        0.0 // Unknown cost
                                    };

                                    let usage = TokenUsage::new(
                                        input_tokens,
                                        output_tokens,
                                        "unknown",
                                        "unknown",
                                        duration_ms,
                                        estimated_cost,
                                    );

                                    // Store and emit usage
                                    {
                                        let mut stats = usage_stats.write().unwrap();
                                        stats.push(usage.clone());
                                    }

                                    if config.emit_events {
                                        if let Some(dispatcher) = &event_dispatcher {
                                            let event = AgentEvent::TokenUsage(TokenUsageEvent {
                                                metadata: EventMetadata::new(
                                                    "default".to_string(),
                                                    uuid::Uuid::new_v4().to_string(),
                                                    None,
                                                ),
                                                usage,
                                            });

                                            let dispatcher_clone = dispatcher.clone();
                                            tokio::spawn(async move {
                                                dispatcher_clone.dispatch(event).await;
                                            });
                                        }
                                    }

                                    if config.log_usage {
                                        tracing::info!(
                                            provider = "unknown",
                                            model = "unknown",
                                            input_tokens = input_tokens,
                                            output_tokens = output_tokens,
                                            total_tokens = input_tokens + output_tokens,
                                            estimated_cost = estimated_cost,
                                            duration_ms = duration_ms,
                                            "🔢 Token usage tracked"
                                        );
                                    }

                                    Some((Ok(chunk), (stream, start_time)))
                                }
                                _ => Some((Ok(chunk), (stream, start_time))),
                            }
                        }
                        Some(Err(e)) => Some((Err(e), (stream, start_time))),
                        None => None,
                    }
                }
            },
        )))
    }
}

#[async_trait]
impl AgentMiddleware for TokenTrackingMiddleware {
    fn id(&self) -> &'static str {
        "token_tracking"
    }

    async fn modify_model_request(&self, _ctx: &mut MiddlewareContext<'_>) -> anyhow::Result<()> {
        // Token tracking doesn't modify requests, just monitors them
        Ok(())
    }
}

/// Summary of token usage across all requests
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TokenUsageSummary {
    pub total_input_tokens: u32,
    pub total_output_tokens: u32,
    pub total_tokens: u32,
    pub total_cost: f64,
    pub total_duration_ms: u64,
    pub request_count: usize,
}

impl TokenUsageSummary {
    pub fn average_tokens_per_request(&self) -> f64 {
        if self.request_count > 0 {
            self.total_tokens as f64 / self.request_count as f64
        } else {
            0.0
        }
    }

    pub fn average_cost_per_request(&self) -> f64 {
        if self.request_count > 0 {
            self.total_cost / self.request_count as f64
        } else {
            0.0
        }
    }
}