Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

Conversation cache: --conversation-cache-min-tokens parks only conversations of at least N tokens

William Charbonneau Perrier624070c5df23
diff --git a/docs/DETAILS.md b/docs/DETAILS.md
index d5a0c1fdf..d8fd0db35 100644
--- a/docs/DETAILS.md
+++ b/docs/DETAILS.md
@@ -932,6 +932,14 @@ If reserving space for growth would evict another conversation, parking uses a
full capture instead.
Oldest parked entries are evicted first.
Oversized snapshots or host allocation failures fall back to ordinary prompt processing.
+`--conversation-cache-min-tokens N` (default 0 = park every conversation) parks only conversations of at least N
+tokens. A parked conversation costs one of the slots whatever its length, and parking is a switch-time decision, so a
+client that interleaves short side requests with one long conversation spends the cache on the side requests and, once
+it is full, evicts the long one oldest-first - exactly the reuse it wanted. Setting the minimum above the side
+requests' length keeps the long conversation parked. The check runs before `make_room()`, so a skipped park evicts
+nothing and stores nothing; the log line is `conversation cache: skip parking (N tokens, below the M minimum;
+parked=P)`. 0 is the pre-flag behaviour and the flag leaves the answers untouched - it only decides which
+conversations keep a slot.
`--conversation-cache-min-free-mib N` (default 2560) additionally requires that
physical-RAM headroom remain available: the engine checks before allocation and
again after capture. Unknown telemetry or insufficient RAM skips parking. Windows
diff --git a/include/strata/core/conversation_cache.hpp b/include/strata/core/conversation_cache.hpp
index 9938bb6f6..56d0d774f 100644
--- a/include/strata/core/conversation_cache.hpp
+++ b/include/strata/core/conversation_cache.hpp
@@ -196,8 +196,14 @@ public:
bool live = false;
};
- ConversationCache(size_t budget, size_t slots) : budget_(budget), slots_(slots) {}
+ ConversationCache(size_t budget, size_t slots, int64_t min_tokens = 0)
+ : budget_(budget), slots_(slots), min_tokens_(min_tokens) {}
bool enabled() const { return budget_ != 0 && slots_ != 0; }
+ /// `--conversation-cache-min-tokens`: whether a conversation of this length is worth one of `slots`. Every
+ /// parked conversation costs a whole slot whatever its length, so a client that interleaves short side requests
+ /// with one long conversation spends the cache on the side requests and evicts the long one (oldest first) -
+ /// exactly the reuse it wanted. 0 parks every conversation, whatever its size.
+ bool wants(int64_t tokens) const { return min_tokens_ <= 0 || tokens >= min_tokens_; }
size_t bytes() const { return bytes_ + reuse_.bytes(); }
size_t size() const { return entries_.size(); }
size_t evictions() const { return evictions_; }
@@ -332,6 +338,7 @@ public:
private:
size_t budget_ = 0, slots_ = 0, bytes_ = 0, evictions_ = 0, superseded_ = 0;
+ int64_t min_tokens_ = 0; // --conversation-cache-min-tokens: 0 parks every conversation, whatever its size
std::deque<SavedConversation> entries_; // least recently active first
ConversationKvReuse reuse_;
};
diff --git a/src/core/conversation_cache_test.cpp b/src/core/conversation_cache_test.cpp
index d31bf635b..3750d1198 100644
--- a/src/core/conversation_cache_test.cpp
+++ b/src/core/conversation_cache_test.cpp
@@ -241,6 +241,23 @@ int main() {
check(!disabled.put(image({1,2,3})) && !no_slots.put(image({1,2,3})), "disabled cache stores nothing");
check(disabled.best(a,{},true).tokens == 0, "disabled cache has no matches");
}
+ {
+ // --conversation-cache-min-tokens: a conversation shorter than the minimum is not worth one of the slots.
+ // Parking is a switch-time decision, so without this a client's short side requests park themselves and,
+ // once the cache is full, evict the long conversation they were interleaved with (oldest first).
+ ConversationCache any(1024, 4, 0), floor(1024, 4, 12288);
+ check(any.enabled() && floor.enabled(), "the minimum does not disable the cache");
+ check(any.wants(0) && any.wants(1) && any.wants(1 << 20), "0 parks every length, as before the flag");
+ check(!floor.wants(0) && !floor.wants(1) && !floor.wants(12287), "below the minimum is not parked");
+ check(floor.wants(12288) && floor.wants(12289), "the minimum itself and above are parked");
+ // the CLI rejects a negative, but the predicate must not invert for one that reaches it another way
+ ConversationCache negative(1024, 4, -1);
+ check(negative.wants(0) && negative.wants(1), "a negative minimum parks everything, like 0");
+ // wants() answers for the length alone; the gate is enabled() && wants(), so a disabled cache parks
+ // nothing whatever the length - keep the two independent rather than folding one into the other
+ ConversationCache disabled_min(0, 4, 12288);
+ check(!disabled_min.enabled() && disabled_min.wants(1 << 20), "a disabled cache still answers for length");
+ }
{
// layer-split parking: checkpoints moved apart into stage parts and put back, no running state copied
auto running = [](std::initializer_list<int32_t> ids, uint8_t salt, size_t parts) {
diff --git a/src/program/generate.cpp b/src/program/generate.cpp
index 433d39576..4ac0fe77e 100644
--- a/src/program/generate.cpp
+++ b/src/program/generate.cpp
@@ -591,6 +591,10 @@ struct Options {
int64_t conversation_cache_mib = 0; // opt-in host RAM for independent conversations
int conversation_cache_slots = 4;
int64_t conversation_cache_min_free_mib = 2560;
+ /// --serve: park only conversations of at least this many tokens. Every parked conversation costs one of
+ /// conversation_cache_slots whatever its length, so without it a client that interleaves short side requests
+ /// with one long conversation evicts the long one (oldest first). 0 = park every conversation.
+ int64_t conversation_cache_min_tokens = 0;
/// --serve SAVE: disk space a session file must leave free where it is written (MiB; 0 = no check)
int64_t session_min_free_mib = 4096;
/// --serve: also keep a checkpoint every N freshly read prompt tokens (0 = only at the last turn boundary)
@@ -735,6 +739,8 @@ void usage() {
" --conversation-cache-slots N --serve: at most N parked conversations (default 4)\n"
" --conversation-cache-min-free-mib N --serve: physical RAM floor when parking or restoring a\n"
" session file (default 2560)\n"
+ " --conversation-cache-min-tokens N --serve: park only conversations of at least N tokens, so\n"
+ " short side requests do not take the slots a long one needs (default 0 = park any)\n"
" --session-min-free-mib N --serve: disk space a SAVE must leave free (default 4096; 0 = no check)\n"
" --vram-elastic --serve (#533, opt-in, NVIDIA): the expert cache in segments (--vram-segment-mib,\n"
" default 512), so the command `VRAM <reserve_mib>` (the server's POST /v1/vram) can\n"
@@ -1814,17 +1820,20 @@ int main(int argc, char** argv) {
else if (a == "--vision") o.vision = true;
else if (a == "--prompt-cache") o.prompt_cache = std::max(0, std::atoi(next("--prompt-cache")));
else if (a == "--conversation-cache-mib" || a == "--conversation-cache-slots" ||
- a == "--conversation-cache-min-free-mib" || a == "--session-min-free-mib") {
+ a == "--conversation-cache-min-free-mib" || a == "--conversation-cache-min-tokens" ||
+ a == "--session-min-free-mib") {
const std::string value = next(a.c_str());
int64_t number = 0;
const auto result = std::from_chars(value.data(), value.data() + value.size(), number);
- const int64_t limit = a == "--conversation-cache-slots" ? INT32_MAX : INT64_MAX / (1024 * 1024);
+ const int64_t limit = (a == "--conversation-cache-slots" || a == "--conversation-cache-min-tokens")
+ ? INT32_MAX : INT64_MAX / (1024 * 1024);
if (result.ec != std::errc{} || result.ptr != value.data() + value.size() || number < 0 || number > limit) {
std::fprintf(stderr, "%s needs a nonnegative integer within range\n", a.c_str());
return 2;
}
if (a == "--conversation-cache-mib") o.conversation_cache_mib = number;
else if (a == "--conversation-cache-min-free-mib") o.conversation_cache_min_free_mib = number;
+ else if (a == "--conversation-cache-min-tokens") o.conversation_cache_min_tokens = number;
else if (a == "--session-min-free-mib") o.session_min_free_mib = number;
else o.conversation_cache_slots = (int) number;
}
@@ -6966,7 +6975,7 @@ int main(int argc, char** argv) {
// (the snapshots accept a null draft)
strata::core::ConversationCache conversations(
o.prompt_cache > 0 ? (size_t) o.conversation_cache_mib * 1024 * 1024 : 0,
- (size_t) o.conversation_cache_slots);
+ (size_t) o.conversation_cache_slots, o.conversation_cache_min_tokens);
// Disk sessions: what a session file is bound to. The model fingerprint samples every model input this
// engine loaded, by role (conversation_file.hpp), once; the config fingerprint covers the RESOLVED settings
// that change what the saved bytes mean - the rope (K is cached post-RoPE), the loaded control vector, the
@@ -7045,6 +7054,16 @@ int main(int argc, char** argv) {
// addresses change: all parked images live in ordinary host vectors.
auto park_current_body = [&](size_t held) -> bool {
if (!conversations.enabled() || !live_ok || live.empty()) return true;
+ // --conversation-cache-min-tokens: a short conversation is not worth one of the slots. Parking is a
+ // switch-time decision, so a client's small side requests would otherwise park themselves and, once the
+ // cache is full, evict the long conversation they were interleaved with (oldest first) - the reuse it
+ // wanted. Returning true is the same non-error skip the byte-budget and RAM-floor paths below take.
+ if (!conversations.wants((int64_t) live.size())) {
+ std::fprintf(stderr, "strata serve: conversation cache: skip parking (%zu tokens, below the %lld "
+ "minimum; parked=%zu)\n",
+ live.size(), (long long) o.conversation_cache_min_tokens, conversations.size());
+ return true;
+ }
// #342: before make_room evicts oldest-first, the copies of this conversation a turn back go (they hold
// nothing the outgoing chain does not, apart from the tail this conversation rewrote)
if (const size_t dropped = conversations.drop_superseded(live, live_imgs, checks, cvec_cached))
@@ -8043,7 +8062,8 @@ int main(int argc, char** argv) {
"expert_slots_primary=%lld expert_cache_primary_mib=%lld spec=%d "
"mtp_max=%d lookup=%d vram_free_mib=%lld cvec=%s arena_mib=%lld pool_workers=%d pcie_frac=%.2f "
"spec_min_p=%.2f conversation_cache_mib=%lld conversation_cache_slots=%d "
- "conversation_cache_min_free_mib=%lld tail_role_token=%lld vram_elastic=%d%s engine=" STRATA_VERSION "\n",
+ "conversation_cache_min_free_mib=%lld conversation_cache_min_tokens=%lld "
+ "tail_role_token=%lld vram_elastic=%d%s engine=" STRATA_VERSION "\n",
(long long) o.max_context, o.kv.c_str(),
(long long) (g.n_qsa_layers() > 0 && ss.qsa_states[ss.qsa_primary()].kv_mode == 1
? ss.qsa_states[ss.qsa_primary()].n_slots * 4 : 0),
@@ -8054,7 +8074,8 @@ int main(int argc, char** argv) {
(long long) ((o.mmap_experts ? src.resident_bytes() : strata::kernels::cpu::expert_layout().total) >> 20),
pool.workers(), o.pcie_frac,
o.spec_min_p, (long long) o.conversation_cache_mib, o.conversation_cache_slots,
- (long long) o.conversation_cache_min_free_mib, (long long) o.tail_role_token, xcache.segmented() ? 1 : 0,
+ (long long) o.conversation_cache_min_free_mib, (long long) o.conversation_cache_min_tokens,
+ (long long) o.tail_role_token, xcache.segmented() ? 1 : 0,
o.batch > 0 ? (" batch_slots=" + std::to_string(o.batch) +
" slot_cache=" + std::to_string(o.prompt_cache > 0 ? 1 : 0) +
" batch_groups=" + std::to_string(o.batch_groups)).c_str() : "");