Repository navigation
feat: (P1)track prompt prefix stability and report cache hit rate - #1649
rohanshrma222 wants to merge 1 commit into
Conversation
nc-review: comments — 1 nit@rohanshrma222 — a few things worth a look, none blocking. The PR adds prompt-prefix observability: a ⚪ nit · The prefix-stability signal is logged at 🔴 blocking · 🟠 a reviewer would ask for a change · ⚪ optional Automated code review — correctness, security, design, tests, plus duplicates and scope. A human still decides; this is not a substitute for review and is not exhaustive. The required status checks separately cover lint, formatting, types, unused dependencies, the test suite and the build. This bot never merges. Maintainers can rerun with |
|
Hi @rohanshrma222, thanks for this PR! It looks like a maintainer has left feedback Whenever you get a chance, could you take a look at the open comments? |
|
Hi @rohanshrma222, just a heads-up that this PR still has outstanding items If we don't hear back within about a week, we'll close it to keep the queue tidy. |
|
PTAL |
Description
First PR of 6 for #1574 (KV-cache-aware prompt lanes).
Before restructuring prompt construction, we need to be able to see where the
cache actually breaks otherwise there's no way to show that the later PRs
help. This PR adds only measurement; no prompt, tool or history behaviour
changes.
1. Prompt prefix stability tracking (
source/ai-sdk-client/chat/prefix-tracker.ts)Fingerprints each outgoing request — system prompt, tool schemas in send
order, and converted history — and compares it with the previous request on
the same client. One debug line per request reports:
append-only— only new messages were added, so the server can reuse its cachesystem-changed— the system prompt differedtools-changed— which tools were added or removed, or whether they were reorderedhistory-changed— the index of the first rewritten messageOnly hashes are logged, never prompt content. The whole thing is wrapped in
try/catch so diagnostics can never fail a request, and tool schema hashes are
memoized in a
WeakMap, so there's no per-request seriathe first call.
2. Cache hit rate in the usage indicator (
source/usage/format.ts)Tokens: 12.4k | 9.8k cached (82%) | ~$0.02. The percenthan guessed when a provider doesn't report input tokens or reports
inconsistent counts.
Worth noting for reviewers: nanocoder already picks up cached-token counts from
OpenAI-compatible servers (llama.cpp, vLLM, OpenRouter)
just Anthropic — so no provider plumbing was needed here, only surfacing it.
The rest of the plan
Each of these will be its own PR against the same issue:
out of the system prompt into the current user turn — currently the system
prompt differs on every user message, which looks lik
goes unhealthy instead of unregistering mid-session
switch rather than removing them
cache_prompt/id_slot) and docs for--cache-reuse,--enable-prefix-caching, fixed `nuType of Change
Changeset
pnpm changeset) describing thTesting
Automated Tests
.spec.ts/tsxfilespnpm test:allcompletes14 new tests: 11 for the tracker (append-only, system change, tool
add/remove/reorder, schema change, rewritten history, dr
determinism, concurrent ordering), 1 asserting
handleChatrecords eachrequest, and 3 for the hit rate.
On
pnpm test:all:scripts/test.shdoesn't run on myeach check separately —
test:format,test:lint,test:typesandtest:knipall pass, and the targeted spec files pass. The full AVA
failures on Windows (path validation, bash executor, daemon Unix sockets, voice
plugin,
inkimports); I confirmed the one nearest my change,source/usage/calculator.spec.ts, fails identically ona CI run to confirm green on Linux.
Manual Testing
Checklist