Skip to content

armgit / janas

Public Imported from GitHub
0

37 tok/s from Qwen3-Next-80B (48 GB) on a 32 GB laptop, 24 without drafts vs 2.8 for llama.cpp; reading 62 vs 16 tok/s. Faster than llama.cpp on all 13 models compared in Q4_K_M. LLMs bigger than your RAM, pure C, CPU or GPU.

Develop 2026-10-14: a ceiling on the reasoning (--reasoning-budget, reasoning_budget_tokens, /think N), closed with a sentence and the closing mark

camauri73e9ec43f809
diff --git a/ChangeLog.md b/ChangeLog.md
index df4fbb3..13f1853 100644
--- a/ChangeLog.md
+++ b/ChangeLog.md
@@ -2,6 +2,11 @@
> Curated, user-facing summary of completed work, newest first.
+## [2026-10-13] - A ceiling on the reasoning
+
+- **The reasoning can be given a ceiling**: `max_reasoning` in the library's chat parameters, `--reasoning-budget` and `reasoning_budget_tokens` (or `thinking_budget_tokens`, the names llama.cpp takes) on the server, `--think N` and `/think N` in the chat. A model still reasoning there has its reasoning closed for it, a sentence ("I have thought enough; now I give the answer.") and the closing mark written as if it had written them, and goes on with its answer; with no ceiling nothing changes, the replies identical to the bit. The sentence is what makes a small model answer: cut at 32 tokens with janas-chat's system message, Qwen3.5-0.8B, Qwen3.5-2B and Qwen3-4B ended 20 of 36 replies themselves given the mark alone, the small ones going on reasoning in their answer and mostly looping to its end, and 35 of 36 given the sentence, the replies a third as long (96 tokens on average against 265); Qwen3-4B answered either way.
+
+
## [2026-10-13] - A build without the GPU, and a request that never ended
- **A build without GPU support links again** (issue #22, reported by mashirotamigi): two of the GPU's functions had no stand-in in such a build, and without `glslangValidator` the link failed.
diff --git a/docs/chat.md b/docs/chat.md
index 240be0e..64653ea 100644
--- a/docs/chat.md
+++ b/docs/chat.md
@@ -52,7 +52,7 @@ Qwen3.5-0.8B does not take `--head 4`: with it, it answered "3" to "2+3". Gemma
| `--no-markdown` | print the model's marks instead of reading them |
| `--no-gpu` | never give the GPU work, whatever the mode says |
| `--mode auto\|eco\|max` | power mode: `eco` never uses the GPU for speed alone |
-| `--think on\|off` | reasoning before replying, for models that do it |
+| `--think on\|off\|N` | reasoning before replying, for models that do it; a number: on, at most N tokens of it, then a sentence that closes it and the answer |
| `--stats` | a line of counters after every reply: speeds, time to the first token, threads, memory |
| `--progress <s>` | a line on stderr every `s` seconds while a long prompt is read (tokens read, rate, time left as an estimate), and one after each reply; on a terminal the status line shows the same while it reads |
@@ -76,7 +76,7 @@ In the chat, a line starting with `/` is a command, and it turns grey-blue as so
| `/context` | how full the context is, and with what: the last prompt by its parts (system message and tools, conversation, last message) |
| `/reset` | a new conversation, and a clean window |
| `/system [text\|off]` | show or change the system message |
-| `/temp <t>`, `/spec`, `/think` | sampling, drafts, reasoning |
+| `/temp <t>`, `/spec`, `/think` | sampling, drafts, reasoning (`/think 512`: on, at most 512 tokens of it) |
| `/mode [auto\|eco\|max]` | power mode, and what the engine chose |
| `/experts [n]` | experts per token: fewer is faster and a little less accurate; any number, `0` included, turns off the leaving out of light experts not in memory |
| `/markdown [on\|off]` | read the model's marks, or print them |
diff --git a/docs/serving.md b/docs/serving.md
index c0f75a4..198ba35 100644
--- a/docs/serving.md
+++ b/docs/serving.md
@@ -28,6 +28,7 @@ bin/x86_64-linux/janas-server qwen3-next.jns
| `--api-key KEY` | ask every client for this key, as `Authorization: Bearer KEY`; `JANAS_API_KEY` sets it too |
| `--name ID` | the model's name for clients; the file's name without `.jns` by default |
| `--think on\|off` | reasoning before replying, for models that do it, when a request does not say |
+| `--reasoning-budget N` | the longest reasoning, in tokens, when a request does not say: there it is closed and the model answers (-1: no limit, the default; 0: no reasoning) |
| `--test` | serve a chat page for trying it out, on `/test` (below) |
| `--max-input N` | the longest input a request may have, in tokens, below the context: a longer one gets a 400 (`input_limit_exceeded`) whose error says `input_tokens`, `max_input_tokens`, `excess_tokens` and the parts, and nothing is ever cut to fit |
| `--warn-input N` | longer inputs are taken, and said on stderr with their parts |
@@ -67,7 +68,7 @@ curl http://127.0.0.1:8080/v1/chat/completions \
A client that lets you set the address of an OpenAI-compatible API should need nothing else: the base URL is `http://127.0.0.1:8080/v1`, and the key whatever you gave `--api-key` (anything, if you gave none). It has been tried here with `curl`, its own test page, the `openai` Python SDK and Open WebUI 0.11.4 (driven through its own API: streamed chat, titles, tags, follow-up suggestions, and a document indexed with Janas-LLM's embeddings and asked about), and by a user with AnythingLLM.
-**Models that reason, and the limits a client sets.** Qwen3 and its successors reason before they answer, unless told not to. OpenAI's `max_completion_tokens` (and `max_output_tokens` in the Responses API) counts the reasoning too, and `janas-server` takes it so. The older `max_tokens` of a chat, which OpenAI does not take at all for the models that reason, is taken for the answer alone: a client that sends it means the answer, and counted with the reasoning a small limit was spent before a word of it was written (Open WebUI asks for a chat's title with 1000, and got none).
+**Models that reason, and the limits a client sets.** Qwen3 and its successors reason before they answer, unless told not to. OpenAI's `max_completion_tokens` (and `max_output_tokens` in the Responses API) counts the reasoning too, and `janas-server` takes it so. The older `max_tokens` of a chat, which OpenAI does not take at all for the models that reason, is taken for the answer alone: a client that sends it means the answer, and counted with the reasoning a small limit was spent before a word of it was written (Open WebUI asks for a chat's title with 1000, and got none). The reasoning itself has a ceiling only when one is asked for: `reasoning_budget_tokens` (or `thinking_budget_tokens`, the names llama.cpp takes) in a request, `--reasoning-budget` for the requests that say nothing. A model still reasoning there has its reasoning closed for it, a sentence ("I have thought enough; now I give the answer.") and the closing mark written as if it had written them, and goes on with its answer, which the limits above still bound: -1 means no ceiling, 0 no reasoning at all. Without one a model that never closed its reasoning would write until the context is full; issue #21's did, its arithmetic broken by a defect since fixed.
**With Open WebUI**, start the server with `--think off`: the titles, tags and suggestions it asks for after every reply are then quick, and a model that reasons can still be asked to (`reasoning_effort`). Its built-in tools, when they are on, add about 5,000 tokens to every prompt; the server reads them once and keeps them computed, but the first reply of a conversation waits for them.
diff --git a/include/janas/llm.bi b/include/janas/llm.bi
index 8de94e4..07cf5db 100644
--- a/include/janas/llm.bi
+++ b/include/janas/llm.bi
@@ -85,6 +85,9 @@ type janas_llm_chat_params
'' JANAS_LLM_ELIMIT, never a cut (0: none)
max_answer as long '' the longest answer, reasoning not counted
'' (0: none); max_reply counts it too
+ max_reasoning as long '' the longest reasoning, closed for the model
+ '' there with a sentence and the mark, the
+ '' answer following (0: none)
end type
type janas_llm_chat_stats
@@ -129,6 +132,9 @@ type janas_llm_chat_stats
'' seconds left to read the prompt (stage INPUT), an estimate; -1:
'' not yet known
input_eta_seconds as double
+ '' of output_tokens, the reasoning's; 1 when max_reasoning closed it
+ reasoning_tokens as ulong
+ reasoning_cut as long
end type
const JANAS_LLM_STAGE_IDLE = 0
diff --git a/include/janas/llm.h b/include/janas/llm.h
index bb383d5..9e9963e 100644
--- a/include/janas/llm.h
+++ b/include/janas/llm.h
@@ -299,6 +299,13 @@ struct janas_llm_chat_params {
JANAS_LLM_FINISH_LENGTH there, like at max_reply, which counts the
reasoning too and still applies. 0: no limit (default) */
int32_t max_answer;
+ /* the longest reasoning, in tokens, for the models that reason: one
+ still reasoning there has it closed - the engine writes a sentence
+ ("I have thought enough; now I give the answer.") and the closing
+ mark as if the model had - and goes on with its answer, which
+ max_answer and max_reply still limit; the sentence is reasoning, a
+ dozen tokens past the limit. 0: no limit (default) */
+ int32_t max_reasoning;
};
JANAS_LLM_API void
@@ -668,6 +675,10 @@ struct janas_llm_chat_stats {
the estimate follows that cost as it grows with the position rather
than the latest speed, which would promise too little */
double input_eta_seconds;
+ /* of output_tokens, those of the reasoning; and 1 when max_reasoning
+ closed it rather than the model */
+ uint32_t reasoning_tokens;
+ int32_t reasoning_cut;
};
/* janas_llm_chat_stats.stage */
diff --git a/src/chat/main.c b/src/chat/main.c
index f92d31d..f640b78 100644
--- a/src/chat/main.c
+++ b/src/chat/main.c
@@ -301,7 +301,8 @@ static void usage(void)
" --mode <m> auto (default), eco or max\n"
" --think <on|off> reasoning before replying, for models that do\n"
" it (default off: it is slow, and a small model\n"
- " reasons at length about very little)\n"
+ " reasons at length about very little); a number\n"
+ " N: on, at most N tokens of it, then the answer\n"
" --stats speed report after every reply\n"
" --no-markdown print the model's marks instead of "
"reading them\n"
@@ -353,9 +354,10 @@ static const struct {
{"/mode [auto|eco|max]", "power mode, and what the engine chose"},
{"/experts [n]",
"experts per token (fewer: faster, a little less accurate)"},
- {"/think [on|off]",
+ {"/think [on|off|N]",
"reasoning before replying, for models that do it (off by default: the "
- "bar says reasoning while it is on)"},
+ "bar says reasoning while it is on); N: on, at most N tokens of it, "
+ "then the answer"},
{"/markdown [on|off]", "read the model's marks, or print them"},
{"/gpu [on|off]", "whether the GPU may be given work"},
{"/mcp", "the MCP servers running and the tools they give the model"},
@@ -968,9 +970,17 @@ static int take_options(int n, char **w, struct chat_opts *o, char *why,
else if (strcmp(a, "--draft") == 0)
o->mp->draft_path = v;
else if (strcmp(a, "--think") == 0) {
- if ((k = opt_choice(a, v, ON_OFF, 2, why, why_len)) < 0)
- return -1;
- o->cp->thinking = k;
+ char *end;
+ long b = strtol(v, &end, 10);
+ if (*v && !*end && b > 0 && b <= INT32_MAX) {
+ o->cp->thinking = 1; /* on, at most b tokens of it */
+ o->cp->max_reasoning = (int32_t)b;
+ } else {
+ if ((k = opt_choice(a, v, ON_OFF, 2, why, why_len)) < 0)
+ return -1;
+ o->cp->thinking = k;
+ o->cp->max_reasoning = 0;
+ }
} else if (strcmp(a, "--mode") == 0) {
if ((k = opt_choice(a, v, MODES, 3, why, why_len)) < 0)
return -1;
@@ -1200,7 +1210,10 @@ static void conf_write(const char *path, const struct chat_opts *o)
(double)c->min_p);
if (c->max_reply)
fprintf(f, "--max %d\n", c->max_reply);
- fprintf(f, "--think %s\n", c->thinking ? "on" : "off");
+ if (c->thinking && c->max_reasoning > 0)
+ fprintf(f, "--think %d\n", c->max_reasoning);
+ else
+ fprintf(f, "--think %s\n", c->thinking ? "on" : "off");
if (!c->speculate)
fprintf(f, "--no-spec\n");
if (*o->stats)
@@ -2012,10 +2025,26 @@ int main(int argc, char **argv)
if (janas_llm_chat_set_params(c, &cp) != JANAS_LLM_OK)
term_printf(&term, "%s\n", janas_llm_last_error());
} else if (strcmp(msg, "/think") == 0) {
- cp.thinking = strcmp(arg, "off") == 0 ? 0
- : strcmp(arg, "on") == 0 ? 1
- : !cp.thinking;
- if (janas_llm_chat_set_params(c, &cp) == JANAS_LLM_OK)
+ char *end;
+ long b = strtol(arg, &end, 10);
+ if (*arg && !*end && b > 0 && b <= INT32_MAX) {
+ cp.thinking = 1; /* on, at most b tokens of it */
+ cp.max_reasoning = (int32_t)b;
+ } else {
+ cp.thinking = strcmp(arg, "off") == 0 ? 0
+ : strcmp(arg, "on") == 0 ? 1
+ : !cp.thinking;
+ if (strcmp(arg, "on") == 0)
+ cp.max_reasoning = 0;
+ }
+ if (janas_llm_chat_set_params(c, &cp) != JANAS_LLM_OK)
+ term_printf(&term, "%s\n", janas_llm_last_error());
+ else if (cp.thinking && cp.max_reasoning > 0)
+ term_printf(&term,
+ "reasoning on, at most %d tokens of it (for "
+ "models that reason)\n",
+ cp.max_reasoning);
+ else
term_printf(&term,
"reasoning %s (for models that reason)\n",
cp.thinking ? "on" : "off");
diff --git a/src/llm/api_chat.c b/src/llm/api_chat.c
index 0148f03..975b018 100644
--- a/src/llm/api_chat.c
+++ b/src/llm/api_chat.c
@@ -30,8 +30,8 @@ static int32_t gen_options(const janas_llm_chat *c, struct janas_gen_options *o)
{
const struct janas_llm_chat_params *p = &c->p;
if (!(p->temperature >= 0) || p->top_k < 0 || !(p->top_p >= 0) ||
- !(p->min_p >= 0) || p->max_reply < 0 || p->top_logprobs < 0 ||
- p->top_logprobs > JANAS_SAMPLE_MAX_TOP ||
+ !(p->min_p >= 0) || p->max_reply < 0 || p->max_reasoning < 0 ||
+ p->top_logprobs < 0 || p->top_logprobs > JANAS_SAMPLE_MAX_TOP ||
!(p->presence_penalty >= -2 && p->presence_penalty <= 2) ||
!(p->frequency_penalty >= -2 && p->frequency_penalty <= 2))
return janas_api_fail(JANAS_LLM_EINVAL, "invalid chat parameters");
@@ -651,6 +651,7 @@ int janas_api_begin_reply(janas_llm_chat *c, uint32_t reply_at, uint32_t total,
c->closed = c->ended = 0;
c->reply_tokens = 0;
c->answer_tokens = 0;
+ c->n_forced = c->i_forced = 0;
c->npend = 0;
c->n_rtext = 0;
c->reply_at = reply_at;
diff --git a/src/llm/api_reply.c b/src/llm/api_reply.c
index ed35c93..27ca129 100644
--- a/src/llm/api_reply.c
+++ b/src/llm/api_reply.c
@@ -4,6 +4,7 @@
* api_reply.c - the public API: the reply, piece by piece, and its counters
* (see include/janas/llm.h).
*/
+#include <stdlib.h>
#include <string.h>
#include <sys/resource.h>
#include <time.h>
@@ -251,6 +252,8 @@ static void take(janas_llm_chat *c, int32_t t)
janas_api_record(c, t, JANAS_LLM_PART_CALL, tmp, n);
return;
}
+ if (c->in_think)
+ c->stats.reasoning_tokens++; /* what max_reasoning counts */
janas_api_record(
c, t, c->in_think ? JANAS_LLM_PART_REASONING : JANAS_LLM_PART_ANSWER,
tmp, n);
@@ -259,6 +262,49 @@ static void take(janas_llm_chat *c, int32_t t)
give(c, tmp, n);
}
+/*
+ * The reasoning at max_reasoning tokens, closed for the model: a sentence of
+ * ours, the format's closing mark (</think>, Gemma's <channel|>) and, after
+ * </think>, the blank line the model itself writes there; the answer follows
+ * (the drafts beyond are dropped). The sentence is what makes a small model
+ * answer: cut at 32 tokens with janas-chat's system message, Qwen3.5-0.8B and
+ * 2B given the mark alone went on reasoning in their answer and mostly looped
+ * to the end of the reply (20 of 36 replies ended by the model, the three
+ * models with Qwen3-4B), given the sentence first answered (35 of 36, the
+ * replies a third as long: 96 tokens on average against 265); Qwen3-4B answered
+ * either way (13 Oct 2026). The sentence is reasoning, shown as such and
+ * counted: the reasoning ends that many tokens past the ceiling. Without a
+ * ceiling a model whose reasoning never closes - issue #21's, its arithmetic
+ * broken - writes on to the end of the context, a chat's max_tokens counting
+ * the answer alone.
+ */
+static void close_reasoning(janas_llm_chat *c)
+{
+ const janas_llm *llm = c->llm;
+ if (llm->think_close < 0)
+ return; /* no mark: the context ends the reply */
+ static const char say[] =
+ "\n\nI have thought enough; now I give the answer.\n";
+ int32_t ids[80];
+ long n = janas_tokenizer_encode(llm->tok, say, sizeof(say) - 1, 0, ids, 64);
+ if (n < 0 || n > 64)
+ n = 0;
+ ids[n++] = llm->think_close;
+ if (llm->format != FORMAT_GEMMA) {
+ long m = janas_tokenizer_encode(llm->tok, "\n\n", 2, 0, ids + n, 8);
+ if (m > 0 && m <= 8)
+ n += m;
+ }
+ if (janas_llm_session_append(c->s, ids, (uint32_t)n) != 0)
+ return; /* no room for it: the context ends the reply */
+ c->stats.reasoning_cut = 1;
+ /* taken one a call (janas_llm_chat_next): the reasoning's last piece
+ is returned as reasoning before the answer begins */
+ memcpy(c->forced, ids, (size_t)n * sizeof(*ids));
+ c->n_forced = (uint32_t)n;
+ c->i_forced = 0;
+}
+
int32_t janas_llm_chat_next(janas_llm_chat *c, char *buf, int32_t cap,
int32_t *len)
{
@@ -297,6 +343,9 @@ int32_t janas_llm_chat_next(janas_llm_chat *c, char *buf, int32_t cap,
c->answer_tokens >= (uint32_t)c->p.max_answer)) {
c->ended = 1;
c->stats.finish = JANAS_LLM_FINISH_LENGTH;
+ } else if (c->i_forced < c->n_forced) {
+ t = c->forced[c->i_forced++]; /* in the session already */
+ got = 1;
} else {
r = janas_llm_session_next(c->s, &t);
got = r == 0;
@@ -319,6 +368,10 @@ int32_t janas_llm_chat_next(janas_llm_chat *c, char *buf, int32_t cap,
} else if (got) {
c->reply_tokens++;
take(c, t);
+ if (c->in_think && c->p.max_reasoning > 0 &&
+ c->stats.reasoning_tokens >= (uint32_t)c->p.max_reasoning &&
+ c->i_forced >= c->n_forced)
+ close_reasoning(c);
}
janas_api_update_stats(c);
if (r != 0) {
diff --git a/src/llm/chat.h b/src/llm/chat.h
index 8999a9d..c5ac24b 100644
--- a/src/llm/chat.h
+++ b/src/llm/chat.h
@@ -82,7 +82,12 @@ struct janas_llm_chat {
int chan_label; /* Gemma: the channel's name ("thought\n") to skip */
uint32_t reply_tokens;
uint32_t answer_tokens; /* of them, outside the reasoning */
- char pend[1024]; /* reply bytes not yet returned */
+ /* the closing of a reasoning cut at max_reasoning: already in the
+ session, taken one a call as if the model wrote them, so that each
+ piece of text comes out as reasoning or answer (close_reasoning) */
+ int32_t forced[80];
+ uint32_t n_forced, i_forced;
+ char pend[1024]; /* reply bytes not yet returned */
size_t npend;
/* where each turn starts in the sequence, so that sliding the context
forgets whole exchanges, and how long the system message is */
diff --git a/src/server/chat.c b/src/server/chat.c
index f441acf..229c9d4 100644
--- a/src/server/chat.c
+++ b/src/server/chat.c
@@ -57,7 +57,7 @@ static int create(struct srv_req *r, int chat)
srv_job_release(j);
return ret;
}
- j->p.thinking = r->srv->cfg.thinking; /* unless the request says */
+ srv_reasoning_default(&r->srv->cfg, &j->p); /* unless the request says */
if (srv_parse_request(r->body, r->n_body, chat, j, &pr, &e) != 0) {
ret = e.oom ? srv_reply_error(r, 500, "server_error", NULL, "%s", e.msg)
: srv_reply_error(r, 400, "invalid_request_error", e.code,
diff --git a/src/server/main.c b/src/server/main.c
index fbdabad..49719a6 100644
--- a/src/server/main.c
+++ b/src/server/main.c
@@ -53,6 +53,10 @@ static void usage(FILE *f)
" --think on|off reasoning before replying, for models that do\n"
" it, when a request does not say (default: the\n"
" model's own)\n"
+ " --reasoning-budget N the longest reasoning, in tokens, when a\n"
+ " request does not say: there the reasoning is\n"
+ " closed and the model answers (-1: no limit,\n"
+ " the default; 0: no reasoning)\n"
" --keep N conversations kept computed besides the one\n"
" in use, so that a client coming back is not\n"
" read again (default 8; 0: none)\n"
@@ -107,23 +111,19 @@ static const char *arg(int argc, char **argv, int *i)
}
/* every option, for the nearest to one mistyped */
-static const char *const OPTIONS[] = {"--help", "--version",
- "--host", "--port",
- "--api-key", "--name",
- "--ctx", "--reserve",
- "--cache", "--mtp",
- "--draft", "--mode",
- "--think", "--embedding-model",
- "--no-mtp", "--no-gpu",
- "--keep", "--keep-memory",
- "--keep-disk", "--store",
- "--no-store", "--queue",
- "--max-body", "--connections",
- "--max-input", "--warn-input",
- "--fim-prefix", "--fim-suffix",
- "--fim-max", "--metrics",
- "--progress", "--no-mcp",
- "--test", "--verbose"};
+static const char *const OPTIONS[] = {
+ "--help", "--version", "--host",
+ "--port", "--api-key", "--name",
+ "--ctx", "--reserve", "--cache",
+ "--mtp", "--draft", "--mode",
+ "--think", "--embedding-model", "--reasoning-budget",
+ "--no-mtp", "--no-gpu", "--keep",
+ "--keep-memory", "--keep-disk", "--store",
+ "--no-store", "--queue", "--max-body",
+ "--connections", "--max-input", "--warn-input",
+ "--fim-prefix", "--fim-suffix", "--fim-max",
+ "--metrics", "--progress", "--no-mcp",
+ "--test", "--verbose"};
/* the value of option argv[*i], read whole within [lo, hi]; a mistake
ends the program, as a missing value does */
@@ -206,6 +206,7 @@ int main(int argc, char **argv)
c->max_connections = 64;
c->api_key = getenv("JANAS_API_KEY");
c->thinking = -1;
+ c->reasoning_budget = -1;
for (int i = 1; i < argc; i++) {
const char *a = argv[i];
if (!strcmp(a, "--help") || !strcmp(a, "-h")) {
@@ -243,6 +244,8 @@ int main(int argc, char **argv)
} else if (!strcmp(a, "--think")) {
static const char *const on_off[] = {"off", "on"};
c->thinking = choice(argc, argv, &i, on_off, 2);
+ } else if (!strcmp(a, "--reasoning-budget")) {
+ c->reasoning_budget = (int)num(argc, argv, &i, -1, INT32_MAX);
} else if (!strcmp(a, "--embedding-model")) {
c->embed_path = arg(argc, argv, &i);
} else if (!strcmp(a, "--no-mtp")) {
diff --git a/src/server/request.h b/src/server/request.h
index 70cde26..0d581ef 100644
--- a/src/server/request.h
+++ b/src/server/request.h
@@ -23,6 +23,10 @@ char *srv_dup_n(const char *s, size_t n);
/* A number between lo and hi: 1 with *out set, 0 absent (or null), -1 not a
number or out of range. */
int srv_num(yyjson_val *v, double lo, double hi, double *out);
+/* A ceiling on the reasoning by llama.cpp's names, reasoning_budget_tokens
+ or thinking_budget_tokens: -1 none, 0 no reasoning, N at most N tokens of
+ it, then the answer. 0, or -1 when the value is not one of those. */
+int srv_reasoning_budget(yyjson_val *root, struct janas_llm_chat_params *p);
/* A value as JSON text (NULL on memory). */
char *srv_json_text(yyjson_val *v, size_t *n);
diff --git a/src/server/request_opts.c b/src/server/request_opts.c
index 56daee6..df7ed8d 100644
--- a/src/server/request_opts.c
+++ b/src/server/request_opts.c
@@ -358,5 +358,37 @@ int srv_read_chat_opts(struct srv_perr *e, struct srv_parsed *pr,
yyjson_val *et = kw ? yyjson_obj_get(kw, "enable_thinking") : NULL;
if (et && yyjson_is_bool(et))
j->p.thinking = yyjson_get_bool(et) ? 1 : 0;
+ if (srv_reasoning_budget(root, &j->p) != 0)
+ return srv_pfail(
+ pr, BAD("invalid_value",
+ "'reasoning_budget_tokens' is -1 (no limit), 0 (no "
+ "reasoning) or a number of tokens."));
+ return 0;
+}
+
+void srv_reasoning_default(const struct srv_config *cfg,
+ struct janas_llm_chat_params *p)
+{
+ p->thinking = cfg->thinking;
+ if (cfg->reasoning_budget == 0)
+ p->thinking = 0;
+ else if (cfg->reasoning_budget > 0)
+ p->max_reasoning = cfg->reasoning_budget;
+}
+
+int srv_reasoning_budget(yyjson_val *root, struct janas_llm_chat_params *p)
+{
+ yyjson_val *b = yyjson_obj_get(root, "reasoning_budget_tokens");
+ if (!b)
+ b = yyjson_obj_get(root, "thinking_budget_tokens");
+ double x;
+ int got = srv_num(b, -1, 2147483647.0, &x);
+ if (got < 0 || (got && x != (int32_t)x))
+ return -1;
+ if (got && x == 0) {
+ p->thinking = 0;
+ p->max_reasoning = 0;
+ } else if (got)
+ p->max_reasoning = x < 0 ? 0 : (int32_t)x;
return 0;
}
diff --git a/src/server/responses_in.c b/src/server/responses_in.c
index 3082cd1..5f29d25 100644
--- a/src/server/responses_in.c
+++ b/src/server/responses_in.c
@@ -533,7 +533,7 @@ int resp_read(struct srv_req *r, struct resp *x, int *ret)
yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);
struct srv_job *j = x->j;
j->kind = JOB_CHAT;
- j->p.thinking = r->srv->cfg.thinking;
+ srv_reasoning_default(&r->srv->cfg, &j->p);
if (srv_job_choices(j, 1, 1) != 0)
goto oom;
double v;
@@ -571,6 +571,12 @@ int resp_read(struct srv_req *r, struct resp *x, int *ret)
j->p.thinking = !strcmp(eff, "none") || !strcmp(eff, "minimal") ? 0 : 1;
x->reasoning_asked = 1;
}
+ if (srv_reasoning_budget(root, &j->p) != 0) {
+ bad(r, ret,
+ "'reasoning_budget_tokens' is -1 (no limit), 0 (no "
+ "reasoning) or a number of tokens.");
+ goto out;
+ }
yyjson_val *st = yyjson_obj_get(root, "store");
x->store = !st || !yyjson_is_bool(st) || yyjson_get_bool(st); /* default
true */
diff --git a/src/server/server.h b/src/server/server.h
index 80f9bf7..8c750ab 100644
--- a/src/server/server.h
+++ b/src/server/server.h
@@ -36,9 +36,11 @@ struct srv_config {
size_t max_body; /* bytes of a request body at most */
int max_connections;
int verbose;
- int test; /* serve the test chat page at /test */
- int thinking; /* reasoning when a request does not say: 1, 0, or -1
- for the model's own default */
+ int test; /* serve the test chat page at /test */
+ int thinking; /* reasoning when a request does not say: 1, 0, or -1
+ for the model's own default */
+ int reasoning_budget; /* the longest reasoning when a request does not
+ say, in tokens: -1 no limit (default), 0 none */
const char *embed_path; /* the embedding model, or NULL */
const char *embed_id; /* its name for clients */
const char *store_dir; /* where stored documents outlive the server;
@@ -68,6 +70,11 @@ struct srv_engine;
/* JOB_COUNT: only how many tokens the messages are (j->prompt_tokens) */
enum job_kind { JOB_CHAT, JOB_TEXT, JOB_COUNT, JOB_EMBED };
+/* The server's reasoning for a request that does not say (--think,
+ --reasoning-budget), into p. */
+void srv_reasoning_default(const struct srv_config *cfg,
+ struct janas_llm_chat_params *p);
+
/* A message of a chat request, with the tool calls of an assistant's. */
struct srv_msg {
int32_t role; /* JANAS_LLM_ROLE_... */