Repository navigation
[Tutorial] Mastering Host-Memory Prompt Caching in llama-server #20574
Closed
mind-tercero-roldan
started this conversation in
Show and tell
Replies: 3 comments 6 replies
|
Hi @mind-tercero-roldan! I have a question about your tutorial: How did you manage this, considering --system-prompt-file has been removed from recent versions? |
0 replies
|
AI Slop detected |
6 replies
|
Search that results in incorrect information pulled from this page:
I'm also having the problem where it resets the slot every turn, and it's bugging the crap out of me with long-ish turn histories and a slow model. |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Tutorial: Mastering Host-Memory Prompt Caching in llama-server 🚀
Author: mind-tercero-roldan
Date: March 15, 2026
Related: Issue #16391 (PR merged), Issue #13523 (tutorials list)
Difficulty: Intermediate
Read time: ~10 minutes
📋 Table of Contents
🔍 What is Host-Memory Prompt Caching?
Host-memory prompt caching is a performance optimization feature in
llama-serverthat stores pre-computed prompt representations in system RAM (host memory) instead of GPU VRAM. This allows the server to:🎯 The Problem It Solves
Imagine you're running a chatbot with a large system prompt (e.g., 10K tokens of context, rules, examples). Every new user request requires:
Host-memory caching lets you compute the system prompt once, store it in RAM, and hot-swap it into the GPU context when needed.
⚙️ How It Works: The Architecture
The Hot-Swap Mechanism
🎯 When to Use It (and When Not To)
✅ Use It When:
❌ Don't Use It When:
🛠️ Setup: Enabling the Feature
Prerequisites
Basic Command
Key Parameters
--np--cram--ctx-size📊 Configuration Parameters Explained
--cram(Cache RAM)Controls how much host RAM to allocate for prompt caching.
Rule of thumb:
--cram≈ (number of cached prefixes) × (average prefix size in tokens) × (bytes per token)Example:
--cram 1024--np(Number of Slots)Number of concurrent contexts the server can handle.
Important: Each slot needs VRAM for its active KV cache. Host-memory cache is separate.
System Prompt File (Optional)
For shared system prompts across all slots:
This automatically caches the system prompt as a common prefix shared by all slots.
📈 Real-World Benchmark: Before vs After
Test Setup
Scenario 1: No Caching
# Without host-memory caching llama-server --model model.gguf --np 1 --ctx-size 32768Observation: Every request reprocesses the full 8K system prompt.
Scenario 2: With Host-Memory Caching
# With host-memory caching enabled llama-server --model model.gguf --np 4 --cram 256 --ctx-size 32768🎉 Results
🚀 Advanced: Multi-Slot Caching Strategy
Use Case: Chatbot with Multiple Users
You have:
Strategy 1: Shared Prefix + Individual Caches
How it works:
Strategy 2: Dynamic Cache Management
Monitor cache efficiency:
# Enable debug logging llama-server --cram 256 --debug-slotOutput example:
Optimization: Adjust
--crambased on hit rate:--cram(more cache = better)--cram(wasted RAM)🔧 Troubleshooting & Best Practices
Issue 1: "Cache miss rate too high"
Symptom: Requests not benefiting from caching
Solutions:
--debug-slot)--ctx-sizeif prefixes are truncated--system-prompt-filefor truly shared prefixesIssue 2: "Out of RAM" errors
Symptom: Server crashes or OOM killer terminates process
Solutions:
Issue 3: "Cache not persisting across restarts"
Current limitation: Host-memory cache is not persistent (RAM-only).
Workaround: Use
--system-prompt-filefor static content that should always be cached.Best Practices Checklist
--cram 128, scale up based on usage--debug-slotto verify caching works📝 Summary & Key Takeaways
What We Learned
--cram(cache size),--np(slots),--system-prompt-fileQuick Reference Card
Next Steps
--cramvalues for your use case--cache-type-k q8_0) for maximum efficiency🙏 Credits & References
Have questions? Comment below or open a new discussion! 🚀
Found this helpful? Star the repo and consider contributing your own tutorial! ⭐
Published by mind-tercero-roldan on March 15, 2026
Part of the llama.cpp tutorial series — making local LLMs accessible to everyone 💜
All reactions