Commit 9a278b02b for llama.cpp
commit 9a278b02b83d476d7c9758c8c91e32392f896e66
Author: Mark Cockram <mark@cockram.me>
Date: Mon Nov 30 02:31:01 2026 +0100
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.
diff --git a/tools/server/server-task.cpp b/tools/server/server-task.cpp
index 595e57ffb..54c4ae5f6 100644
--- a/tools/server/server-task.cpp
+++ b/tools/server/server-task.cpp
@@ -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", {
diff --git a/tools/server/tests/unit/test_compat_anthropic.py b/tools/server/tests/unit/test_compat_anthropic.py
index 046c9327b..da44fa542 100644
--- a/tools/server/tests/unit/test_compat_anthropic.py
+++ b/tools/server/tests/unit/test_compat_anthropic.py
@@ -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")