feat(observability): propagate optional cost_usd on agent end
This commit is contained in:
parent
52a4c9d2b8
commit
8371f412f8
6 changed files with 15 additions and 1 deletions
|
|
@ -557,6 +557,7 @@ pub async fn run(
|
||||||
agent.observer.record_event(&ObserverEvent::AgentEnd {
|
agent.observer.record_event(&ObserverEvent::AgentEnd {
|
||||||
duration: start.elapsed(),
|
duration: start.elapsed(),
|
||||||
tokens_used: None,
|
tokens_used: None,
|
||||||
|
cost_usd: None,
|
||||||
});
|
});
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
||||||
|
|
@ -1048,6 +1048,7 @@ pub async fn run(
|
||||||
observer.record_event(&ObserverEvent::AgentEnd {
|
observer.record_event(&ObserverEvent::AgentEnd {
|
||||||
duration,
|
duration,
|
||||||
tokens_used: None,
|
tokens_used: None,
|
||||||
|
cost_usd: None,
|
||||||
});
|
});
|
||||||
|
|
||||||
Ok(final_output)
|
Ok(final_output)
|
||||||
|
|
|
||||||
|
|
@ -48,9 +48,10 @@ impl Observer for LogObserver {
|
||||||
ObserverEvent::AgentEnd {
|
ObserverEvent::AgentEnd {
|
||||||
duration,
|
duration,
|
||||||
tokens_used,
|
tokens_used,
|
||||||
|
cost_usd,
|
||||||
} => {
|
} => {
|
||||||
let ms = u64::try_from(duration.as_millis()).unwrap_or(u64::MAX);
|
let ms = u64::try_from(duration.as_millis()).unwrap_or(u64::MAX);
|
||||||
info!(duration_ms = ms, tokens = ?tokens_used, "agent.end");
|
info!(duration_ms = ms, tokens = ?tokens_used, cost_usd = ?cost_usd, "agent.end");
|
||||||
}
|
}
|
||||||
ObserverEvent::ToolCallStart { tool } => {
|
ObserverEvent::ToolCallStart { tool } => {
|
||||||
info!(tool = %tool, "tool.start");
|
info!(tool = %tool, "tool.start");
|
||||||
|
|
@ -133,10 +134,12 @@ mod tests {
|
||||||
obs.record_event(&ObserverEvent::AgentEnd {
|
obs.record_event(&ObserverEvent::AgentEnd {
|
||||||
duration: Duration::from_millis(500),
|
duration: Duration::from_millis(500),
|
||||||
tokens_used: Some(100),
|
tokens_used: Some(100),
|
||||||
|
cost_usd: Some(0.0015),
|
||||||
});
|
});
|
||||||
obs.record_event(&ObserverEvent::AgentEnd {
|
obs.record_event(&ObserverEvent::AgentEnd {
|
||||||
duration: Duration::ZERO,
|
duration: Duration::ZERO,
|
||||||
tokens_used: None,
|
tokens_used: None,
|
||||||
|
cost_usd: None,
|
||||||
});
|
});
|
||||||
obs.record_event(&ObserverEvent::ToolCallStart {
|
obs.record_event(&ObserverEvent::ToolCallStart {
|
||||||
tool: "shell".into(),
|
tool: "shell".into(),
|
||||||
|
|
|
||||||
|
|
@ -48,10 +48,12 @@ mod tests {
|
||||||
obs.record_event(&ObserverEvent::AgentEnd {
|
obs.record_event(&ObserverEvent::AgentEnd {
|
||||||
duration: Duration::from_millis(100),
|
duration: Duration::from_millis(100),
|
||||||
tokens_used: Some(42),
|
tokens_used: Some(42),
|
||||||
|
cost_usd: Some(0.001),
|
||||||
});
|
});
|
||||||
obs.record_event(&ObserverEvent::AgentEnd {
|
obs.record_event(&ObserverEvent::AgentEnd {
|
||||||
duration: Duration::ZERO,
|
duration: Duration::ZERO,
|
||||||
tokens_used: None,
|
tokens_used: None,
|
||||||
|
cost_usd: None,
|
||||||
});
|
});
|
||||||
obs.record_event(&ObserverEvent::ToolCallStart {
|
obs.record_event(&ObserverEvent::ToolCallStart {
|
||||||
tool: "shell".into(),
|
tool: "shell".into(),
|
||||||
|
|
|
||||||
|
|
@ -227,6 +227,7 @@ impl Observer for OtelObserver {
|
||||||
ObserverEvent::AgentEnd {
|
ObserverEvent::AgentEnd {
|
||||||
duration,
|
duration,
|
||||||
tokens_used,
|
tokens_used,
|
||||||
|
cost_usd,
|
||||||
} => {
|
} => {
|
||||||
let secs = duration.as_secs_f64();
|
let secs = duration.as_secs_f64();
|
||||||
let start_time = SystemTime::now()
|
let start_time = SystemTime::now()
|
||||||
|
|
@ -243,6 +244,9 @@ impl Observer for OtelObserver {
|
||||||
if let Some(t) = tokens_used {
|
if let Some(t) = tokens_used {
|
||||||
span.set_attribute(KeyValue::new("tokens_used", *t as i64));
|
span.set_attribute(KeyValue::new("tokens_used", *t as i64));
|
||||||
}
|
}
|
||||||
|
if let Some(c) = cost_usd {
|
||||||
|
span.set_attribute(KeyValue::new("cost_usd", *c));
|
||||||
|
}
|
||||||
span.end();
|
span.end();
|
||||||
|
|
||||||
self.agent_duration.record(secs, &[]);
|
self.agent_duration.record(secs, &[]);
|
||||||
|
|
@ -394,10 +398,12 @@ mod tests {
|
||||||
obs.record_event(&ObserverEvent::AgentEnd {
|
obs.record_event(&ObserverEvent::AgentEnd {
|
||||||
duration: Duration::from_millis(500),
|
duration: Duration::from_millis(500),
|
||||||
tokens_used: Some(100),
|
tokens_used: Some(100),
|
||||||
|
cost_usd: Some(0.0015),
|
||||||
});
|
});
|
||||||
obs.record_event(&ObserverEvent::AgentEnd {
|
obs.record_event(&ObserverEvent::AgentEnd {
|
||||||
duration: Duration::ZERO,
|
duration: Duration::ZERO,
|
||||||
tokens_used: None,
|
tokens_used: None,
|
||||||
|
cost_usd: None,
|
||||||
});
|
});
|
||||||
obs.record_event(&ObserverEvent::ToolCallStart {
|
obs.record_event(&ObserverEvent::ToolCallStart {
|
||||||
tool: "shell".into(),
|
tool: "shell".into(),
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,7 @@ pub enum ObserverEvent {
|
||||||
AgentEnd {
|
AgentEnd {
|
||||||
duration: Duration,
|
duration: Duration,
|
||||||
tokens_used: Option<u64>,
|
tokens_used: Option<u64>,
|
||||||
|
cost_usd: Option<f64>,
|
||||||
},
|
},
|
||||||
/// A tool call is about to be executed.
|
/// A tool call is about to be executed.
|
||||||
ToolCallStart {
|
ToolCallStart {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue