1use std::cell::RefCell;
2
3#[derive(Debug, Clone)]
9pub struct LlmTraceEntry {
10 pub model: String,
11 pub input_tokens: i64,
12 pub output_tokens: i64,
13 pub duration_ms: u64,
14}
15
16thread_local! {
17 static LLM_TRACE: RefCell<Vec<LlmTraceEntry>> = const { RefCell::new(Vec::new()) };
18 static LLM_TRACING_ENABLED: RefCell<bool> = const { RefCell::new(false) };
19}
20
21pub fn enable_tracing() {
23 LLM_TRACING_ENABLED.with(|v| *v.borrow_mut() = true);
24}
25
26pub fn take_trace() -> Vec<LlmTraceEntry> {
28 LLM_TRACE.with(|v| std::mem::take(&mut *v.borrow_mut()))
29}
30
31pub fn peek_trace() -> Vec<LlmTraceEntry> {
33 LLM_TRACE.with(|v| v.borrow().clone())
34}
35
36pub fn peek_trace_summary() -> (i64, i64, i64, i64) {
38 LLM_TRACE.with(|v| {
39 let entries = v.borrow();
40 let mut input = 0i64;
41 let mut output = 0i64;
42 let mut duration = 0i64;
43 let count = entries.len() as i64;
44 for e in entries.iter() {
45 input += e.input_tokens;
46 output += e.output_tokens;
47 duration += e.duration_ms as i64;
48 }
49 (input, output, duration, count)
50 })
51}
52
53pub(crate) fn reset_trace_state() {
55 LLM_TRACE.with(|v| v.borrow_mut().clear());
56 LLM_TRACING_ENABLED.with(|v| *v.borrow_mut() = false);
57}
58
59pub(crate) fn trace_llm_call(entry: LlmTraceEntry) {
60 LLM_TRACING_ENABLED.with(|enabled| {
61 if *enabled.borrow() {
62 LLM_TRACE.with(|v| v.borrow_mut().push(entry));
63 }
64 });
65}