langgraph-tracing 0.2.1

Lightweight LLM tracing and observability for LangGraph applications
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
use crate::event_bus::EventBus;
use crate::store::TracingStore;
use crate::types::*;
use async_trait::async_trait;
use langgraph_checkpoint::config::RunnableConfig;
use langgraph_prebuilt::traits::{BaseChatModel, BaseTool, ToolDef};
use langgraph_prebuilt::types::Message;
use serde_json::Value as JsonValue;
use std::sync::Arc;
use std::time::Instant;
use uuid::Uuid;

/// Wrapper around any BaseChatModel that records LLM call traces.
pub struct TracingChatModel<M: BaseChatModel> {
    inner: M,
    store: Arc<dyn TracingStore>,
    event_bus: EventBus,
    trace_id: String,
    parent_span_id: Option<String>,
}

impl<M: BaseChatModel> TracingChatModel<M> {
    pub fn new(
        inner: M,
        store: Arc<dyn TracingStore>,
        event_bus: EventBus,
        trace_id: String,
    ) -> Self {
        Self {
            inner,
            store,
            event_bus,
            trace_id,
            parent_span_id: None,
        }
    }

    pub fn with_parent_span(mut self, span_id: String) -> Self {
        self.parent_span_id = Some(span_id);
        self
    }
}

fn record_llm_span(
    store: &dyn TracingStore,
    event_bus: &EventBus,
    trace_id: &str,
    parent_span_id: &Option<String>,
    model_name: &str,
    input_json: JsonValue,
    result: &Result<Message, langgraph_prebuilt::traits::ModelError>,
) {
    let span_id = Uuid::new_v4().to_string();
    match result {
        Ok(response) => {
            let output_json = serde_json::to_value(response).unwrap_or(JsonValue::Null);
            let usage = response.usage();
            let mut span = Span::new(
                span_id,
                trace_id.to_string(),
                parent_span_id.clone(),
                model_name.to_string(),
                SpanType::LlmGeneration,
                input_json,
            );
            span.finish(output_json, SpanStatus::Success);
            span.metadata.model = Some(model_name.to_string());
            span.metadata.tokens_in = usage.map(|u| u.prompt_tokens);
            span.metadata.tokens_out = usage.map(|u| u.completion_tokens);
            span.metadata.total_tokens = usage.map(|u| u.total_tokens);
            store.add_span(span.clone());
            event_bus.publish(crate::event_bus::TracingEvent::SpanCreated { span });
        }
        Err(e) => {
            let mut span = Span::new(
                span_id,
                trace_id.to_string(),
                parent_span_id.clone(),
                model_name.to_string(),
                SpanType::LlmGeneration,
                input_json,
            );
            span.finish(
                serde_json::json!({"error": e.to_string()}),
                SpanStatus::Error,
            );
            span.metadata.model = Some(model_name.to_string());
            store.add_span(span.clone());
            event_bus.publish(crate::event_bus::TracingEvent::SpanCreated { span });
        }
    }
}

