Repository navigation
fix(llm): keep the model loaded without evicting its prompt cache - #30
Merged
Merged
Conversation
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.
Owner
Author
Review summary: correctOne sweep, locally. Condition. The ping keeps the model loaded ⇔ it resets the keep_alive timer, and it must not change the prompt cache. Verified:
1892 tests pass. Verdict: correct. I'll merge when CI passes. |
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
"hi"(before)"hi"withOLLAMA_NUM_PARALLEL=2A 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
done_reason: "load", 0 s), resets the keep_alive timer, and leaves the cache alone. Checked directly: after an empty-prompt ping, the model'sexpires_atmoved forward by a full keep-alive window.num_predict: 0, not for an empty prompt; the Phase 5 section now says which is which.Review and CI