[BUG] Time to first token is set as gen_ai.server.time_to_first_token (ms) instead of semconv gen_ai.response.time_to_first_chunk (s)
Human Overview
Our dashboard in Langfuse is not showing time-to-first-token statistics. This is partly because of a bug in langfuse, but while looking into this, my agent found that Strands is not setting the value correctly according to the Otel semantic conventions
Checks
- [x] I have updated to the lastest minor and patch version of Strands
- [x] I have checked the documentation and this is not expected behavior
- [x] I have searched ./issues and there are no duplicates of my issue
SDK Language
Python
Strands Version
1.64.0
Language Runtime Version
Python 3.13
Operating System
Linux (Amazon Bedrock AgentCore Runtime)
Installation Method
pip
Steps to Reproduce
- Enable tracing (e.g.
StrandsTelemetry().setup_console_exporter()) and run any agent with a streaming model (e.g.BedrockModel). - Inspect the
chatspan's attributes after a model call.
from strands import Agent
from strands.telemetry import StrandsTelemetry
StrandsTelemetry().setup_console_exporter()
agent = Agent()
agent("Hello") # console exporter prints the chat span attributes
The span has gen_ai.server.time_to_first_token set to timeToFirstByteMs (tracer.py#L277-L278). Same for gen_ai.server.request.duration (L280-281).
Expected Behavior
Per the OTel GenAI semantic conventions, an inference span for a streaming request should carry gen_ai.response.time_to_first_chunk, a double in seconds (registry.yaml, spans.yaml).
Actual Behavior
The span carries gen_ai.server.time_to_first_token as an integer in milliseconds. In the semconv this name is a server-side metric (histogram, seconds) for model servers, not a span attribute — and Strands is the client. Observability backends that map the semconv attribute (e.g. Langfuse derives time to first token from OTel attributes) don't pick it up, so time to first token shows as empty even though Strands measured it. The same applies to gen_ai.server.request.duration (ms, metric name used as a span attribute).
Possible Solution
Emit gen_ai.response.time_to_first_chunk = timeToFirstByteMs / 1000. To avoid breaking consumers of the current attribute, dual-emit the old gen_ai.server.time_to_first_token unless OTEL_SEMCONV_STABILITY_OPT_IN=gen_ai_latest_experimental is set — the pattern _add_usage_attributes already uses for the cache-token attributes.
Additional Context
This issue was drafted with AI assistance (Claude Code) and reviewed by me.
Related Issues
Langfuse: https://github.com/langfuse/langfuse/issues/20081
Proposed fix (ready to PR)
The fix is complete and tested on my fork: AnnasMazhar/harness-sdk branch `fix/6053-genai-response-time-to-first-chunk` (commit fa177742fabcbedad40640044e32b3b08b9f2c8a). I could not open the PR myself right now - my account gets a permission error when creating a pull request against this repo - so here is the full patch and the branch is ready for a maintainer to pull from, or for me to open once the permission issue clears.
Change: emit gen_ai.response.time_to_first_chunk as a double in seconds, and keep gen_ai.server.time_to_first_token dual-emitted (milliseconds) unless `OTEL_SEMCONV_STABILITY_OPT_IN=gen_ai_latest_experimental` - the same pattern already used for the cache-token attributes in `_add_optional_usage_and_metrics_attributes`.
Scope: Python only. `strands-ts` is untouched. `gen_ai.server.request.duration` is left alone - the semconv registry defines no client-side span attribute for total request duration, so there is no correct name to migrate it to.
diff --git a/strands-py/src/strands/telemetry/tracer.py b/strands-py/src/strands/telemetry/tracer.py
index ebb420376..296d42ca4 100644
--- a/strands-py/src/strands/telemetry/tracer.py
+++ b/strands-py/src/strands/telemetry/tracer.py
@@ -275,7 +275,12 @@ class Tracer:
attributes["gen_ai.usage.cache_write_input_tokens"] = usage["cacheWriteInputTokens"]
if metrics.get("timeToFirstByteMs", 0) > 0:
- attributes["gen_ai.server.time_to_first_token"] = metrics["timeToFirstByteMs"]
+ # gen_ai.response.time_to_first_chunk is the semconv name for the client-side
+ # time to first chunk, in seconds.
+ attributes["gen_ai.response.time_to_first_chunk"] = metrics["timeToFirstByteMs"] / 1000
+ # Deprecated pre-semconv name, dual-emitted unless opted into the latest conventions
+ if not self.use_latest_genai_conventions:
+ attributes["gen_ai.server.time_to_first_token"] = metrics["timeToFirstByteMs"]
if metrics.get("latencyMs", 0) > 0:
attributes["gen_ai.server.request.duration"] = metrics["latencyMs"]
diff --git a/strands-py/tests/strands/telemetry/test_tracer.py b/strands-py/tests/strands/telemetry/test_tracer.py
index 3181cf2a9..e58c748f9 100644
--- a/strands-py/tests/strands/telemetry/test_tracer.py
+++ b/strands-py/tests/strands/telemetry/test_tracer.py
@@ -400,6 +400,7 @@ def test_end_model_invoke_span(mock_span):
"gen_ai.usage.completion_tokens": 20,
"gen_ai.usage.output_tokens": 20,
"gen_ai.usage.total_tokens": 30,
+ "gen_ai.response.time_to_first_chunk": 0.01,
"gen_ai.server.time_to_first_token": 10,
"gen_ai.server.request.duration": 20,
}
@@ -431,7 +432,7 @@ def test_end_model_invoke_span_latest_conventions(mock_span, monkeypatch):
"gen_ai.usage.completion_tokens": 20,
"gen_ai.usage.output_tokens": 20,
"gen_ai.usage.total_tokens": 30,
- "gen_ai.server.time_to_first_token": 10,
+ "gen_ai.response.time_to_first_chunk": 0.01,
"gen_ai.server.request.duration": 20,
}
)
@@ -1264,6 +1265,7 @@ def test_end_model_invoke_span_with_cache_metrics(mock_span):
"gen_ai.usage.cache_creation.input_tokens": 3,
"gen_ai.usage.cache_read_input_tokens": 5,
"gen_ai.usage.cache_write_input_tokens": 3,
+ "gen_ai.response.time_to_first_chunk": 0.005,
"gen_ai.server.request.duration": 10,
"gen_ai.server.time_to_first_token": 5,
}
@@ -1304,6 +1306,7 @@ def test_end_model_invoke_span_counts_disjoint_cache_tokens(mock_span):
"gen_ai.usage.cache_creation.input_tokens": 3,
"gen_ai.usage.cache_read_input_tokens": 25,
"gen_ai.usage.cache_write_input_tokens": 3,
+ "gen_ai.response.time_to_first_chunk": 0.005,
"gen_ai.server.request.duration": 10,
"gen_ai.server.time_to_first_token": 5,
}
@@ -1427,6 +1430,55 @@ def test_end_model_invoke_span_latest_conventions_suppresses_deprecated_cache_na
assert "gen_ai.usage.cache_write_input_tokens" not in emitted
+def test_end_model_invoke_span_emits_genai_response_time_to_first_chunk(mock_span):
+ """Regression for #6053: time to first chunk is emitted under the semconv name, in seconds.
+
+ ``gen_ai.response.time_to_first_chunk`` is a double in seconds per the OTel GenAI
+ semconv; the old ``gen_ai.server.time_to_first_token`` name is a server-side metric
+ name in milliseconds and is dual-emitted for backwards compatibility.
+ """
+ tracer = Tracer()
+ message = {"role": "assistant", "content": [{"text": "Response"}]}
+ usage = Usage(inputTokens=10, outputTokens=20, totalTokens=30)
+ metrics = Metrics(latencyMs=20, timeToFirstByteMs=250)
+
+ tracer.end_model_invoke_span(mock_span, message, usage, metrics, "end_turn")
+
+ emitted = mock_span.set_attributes.call_args[0][0]
+ assert emitted["gen_ai.response.time_to_first_chunk"] == 0.25
+ # The deprecated alias keeps its millisecond value.
+ assert emitted["gen_ai.server.time_to_first_token"] == 250
+
+
+def test_end_model_invoke_span_latest_conventions_suppresses_deprecated_time_to_first_token(mock_span, monkeypatch):
+ """Opting into the latest conventions drops the pre-semconv time-to-first-token alias."""
+ monkeypatch.setenv("OTEL_SEMCONV_STABILITY_OPT_IN", "gen_ai_latest_experimental")
+ tracer = Tracer()
+ message = {"role": "assistant", "content": [{"text": "Response"}]}
+ usage = Usage(inputTokens=10, outputTokens=20, totalTokens=30)
+ metrics = Metrics(latencyMs=20, timeToFirstByteMs=250)
+
+ tracer.end_model_invoke_span(mock_span, message, usage, metrics, "end_turn")
+
+ emitted = mock_span.set_attributes.call_args[0][0]
+ assert emitted["gen_ai.response.time_to_first_chunk"] == 0.25
+ assert "gen_ai.server.time_to_first_token" not in emitted
+
+
+def test_end_model_invoke_span_without_time_to_first_byte_has_no_time_to_first_chunk(mock_span):
+ """A model call that never reported a first byte emits no time-to-first-chunk attribute."""
+ tracer = Tracer()
+ message = {"role": "assistant", "content": [{"text": "Response"}]}
+ usage = Usage(inputTokens=10, outputTokens=20, totalTokens=30)
+ metrics = Metrics(latencyMs=20, timeToFirstByteMs=0)
+
+ tracer.end_model_invoke_span(mock_span, message, usage, metrics, "end_turn")
+
+ emitted = mock_span.set_attributes.call_args[0][0]
+ assert "gen_ai.response.time_to_first_chunk" not in emitted
+ assert "gen_ai.server.time_to_first_token" not in emitted
+
+
def test_get_tracer_singleton():
"""Test that get_tracer returns a singleton instance."""
# Reset the singleton first
@@ -1975,7 +2027,7 @@ def test_end_model_invoke_span_langfuse_adds_attributes(mock_span, monkeypatch):
"gen_ai.usage.completion_tokens": 20,
"gen_ai.usage.output_tokens": 20,
"gen_ai.usage.total_tokens": 30,
- "gen_ai.server.time_to_first_token": 10,
+ "gen_ai.response.time_to_first_chunk": 0.01,
"gen_ai.server.request.duration": 20,
}
)
Tests: 3 new tests in `strands-py/tests/strands/telemetry/test_tracer.py` (semconv name in seconds, deprecated alias suppressed under the opt-in, no attribute when no first byte was measured), plus updated expectations for the 5 existing tests that assert the emitted attribute set.
$ pytest tests/strands/telemetry/test_tracer.py
129 passed
$ pytest tests/strands/telemetry/ --ignore=tests/strands/telemetry/test_config.py
169 passed
$ ruff check strands-py/src/strands/telemetry/tracer.py strands-py/tests/strands/telemetry/test_tracer.py
All checks passed!