Compare commits

..
1 Commits
Author SHA1 Message Date
Mark Cockram 9a278b02b8 server : include timings in Anthropic /v1/messages responses (#30244)
Add the timings object to the non-streaming response and to the final
message_delta event of the stream, as the OpenAI chat and Responses
paths already do. Clients such as llama-swap read prompt and generation
speed from it and otherwise report -1 for /v1/messages requests.

Update test_compat_anthropic.py to expect timings there; message_stop
still carries none.
2026-11-30 02:31:01 +01:00
2 changed files with 13 additions and 2 deletions
+8
View File
@@ -794,6 +794,10 @@ json server_task_result_cmpl_final::to_json_anthropic() {
}}
};
if (stats.is_set()) {
res["timings"] = stats.to_json();
}
return res;
}
@@ -975,6 +979,10 @@ json server_task_result_cmpl_final::to_json_anthropic_stream() {
}}
});
if (stats.is_set()) {
events.back().at("data")["timings"] = stats.to_json();
}
events.push_back({
{"event", "message_stop"},
{"data", {
@@ -77,8 +77,9 @@ def test_anthropic_messages_basic():
assert isinstance(res.body["usage"]["input_tokens"], int), "input_tokens should be integer"
assert isinstance(res.body["usage"]["output_tokens"], int), "output_tokens should be integer"
assert res.body["usage"]["output_tokens"] > 0, "Should have generated some tokens"
# Anthropic API should NOT include timings
assert "timings" not in res.body, "Anthropic API should not include timings field"
# llama-server extension: timings are included so clients can read generation speed
assert "timings" in res.body, "Anthropic API should include timings field"
assert res.body["timings"]["predicted_n"] > 0
def test_anthropic_messages_with_system():
@@ -206,6 +207,8 @@ def test_anthropic_messages_streaming():
assert message_delta["delta"]["stop_reason"] in ["end_turn", "max_tokens"]
assert "usage" in message_delta
assert message_delta["usage"]["output_tokens"] > 0
assert "timings" in message_delta, "message_delta should include timings"
assert message_delta["timings"]["predicted_n"] > 0
# Check message_stop
message_stop = next(e for e in events if e["type"] == "message_stop")