#[async_trait]
impl<M: BaseChatModel + 'static> BaseChatModel for TracingChatModel<M> {
    fn name(&self) -> &str {
        self.inner.name()
    }

    fn invoke(
        &self,
        messages: &[Message],
        config: &RunnableConfig,
    ) -> Result<Message, langgraph_prebuilt::traits::ModelError> {
        let start = Instant::now();
        let result = self.inner.invoke(messages, config);
        let input_json = serde_json::to_value(messages).unwrap_or(JsonValue::Null);
        record_llm_span(self.store.as_ref(), &self.event_bus, &self.trace_id, &self.parent_span_id, self.inner.name(), input_json, &result);
        let _ = start;
        result
    }

    async fn ainvoke(
        &self,
        messages: &[Message],
        config: &RunnableConfig,
    ) -> Result<Message, langgraph_prebuilt::traits::ModelError> {
        let start = Instant::now();
        let result = self.inner.ainvoke(messages, config).await;
        let input_json = serde_json::to_value(messages).unwrap_or(JsonValue::Null);
        record_llm_span(self.store.as_ref(), &self.event_bus, &self.trace_id, &self.parent_span_id, self.inner.name(), input_json, &result);
        let _ = start;
        result
    }

    fn astream<'a>(
        &'a self,
        messages: &'a [Message],
        config: &'a RunnableConfig,
    ) -> langgraph_prebuilt::MessageStream<'a> {
        let store = self.store.clone();
        let event_bus = self.event_bus.clone();
        let trace_id = self.trace_id.clone();
        let parent_span_id = self.parent_span_id.clone();
        let model_name = self.inner.name().to_string();
        let input_json = serde_json::to_value(messages).unwrap_or(JsonValue::Null);

        let mut stream = self.inner.astream(messages, config);

        Box::pin(async_stream::stream! {
            let mut accumulated_message: Option<Message> = None;
            
            while let Some(result) = tokio_stream::StreamExt::next(&mut stream).await {
                if let Ok(ref msg) = result {
                    match accumulated_message {
                        None => {
                            accumulated_message = Some(msg.clone());
                        }
                        Some(langgraph_prebuilt::types::Message::Ai { 
                            content: langgraph_prebuilt::types::MessageContent::Text(ref mut acc_text),
                            ref mut tool_calls,
                            ref mut usage,
                            ..
                        }) => {
                            if let langgraph_prebuilt::types::Message::Ai { 
                                content: langgraph_prebuilt::types::MessageContent::Text(ref msg_text),
                                tool_calls: ref msg_tools,
                                usage: ref msg_usage,
                                ..
                            } = msg {
                                acc_text.push_str(msg_text);
                                for tc in msg_tools {
                                    if !tool_calls.iter().any(|existing| existing.id == tc.id && tc.id.is_some()) {
                                        tool_calls.push(tc.clone());
                                    }
                                }
                                if msg_usage.is_some() {
                                    *usage = msg_usage.clone();
                                }
                            }
                        }
                        _ => {
                            // For other message types, just replace (though astream usually only yields AI messages)
                            accumulated_message = Some(msg.clone());
                        }
                    }
                }
                yield result;
            }

            // Record span when stream ends
            if let Some(final_msg) = accumulated_message {
                record_llm_span(
                    store.as_ref(),
                    &event_bus,
                    &trace_id,
                    &parent_span_id,
                    &model_name,
                    input_json,
                    &Ok(final_msg),
                );
            }
        })
    }

    fn bind_tools(&self, tools: Vec<ToolDef>) -> Box<dyn BaseChatModel> {
        // We can't wrap Box<dyn BaseChatModel> in TracingChatModel because
        // Box<dyn BaseChatModel> doesn't implement BaseChatModel.
        // Instead, bind tools on the inner model and wrap the result.
        // We need to create a dynamic wrapper.
        let inner = self.inner.bind_tools(tools);
        Box::new(DynamicTracingChatModel {
            inner,
            store: self.store.clone(),
            event_bus: self.event_bus.clone(),
            trace_id: self.trace_id.clone(),
            parent_span_id: self.parent_span_id.clone(),
        })
    }
}

/// Dynamic wrapper that holds a Box<dyn BaseChatModel> instead of a generic type.
/// This is needed for bind_tools which returns Box<dyn BaseChatModel>.
struct DynamicTracingChatModel {
    inner: Box<dyn BaseChatModel>,
    store: Arc<dyn TracingStore>,
    event_bus: EventBus,
    trace_id: String,
    parent_span_id: Option<String>,
}

#[async_trait]
impl BaseChatModel for DynamicTracingChatModel {
    fn name(&self) -> &str {
        self.inner.name()
    }

    fn invoke(
        &self,
        messages: &[Message],
        config: &RunnableConfig,
    ) -> Result<Message, langgraph_prebuilt::traits::ModelError> {
        let start = Instant::now();
        let result = self.inner.invoke(messages, config);
        let input_json = serde_json::to_value(messages).unwrap_or(JsonValue::Null);
        record_llm_span(self.store.as_ref(), &self.event_bus, &self.trace_id, &self.parent_span_id, self.inner.name(), input_json, &result);
        let _ = start;
        result
    }

    async fn ainvoke(
        &self,
        messages: &[Message],
        config: &RunnableConfig,
    ) -> Result<Message, langgraph_prebuilt::traits::ModelError> {
        let start = Instant::now();
        let result = self.inner.ainvoke(messages, config).await;
        let input_json = serde_json::to_value(messages).unwrap_or(JsonValue::Null);
        record_llm_span(self.store.as_ref(), &self.event_bus, &self.trace_id, &self.parent_span_id, self.inner.name(), input_json, &result);
        let _ = start;
        result
    }

