New Tool Radar

AI Concepts

How to Reuse a Long Prompt in llama.cpp With the Slot Prompt Cache

Save a slot's prompt cache to a file and restore it later, so a long shared prompt is not reprocessed on every request.

Schematic diagram with four steps: start the server with a slot save path, save the slot cache after the long prompt, restore it on the next run, then confirm generation starts sooner.

A long system prompt costs time on every request: the server reprocesses the whole prompt before it can generate. The server’s slot prompt cache fixes that — save a slot’s cache to a file, then restore it later and the prompt is already processed.

Save the slot cache, then restore it

Start the server with --slot-save-path set to the directory where cache files live. After a request has processed your long prompt, save the slot’s cache with POST /slots/{id_slot}?action=save and a JSON body naming the file. On the next run, restore it with POST /slots/{id_slot}?action=restore and the same file name, and the prompt is ready without reprocessing. Use ?action=erase to clear a slot’s cache when the prompt changes.

# save
curl -X POST "http://127.0.0.1:8080/slots/0?action=save" -d '{"filename": "long-prompt.bin"}'
# restore later
curl -X POST "http://127.0.0.1:8080/slots/0?action=restore" -d '{"filename": "long-prompt.bin"}'

The cache belongs to the exact prompt and model that produced it: change either and the restored cache is wrong, not just stale. Keep the cache files with the prompts they came from, and erase a slot before reusing it for a different prompt.

Try it: four checks

Check What you should see If you do not see it
Save path set The server starts with –slot-save-path Check the directory exists
Cache saved The save call returns the file name Check the slot id is right
Restore works The restore call succeeds on the next run Same model, same prompt
Faster second run The second request starts generating sooner Compare with a cold prompt

Expected result. We checked the llama.cpp server README, which documents POST /slots/{id_slot}?action=save, ?action=restore and ?action=erase, with the file stored in the –slot-save-path directory. We did not run the server on this machine.

Limits

This guide covers the slot prompt cache only. A cache is only valid for the exact prompt and model that made it. We did not test any save or restore ourselves.

Sources

See all