Compare commits

...
2 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
Evan Huus 9fcc40d3d4 common : share the kv arg parser with llama-bench (#32831)
Assisted-by: Codex
2026-11-30 01:31:06 +01:00
5 changed files with 27 additions and 38 deletions
+10 -2
View File
@@ -291,13 +291,21 @@ const std::vector<ggml_type> kv_cache_types = {
GGML_TYPE_Q5_1,
};
static ggml_type kv_cache_type_from_str(const std::string & s) {
ggml_type common_parse_kv_cache_type(const std::string & s) {
for (const auto & type : kv_cache_types) {
if (ggml_type_name(type) == s) {
return type;
}
}
throw std::runtime_error("Unsupported cache type: " + s);
return GGML_TYPE_COUNT;
}
static ggml_type kv_cache_type_from_str(const std::string & s) {
const ggml_type type = common_parse_kv_cache_type(s);
if (type == GGML_TYPE_COUNT) {
throw std::runtime_error("Unsupported cache type: " + s);
}
return type;
}
static std::string get_all_kv_cache_types() {
+2
View File
@@ -19,6 +19,8 @@
// CLI argument parsing
//
ggml_type common_parse_kv_cache_type(const std::string & value);
struct common_arg {
std::set<enum llama_example> examples = {LLAMA_EXAMPLE_COMMON};
std::set<enum llama_example> excludes = {};
+2 -34
View File
@@ -493,38 +493,6 @@ static void print_usage(int /* argc */, char ** argv) {
"'first-last' or 'first-last+step' or 'first-last*mult'.\n");
}
static ggml_type ggml_type_from_name(const std::string & s) {
if (s == "f16") {
return GGML_TYPE_F16;
}
if (s == "bf16") {
return GGML_TYPE_BF16;
}
if (s == "f8_e4m3") {
return GGML_TYPE_F8_E4M3;
}
if (s == "q8_0") {
return GGML_TYPE_Q8_0;
}
if (s == "q4_0") {
return GGML_TYPE_Q4_0;
}
if (s == "q4_1") {
return GGML_TYPE_Q4_1;
}
if (s == "q5_0") {
return GGML_TYPE_Q5_0;
}
if (s == "q5_1") {
return GGML_TYPE_Q5_1;
}
if (s == "iq4_nl") {
return GGML_TYPE_IQ4_NL;
}
return GGML_TYPE_COUNT;
}
static cmd_params parse_cmd_params(int argc, char ** argv) {
cmd_params params;
std::string arg;
@@ -644,7 +612,7 @@ static cmd_params parse_cmd_params(int argc, char ** argv) {
std::vector<ggml_type> types;
for (const auto & t : p) {
ggml_type gt = ggml_type_from_name(t);
ggml_type gt = common_parse_kv_cache_type(t);
if (gt == GGML_TYPE_COUNT) {
invalid_param = true;
break;
@@ -664,7 +632,7 @@ static cmd_params parse_cmd_params(int argc, char ** argv) {
std::vector<ggml_type> types;
for (const auto & t : p) {
ggml_type gt = ggml_type_from_name(t);
ggml_type gt = common_parse_kv_cache_type(t);
if (gt == GGML_TYPE_COUNT) {
invalid_param = true;
break;
+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")