    fn astream<'a>(
        &'a self,
        messages: &'a [Message],
        config: &'a RunnableConfig,
    ) -> langgraph_prebuilt::MessageStream<'a> {
        let store = self.store.clone();
        let event_bus = self.event_bus.clone();
        let trace_id = self.trace_id.clone();
        let parent_span_id = self.parent_span_id.clone();
        let model_name = self.inner.name().to_string();
        let input_json = serde_json::to_value(messages).unwrap_or(JsonValue::Null);

        let mut stream = self.inner.astream(messages, config);

        Box::pin(async_stream::stream! {
            let mut accumulated_message: Option<Message> = None;
            
            while let Some(result) = tokio_stream::StreamExt::next(&mut stream).await {
                if let Ok(ref msg) = result {
                    match accumulated_message {
                        None => {
                            accumulated_message = Some(msg.clone());
                        }
                        Some(langgraph_prebuilt::types::Message::Ai { 
                            content: langgraph_prebuilt::types::MessageContent::Text(ref mut acc_text),
                            ref mut tool_calls,
                            ref mut usage,
                            ..
                        }) => {
                            if let langgraph_prebuilt::types::Message::Ai { 
                                content: langgraph_prebuilt::types::MessageContent::Text(ref msg_text),
                                tool_calls: ref msg_tools,
                                usage: ref msg_usage,
                                ..
                            } = msg {
                                acc_text.push_str(msg_text);
                                for tc in msg_tools {
                                    if !tool_calls.iter().any(|existing| existing.id == tc.id && tc.id.is_some()) {
                                        tool_calls.push(tc.clone());
                                    }
                                }
                                if msg_usage.is_some() {
                                    *usage = msg_usage.clone();
                                }
                            }
                        }
                        _ => {
                            accumulated_message = Some(msg.clone());
                        }
                    }
                }
                yield result;
            }

            // Record span when stream ends
            if let Some(final_msg) = accumulated_message {
                record_llm_span(
                    store.as_ref(),
                    &event_bus,
                    &trace_id,
                    &parent_span_id,
                    &model_name,
                    input_json,
                    &Ok(final_msg),
                );
            }
        })
    }

    fn bind_tools(&self, tools: Vec<ToolDef>) -> Box<dyn BaseChatModel> {
        let inner = self.inner.bind_tools(tools);
        Box::new(DynamicTracingChatModel {
            inner,
            store: self.store.clone(),
            event_bus: self.event_bus.clone(),
            trace_id: self.trace_id.clone(),
            parent_span_id: self.parent_span_id.clone(),
        })
    }
}

/// Wrapper around any BaseTool that records tool call traces.
pub struct TracingTool<T: BaseTool> {
    inner: T,
    store: Arc<dyn TracingStore>,
    event_bus: EventBus,
    trace_id: String,
    parent_span_id: Option<String>,
}

impl<T: BaseTool> TracingTool<T> {
    pub fn new(
        inner: T,
        store: Arc<dyn TracingStore>,
        event_bus: EventBus,
        trace_id: String,
    ) -> Self {
        Self {
            inner,
            store,
            event_bus,
            trace_id,
            parent_span_id: None,
        }
    }

    pub fn with_parent_span(mut self, span_id: String) -> Self {
        self.parent_span_id = Some(span_id);
        self
    }
}

fn record_tool_span(
    store: &dyn TracingStore,
    event_bus: &EventBus,
    trace_id: &str,
    parent_span_id: &Option<String>,
    tool_name: &str,
    input: &JsonValue,
    result: &Result<JsonValue, langgraph_prebuilt::traits::ToolError>,
) {
    let span_id = Uuid::new_v4().to_string();
    let mut span = Span::new(
        span_id,
        trace_id.to_string(),
        parent_span_id.clone(),
        tool_name.to_string(),
        SpanType::ToolCall,
        input.clone(),
    );
    span.metadata.tool_name = Some(tool_name.to_string());

    match result {
        Ok(output) => {
            span.finish(output.clone(), SpanStatus::Success);
        }
        Err(e) => {
            span.finish(
                serde_json::json!({"error": e.to_string()}),
                SpanStatus::Error,
            );
        }
    }

    store.add_span(span.clone());
    event_bus.publish(crate::event_bus::TracingEvent::SpanCreated { span });
}

#[async_trait]
impl<T: BaseTool + 'static> BaseTool for TracingTool<T> {
    fn name(&self) -> &str {
        self.inner.name()
    }

    fn description(&self) -> &str {
        self.inner.description()
    }

    fn parameters(&self) -> Option<&JsonValue> {
        self.inner.parameters()
    }

    fn invoke(&self, args: &JsonValue, config: &RunnableConfig) -> Result<JsonValue, langgraph_prebuilt::traits::ToolError> {
        let start = Instant::now();
        let result = self.inner.invoke(args, config);
        record_tool_span(self.store.as_ref(), &self.event_bus, &self.trace_id, &self.parent_span_id, self.inner.name(), args, &result);
        let _ = start;
        result
    }

    async fn ainvoke(&self, args: &JsonValue, config: &RunnableConfig) -> Result<JsonValue, langgraph_prebuilt::traits::ToolError> {
        let start = Instant::now();
        let result = self.inner.ainvoke(args, config).await;
        record_tool_span(self.store.as_ref(), &self.event_bus, &self.trace_id, &self.parent_span_id, self.inner.name(), args, &result);
        let _ = start;
        result
    }

    fn to_tool_def(&self) -> ToolDef {
        self.inner.to_tool_def()
    }
}