Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

fix(llm): keep the model loaded without evicting its prompt cache - #30

Merged
mkhellat merged 1 commit into
mainfrom
fix/heartbeat-cache
Nov 26, 2026
Merged

mkhellat merged 1 commit into
mainfrom
fix/heartbeat-cache

Conversation

@mkhellat

Copy link
Copy Markdown
Owner

The first fix from the speed investigation after the maintainer's second retest. Model calls there spent 38–77 s in prefill, where cached turns used to take about 2.5 s.

Root cause (measured: Ollama 0.40.17, one slot, host-RAM prompt cache)

The keep-alive heartbeat sent the real prompt "hi" every 240 s. That prompt then held the cache slot, and Ollama would not restore the planner's long saved prompt over it. Both prompts share most of the chat template, and the restore rule keeps whatever already shares more. So the next turn re-read the whole prompt.

Replaying the exact prompts of such a turn:

Ping before the turn Prefill
"hi" (before) 67.95 s
empty prompt (this PR) 32.5 s
"hi" with OLLAMA_NUM_PARALLEL=2 32.4 s

A turn lasts minutes, so a ping landed before most turns. That matches the 50–77 s seen. The rest of the 32 s comes from other causes the investigation found: the project-context block is never cached, and the answer and history blocks are re-read every turn. They're listed for follow-up PRs.

My first hypothesis was that alternating the planner and answer prompts evicts the cache. It was wrong: Ollama restores saved prompts across that, in 0.1 s.

Change

  • The ping sends an empty prompt. Ollama only loads the model (done_reason: "load", 0 s), resets the keep_alive timer, and leaves the cache alone. Checked directly: after an empty-prompt ping, the model's expires_at moved forward by a full keep-alive window.
  • The manual is corrected. It said a ping must produce tokens to keep the model alive. That holds for a real prompt with num_predict: 0, not for an empty prompt; the Phase 5 section now says which is which.
  • No NEWS entry: this fixes unreleased behaviour.

Review and CI

  • The root-cause investigation was delegated to an Opus subagent. The fix is a one-line change I made myself, plus a test, and the keep-alive check above.
  • The full suite passes (1892), ruff is clean, and the manual builds without warnings.
  • CI runs once on this head: it changes the LLM client.

In the maintainer's second retest, model calls spent 38-77 s in
prefill.  Measured (Ollama 0.40.17, one slot, host-RAM prompt cache):
the keep-alive heartbeat sent the prompt "hi" every 240 s.  That
prompt then held the cache slot, and Ollama would not restore the
planner's long saved prompt over it -- "hi" shares most of the chat
template, so it scored higher -- and the next turn re-read the whole
prompt.  Replaying the exact prompts of such a turn: 68 s of prefill
with the "hi" ping, 32.5 s with a load-only ping.  A turn lasts
minutes, so a ping landed before most turns.

The ping now sends an empty prompt: Ollama only loads the model
(done_reason "load"), resets the keep_alive timer -- checked: the
model's expires_at moved forward -- and leaves the cache alone.  The
manual's claim that a ping must produce tokens to keep the model alive
held for a real prompt with num_predict 0, not for an empty prompt;
the Phase 5 section now says which is which.

* src/codebud/llm_stub.py (ModelHeartbeat._ping): Send an empty prompt.
* tests/test_llm_stub.py (test_the_heartbeat_ping_only_loads_the_model):
New test.
* docs/chapters/llm-execution.texi (Phase 5 Idle and Keep-Alive):
Document the load-only ping and why.
@mkhellat

Copy link
Copy Markdown
Owner Author

Review summary: correct

One sweep, locally.

Condition. The ping keeps the model loaded ⇔ it resets the keep_alive timer, and it must not change the prompt cache.

Verified:

  • live: an empty-prompt ping returns done_reason: load in 0 s and moves expires_at forward by a full keep-alive window;
  • the measured replay: 68 s → 32.5 s;
  • num_ctx matches real calls, so no reload;
  • the new test pins the payload;
  • the manual's Phase 5 claim is corrected, with the distinction kept (a real prompt with num_predict: 0 does not reset the timer).

1892 tests pass.

Verdict: correct. I'll merge when CI passes.

@mkhellat

Copy link
Copy Markdown
Owner Author

Merging. CI is green on all five jobs at the branch head, macOS included, plus the docs build. CI on the merge is skipped: the branch sits directly on main (176f618), which has not moved, so the merged tree is exactly the one CI tested; the merge commit carries [skip ci]. I start the docs workflow by hand for the corrected keep-alive section.

@mkhellat
mkhellat merged commit 896ace7 into main Nov 26, 2026
7 checks passed
@mkhellat
mkhellat deleted the fix/heartbeat-cache branch November 26, 2026 11:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant