Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

feat(progress-tracing): record time to first token for model calls

Steven Enamakel4af551d347cd
diff --git a/crates/openhuman-core/src/agent/progress_tracing/collector/events.rs b/crates/openhuman-core/src/agent/progress_tracing/collector/events.rs
index c1088a8e26..7a14a3de4d 100644
--- a/crates/openhuman-core/src/agent/progress_tracing/collector/events.rs
+++ b/crates/openhuman-core/src/agent/progress_tracing/collector/events.rs
@@ -241,6 +241,7 @@ impl SpanCollector {
);
if let Some(state) = self.subagents.get_mut(task_id) {
state.current_iteration_span_id = Some(id);
+ state.first_deltas = Default::default();
}
}
diff --git a/crates/openhuman-core/src/agent/progress_tracing/collector/model_call.rs b/crates/openhuman-core/src/agent/progress_tracing/collector/model_call.rs
index 740a7cc4da..2efcc7973f 100644
--- a/crates/openhuman-core/src/agent/progress_tracing/collector/model_call.rs
+++ b/crates/openhuman-core/src/agent/progress_tracing/collector/model_call.rs
@@ -73,6 +73,13 @@ impl SpanCollector {
.and_then(|idx| self.spans.get(idx))
.map(|span| span.start_unix_ms)
.unwrap_or(now_unix_ms);
+ // Consume this call's first-delta stamps so the next call on the same
+ // iteration (a retry, a repair) starts clean.
+ let first_deltas = match subagent_task_id.and_then(|id| self.subagents.get_mut(id)) {
+ Some(state) => std::mem::take(&mut state.first_deltas),
+ None if subagent_task_id.is_some() => Default::default(),
+ None => std::mem::take(&mut self.first_deltas),
+ };
let labeled_model = format!("{provider_id}.{model}");
let pricing = crate::agent::cost::lookup_pricing(model);
@@ -107,6 +114,26 @@ impl SpanCollector {
);
}
attrs.insert("gen_ai.usage.cost_usd".to_string(), json_f64(cost_usd));
+ // Time to first token. Deltas only arrive on streamed calls, so a
+ // unary call carries neither attribute. Clamped to the span start: the
+ // iteration span opening is the call's approximate start.
+ if let Some(first) = first_deltas.any_unix_ms {
+ let first = first.max(start_unix_ms);
+ attrs.insert(
+ "gen_ai.response.first_token_unix_ms".to_string(),
+ json_u64(first),
+ );
+ attrs.insert(
+ "gen_ai.response.time_to_first_token_ms".to_string(),
+ json_u64(first - start_unix_ms),
+ );
+ }
+ if let Some(first_text) = first_deltas.text_unix_ms {
+ attrs.insert(
+ "gen_ai.response.time_to_first_text_ms".to_string(),
+ json_u64(first_text.max(start_unix_ms) - start_unix_ms),
+ );
+ }
// Pricing basis so Langfuse cost figures are auditable against the
// client-side estimator (USD per million tokens).
attrs.insert(
@@ -125,8 +152,15 @@ impl SpanCollector {
log::debug!(
"[agent-tracing] generation span model={labeled_model} \
iteration={iteration} child={} in={input_tokens} out={output_tokens} \
+ cached={cached_input_tokens} ttft_ms={:?} ttft_text_ms={:?} \
cost_usd={cost_usd:.6} input_captured={} output_captured={}",
subagent_task_id.is_some(),
+ first_deltas
+ .any_unix_ms
+ .map(|first| first.saturating_sub(start_unix_ms)),
+ first_deltas
+ .text_unix_ms
+ .map(|first| first.saturating_sub(start_unix_ms)),
input.is_some(),
output.is_some(),
);
diff --git a/crates/openhuman-core/src/agent/progress_tracing/otlp.rs b/crates/openhuman-core/src/agent/progress_tracing/otlp.rs
index 1bfdb9d459..d3bf808da4 100644
--- a/crates/openhuman-core/src/agent/progress_tracing/otlp.rs
+++ b/crates/openhuman-core/src/agent/progress_tracing/otlp.rs
@@ -359,6 +359,18 @@ fn span_to_otlp(span: &TraceSpan, root: &TraceSpan, environment: &str) -> Value
if let Some(usage) = usage_details(span) {
attrs.push(json_attribute("langfuse.observation.usage_details", &usage));
}
+ // Langfuse derives time to first token from the completion start.
+ if let Some(completion_start) = span
+ .attributes
+ .get("gen_ai.response.first_token_unix_ms")
+ .and_then(Value::as_u64)
+ .and_then(|ms| chrono::DateTime::from_timestamp_millis(ms as i64))
+ {
+ attrs.push(attribute(
+ "langfuse.observation.completion_start_time",
+ completion_start.to_rfc3339_opts(chrono::SecondsFormat::Millis, true),
+ ));
+ }
if let Some(cost) = span
.attributes
.get("gen_ai.usage.cost_usd")