Server: chat/completions live tool continuation via tool_call_id
Fixes #609 - On the Anthropic endpoint, tool rounds continue the live KV slot cheaply
(anthropic live continuation match=tool-output-ids). On the OpenAI
chat/completions endpoint the equivalent never fires: clients replay the
assistant tool-call turn re-serialised (thinking stripped to an empty
block, tool call re-encoded from JSON), so exact token/text prefix
matching fails at the previous generation's tail even though common
reaches the entire previous prompt. The live slot is discarded and
prefill restarts from an older disk checkpoint — O(context) per tool
round. Before (Open WebUI, native function calling):
live kv cache miss live=15139 prompt=20460 common=14903 reason=token-mismatch
kv cache stored tokens=15139 trimmed=0 reason=evict key=token-text size=221.61 MiB
chat ctx=5779..14903:9124 TOOLS prompt done 31.253s
The trailing role:"tool" message's tool_call_id is the same precise
continuation handle as Anthropic's tool_use_id, so this PR reuses that
fast path for API_OPENAI chat: collect trailing tool ids at parse time
(chat_prepare_live_continuation, mirroring the Anthropic prepare), and
when the ids and live token frontier match, keep the sampled KV and
append only EOS + tool results + next assistant prefix.
After, same client, same conversation shape, ~24k context:
chat live continuation match=tool-output-ids ids=1 cached=19597 prompt=22207
chat ctx=19597..22207:2610 TOOLS prompt done 11.234s
chat live continuation match=tool-output-ids ids=1 cached=23507 prompt=23552
chat ctx=23507..23552:45 TOOLS prompt done 0.798s
chat live continuation match=tool-output-ids ids=1 cached=24151 prompt=24164
chat ctx=24151..24164:13 TOOLS prompt done 0.230s
Tool rounds drop from 45-90s to 0.2-11s, and the per-round ~300 MiB evict/store KV disk cycle disappears. A genuine mid-conversation prompt change still evicts and re-prefills correctly (verified by editing history mid-chat).
Design notes:
- State is stored in the existing
anthropic_live_*request/slot fields; the matcher, remember, and clear sites are protocol-gated. A rename to something protocol-neutral would touch many more lines — happy to do that if preferred. - The frontier is only remembered for chat when
should_canonicalize_tool_checkpoint()is false, since canonicalization rewrites the live session and would strand the remembered frontier. - Every gate failure returns 0 and falls through to the existing matching chain, so behaviour is unchanged whenever the fast path doesn't apply.
- Adds
test_chat_live_tail_renders_tool_results_onlycovering the role:"tool" tail rendering and the protocol gate; full embedded suite passes.
Testing
make test: full suite passes — all tracks including--server,--tool-call-quality,--long-context, and--logprob-vectors— on Apple M5 Max 128GB, Metal backend, macOS 26.5.2, modelDeepSeek-V4-Flash-IQ2XXS-w2Q2K-AProjQ8-SExpQ8-OutQ8-chat-v2.gguf(q2).make cpu: builds clean (verified on x86-64 Linux).- Live verification: same model fully resident on Metal, Open WebUI 0.10.x
native function calling — eleven consecutive
chat live continuationhits across chained tool rounds at 6k–24k context (0.2–11s per round vs 45–90s before), zero evict/store KV disk writes between rounds, and correct evict + re-prefill on a genuine mid-conversation prompt edit.
Another affected client, confirming this fix matters beyond Open WebUI: Hermes Agent (OpenAI chat/completions + tools) against ds4-server (DeepSeek V4 Flash, Metal, --batched-session 4, --kv-disk-dir).
From a live session today (macOS, Apple Silicon), the same signature — common reaches the previous prompt's tail, slot discarded anyway:
live kv cache miss live=21704 prompt=32809 common=21366 reason=token-mismatch
live kv cache miss live=32790 prompt=37838 common=20542 reason=token-mismatch
Hermes additionally spawns a background review fork (a second concurrent agent loop replaying the same conversation with a divergent tail), so without live continuation every fork/main alternation mutually evicts the 4 KV slots. Client-observed cache rates bounce between 64–100% turn to turn, with 50–130s latencies at only ~40K context. At the extreme end we watched a 175K-context client cold-prefill from token 0 four times in 15 minutes (ctx=0..175249:175249 prompt done 643.560s).
Happy to build this PR branch and validate it against Hermes — the exact repro environment is running here (ds4-server on 127.0.0.1:8001, Hermes gateway driving it over Telegram). Just say the word.
Independent verification, on the exact use case this would help me with (OpenCode, which talks to ds4-server over /v1/chat/completions, not the Anthropic endpoint).
Applied this diff to a fresh clone of main, built clean, ran a minimal 2-turn tool-call exchange (read_file tool, real production quant) against both a baseline build and this patch, same conversation on both:
Baseline (no patch):
live kv cache miss live=395 prompt=419 common=254 reason=token-mismatch
With this patch:
chat live continuation match=tool-output-ids ids=1 cached=395 prompt=420
Cache hit instead of a miss, exactly as described — only the small delta needs prefilling instead of restarting from common. Small example so the absolute numbers aren't dramatic, but the mechanism triggers correctly and matches the qualitative fix described here and in #609. This is the same class of problem reported in #500 (Claude Code / large-codebase tool rounds), for the subset of users going through the OpenAI-compatible endpoint rather than the native Anthropic one — worth linking the two threads together if it isn't already, since #500's later comments (config tuning) don't actually address this path.
A beautiful fix and should be merged ASAP. Thank you!
Rebased onto latest main and verified clean build.
Another affected client, confirming this fix matters beyond Open WebUI: Hermes Agent (OpenAI chat/completions + tools) against ds4-server (DeepSeek V4 Flash, Metal,
--batched-session 4,--kv-disk-dir).From a live session today (macOS, Apple Silicon), the same signature —
commonreaches the previous prompt's tail, slot discarded anyway:live kv cache miss live=21704 prompt=32809 common=21366 reason=token-mismatch live kv cache miss live=32790 prompt=37838 common=20542 reason=token-mismatchHermes additionally spawns a background review fork (a second concurrent agent loop replaying the same conversation with a divergent tail), so without live continuation every fork/main alternation mutually evicts the 4 KV slots. Client-observed cache rates bounce between 64–100% turn to turn, with 50–130s latencies at only ~40K context. At the extreme end we watched a 175K-context client cold-prefill from token 0 four times in 15 minutes (
ctx=0..175249:175249 prompt done 643.560s).Happy to build this PR branch and validate it against Hermes — the exact repro environment is running here (ds4-server on 127.0.0.1:8001, Hermes gateway driving it over Telegram). Just say the word.
Appreciate you testing it — I think this explains part of your picture but probably not the whole thing, and it's worth separating the two failure modes so whoever picks this up next isn't chasing one fix for two bugs.
The fork/main slot contention (4 KV slots, mutually evicted every alternation) is a scheduling problem #611 was never built for — that's #668/#698 territory, not this patch.
But there's likely a second issue specific to Hermes' request shape that #611 also doesn't cover. I ran this exact patch against Hermes Agent myself a couple weeks back (separately from Open WebUI, which is what #611 was built and tested against) and found chat live continuation essentially never fires for Hermes' tool-call traffic — every round falls straight through to reason=token-mismatch → full disk evict/reload, same 350–500MB-per-round pattern you're describing. Never fully root-caused which part of Hermes' serialization breaks the tail match (message ordering, extra metadata between rounds — haven't isolated it), but it's a real gap in the matcher, independent of the slot-contention issue.
So: even with #668/#698 solving the multi-slot scheduling, I'd expect Hermes traffic to still cold-reload on every tool round underneath that, because the continuation matcher itself doesn't recognize its request shape.
If I get time this weekend I'll try to actually isolate what's different about Hermes' tool-call serialization and see if the matcher can be widened to cover it. Will report back either way — happy to compare notes if you get to it first.
For now, hoping @antirez gets a chance to merge this one — the Open WebUI case it was built for is confirmed fixed by two more people since, and the gaps above (Hermes, fork contention) both look like natural follow-ups rather than reasons to hold this back.