feat: keep_alive input so a local model stays resident
Some checks failed
CI / Linux x86_64 (Forgejo) (push) Failing after 2s

Ollama unloads a model five minutes after its last request, and the
next request then pays a full reload: 30 to 40 seconds for a large
local model, long enough that an operator assumes something is broken.
The stromnetz demonstrator runs on a notebook from around 12.10. and
hits exactly that after any idle period; it works around it today by
setting OLLAMA_KEEP_ALIVE on the server, which is not expressible in a
flow.

Measured against a live Ollama: without the input the model expired
5:02 after the request, with `keep_alive: "90s"` it expired after 92
seconds. Worth knowing for callers: Ollama keeps the value for the
loaded instance, so a later request that omits keep_alive does not
fall back to five minutes until the model has actually been unloaded.

`-1` is sent as a number rather than a string, because Ollama reads
only the numeric form as "never unload"; quoted it is parsed as a
duration and the setting silently does nothing.

With any other wire format the input is REFUSED, not ignored. It
expresses an expectation about the server, and dropping it quietly
would leave the caller paying the reload while believing it was
handled. The message says what to do instead.

Capability goes 0.2.0 to 0.2.1: the addition is optional and additive,
so `@^0.2` and `@^0` both keep resolving, and an unset value leaves
the request byte-identical to the previous version.

Five guards, including that unset omits the field entirely and that
the other wire formats never carry it. Mutation-checked three ways.

Signed-off-by: flemming-it <sf@flemming.it>
This commit is contained in:
flemming-it 2026-10-07 05:49:16 +02:00
parent dd7b419d37
commit 9483f196a1
8 changed files with 230 additions and 4 deletions

View file

@ -20,6 +20,7 @@ Anthropic Messages API.
| `endpoint` | text | Chat endpoint URL matching the selected `api` (see below). |
| `model` | text | Model identifier (e.g. `qwen2.5:14b`, `claude-fable-5`). |
| `api_key` | text | Optional API key. Never appears in outputs, logs, or events. |
| `keep_alive` | text | Ollama only: how long the server keeps the model resident after this request (`4h`, `30m`, `-1` = indefinitely). Empty = server default, which unloads after five minutes. Refused with any other `api`. |
| `system_prompt` | text | Optional system message. Empty = use the model's default. |
| `api` | text | Optional wire format: `ollama` (default), `openai`, `anthropic`. |
@ -140,3 +141,36 @@ steps:
cargo build --release --target wasm32-wasip2
# Output: target/wasm32-wasip2/release/llm_chat.wasm
```
## Keeping a local model resident
Ollama unloads a model five minutes after its last request. The next
request then pays a full reload, which is 30 to 40 seconds for a large
local model: long enough that an operator assumes something is broken.
`keep_alive` sets that window per request:
```yaml
- id: answer
use: llm.chat@^0.2
with:
endpoint: http://localhost:11434/api/chat
model: qwen2.5:7b
keep_alive: "4h"
prompt: $inputs.question
```
Measured against a live Ollama (2026-10-07): without the input the
model expired 5:02 after the request; with `keep_alive: "90s"`, 92
seconds after. Note that Ollama keeps the value for the loaded
instance, so a later request that omits `keep_alive` does not fall
back to five minutes until the model has actually been unloaded.
`-1` is sent as a number, not a string, because Ollama only reads the
numeric form as "never unload".
Only Ollama has this setting. With `api: openai`, `vllm` or
`anthropic` the input is **refused rather than ignored**: it expresses
an expectation about the server, and silently dropping it would leave
you paying the reload while believing it was handled. For vLLM and
OpenAI-compatible servers, residency is configured on the server.