@drod915/kvdiff 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +184 -3
- package/dist/cli.js +1866 -0
- package/dist/cli.js.map +1 -0
- package/dist/ui/assets/index-CFO38XKC.js +1 -0
- package/dist/ui/assets/index-GDnY-22g.css +1 -0
- package/dist/ui/index.html +14 -0
- package/package.json +55 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 kvdiff contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,184 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
# kvdiff
|
|
2
|
+
|
|
3
|
+
**Zero-code local proxy that pinpoints the exact UTF-8 byte breaking your LLM prompt cache (Anthropic, OpenAI, Google Gemini / Antigravity, `llama.cpp`/vLLM), tallies wasted dollars, and optionally auto-heals prefixes live.**
|
|
4
|
+
|
|
5
|
+
[](./LICENSE)
|
|
6
|
+
[](https://nodejs.org)
|
|
7
|
+
[-informational.svg)](#architecture)
|
|
8
|
+
|
|
9
|
+
[](https://github.com/flipcyde101/kvdiff/releases/download/v0.1.0/kvdiff-scoped-demo.mp4)
|
|
10
|
+
|
|
11
|
+
[Watch the 15-second kvdiff demo (MP4)](https://github.com/flipcyde101/kvdiff/releases/download/v0.1.0/kvdiff-scoped-demo.mp4)
|
|
12
|
+
|
|
13
|
+

|
|
14
|
+
|
|
15
|
+
**Try the demo:** `npx @drod915/kvdiff --demo`
|
|
16
|
+
|
|
17
|
+
## The problem
|
|
18
|
+
|
|
19
|
+
Prompt caching can cut the input cost of a long agent session sharply (cached input is billed at a fraction of the normal rate), but only while the start of every request stays byte-for-byte identical. One reordered tool definition or one timestamp in the system prompt silently drops you back to full price, and the provider only tells you that the cache missed, not why.
|
|
20
|
+
|
|
21
|
+
kvdiff sits between your agent and the provider as a local proxy, compares each request with the previous one byte by byte, and tells you the exact byte offset and JSON path where the cached prefix broke, what kind of break it was, and what it cost.
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
## See a Prompt-Cache Bust in 5 Seconds (Zero API Keys Required)
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
npx @drod915/kvdiff --demo
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Opens `http://127.0.0.1:4318` with two simulated sessions. The cache hit rates and dollar amounts are illustrative figures from the demo fixture. Actual cache reuse depends on the provider and whether the matching prefix is still warm.
|
|
32
|
+
|
|
33
|
+
1. **Claude Code + 15 MCP Tools (16 turns):** The first request starts at 0% in the simulation, followed by turns with a seeded 94% cache hit rate. On **Turn 14**, a simulated `mcp-github` reconnect changes the tool order. `kvdiff` highlights the differing byte and shows an estimated cost from the fixture. **Turn 15** illustrates `--heal` restoring tool order and displays a seeded 97% cache hit rate.
|
|
34
|
+
2. **Antigravity / Gemini 2.5 Pro (5 turns):** Catches a volatile timestamp injected into `$.systemInstruction.parts[0].text` on Turn 4.
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
> Record the demo in a terminal with `vhs demo/kvdiff.tape`.
|
|
38
|
+
|
|
39
|
+
### What the demo records (checked on the v0.1.0 line)
|
|
40
|
+
|
|
41
|
+
With the demo running you can read the same data the UI shows from the local API:
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
curl -s http://127.0.0.1:4318/__kvdiff/api/sessions/demo-claude-mcp/turns
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Trimmed real output for turn 14 (Windows 11, Node 24.19, built from `main`). The session itself is a seeded fixture, so the token counts and dollar figures are illustrative; the byte diff and classification are computed by kvdiff:
|
|
48
|
+
|
|
49
|
+
```json
|
|
50
|
+
{
|
|
51
|
+
"turnIndex": 14,
|
|
52
|
+
"provider": "anthropic",
|
|
53
|
+
"usage": { "inputTokens": 2100, "cacheReadTokens": 0, "cacheWriteTokens": 109500 },
|
|
54
|
+
"diagnosis": {
|
|
55
|
+
"kind": "TOOL_REORDER",
|
|
56
|
+
"byteOffset": 1670,
|
|
57
|
+
"jsonPath": "$.tools[9].name",
|
|
58
|
+
"summary": "Tool definitions reordered at $.tools[9].name (byte 1670). Enable --heal to sort tools deterministically.",
|
|
59
|
+
"wastedUsd": 0.37674,
|
|
60
|
+
"cacheHitRate": 0
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
The demo makes no provider calls and needs no API keys. Stop it with Ctrl+C.
|
|
66
|
+
|
|
67
|
+
## Install
|
|
68
|
+
|
|
69
|
+
| How | Command |
|
|
70
|
+
|---|---|
|
|
71
|
+
| Run without installing | `npx @drod915/kvdiff --demo` |
|
|
72
|
+
| Install globally | `npm install -g @drod915/kvdiff` (then run `kvdiff`) |
|
|
73
|
+
| From source | `git clone https://github.com/flipcyde101/kvdiff && cd kvdiff && npm ci && npm run build && node dist/cli.js --help` |
|
|
74
|
+
|
|
75
|
+
Needs Node 24 or newer (kvdiff uses the built-in `node:sqlite`, so there are no native addons to compile). Traffic is stored locally in `~/.kvdiff/kvdiff.db`; pass `--db <path>` to put it somewhere else.
|
|
76
|
+
|
|
77
|
+
---
|
|
78
|
+
|
|
79
|
+
## Quick Start
|
|
80
|
+
|
|
81
|
+
Wrap any coding agent CLI with zero code changes:
|
|
82
|
+
|
|
83
|
+
```bash
|
|
84
|
+
# Claude Code (Anthropic Messages API)
|
|
85
|
+
npx @drod915/kvdiff -- claude
|
|
86
|
+
|
|
87
|
+
# Codex CLI / OpenCode (OpenAI Responses & Chat APIs)
|
|
88
|
+
npx @drod915/kvdiff -- codex
|
|
89
|
+
|
|
90
|
+
# Gemini CLI / Antigravity API workflows (Google Gemini API)
|
|
91
|
+
npx @drod915/kvdiff -- gemini
|
|
92
|
+
|
|
93
|
+
# Opt-in deterministic tool-schema healing
|
|
94
|
+
npx @drod915/kvdiff --heal -- claude
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
In wrap mode, the host and path of an existing `GOOGLE_GEMINI_BASE_URL` are
|
|
98
|
+
used for the Gemini upstream. If it is unset or empty, `GEMINI_API_BASE_URL`
|
|
99
|
+
is used instead. `--upstream-gemini <url>` takes priority over either
|
|
100
|
+
environment variable. Query parameters embedded in upstream base URLs are not
|
|
101
|
+
currently preserved. Standalone `serve` and `ui` modes use explicit flags.
|
|
102
|
+
|
|
103
|
+
Or run as a persistent local proxy for Antigravity, Cursor, IDE plugins, or local `llama.cpp`/vLLM:
|
|
104
|
+
|
|
105
|
+
```bash
|
|
106
|
+
# Cloud providers (Anthropic, OpenAI, Google Gemini / Antigravity)
|
|
107
|
+
npx @drod915/kvdiff serve --port 4318
|
|
108
|
+
|
|
109
|
+
# Local llama.cpp / vLLM KV-cache prefix reuse debugging
|
|
110
|
+
npx @drod915/kvdiff serve --port 4318 --upstream-local http://127.0.0.1:8080
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
Then point your client base URL:
|
|
114
|
+
- `ANTHROPIC_BASE_URL=http://127.0.0.1:4318`
|
|
115
|
+
- `OPENAI_BASE_URL=http://127.0.0.1:4318/v1`
|
|
116
|
+
- `GOOGLE_GEMINI_BASE_URL=http://127.0.0.1:4318`
|
|
117
|
+
|
|
118
|
+
---
|
|
119
|
+
|
|
120
|
+
## What `kvdiff` Catches
|
|
121
|
+
|
|
122
|
+
| Break Kind | What Happened on the Wire | Fix |
|
|
123
|
+
|---|---|---|
|
|
124
|
+
| `TOOL_REORDER` | MCP servers reconnected or registered tools in non-deterministic order | Pass `--heal` to sort `tools[]` (and Gemini `functionDeclarations[]`) deterministically |
|
|
125
|
+
| `TOOL_MUTATED` | A dynamic tool description or schema field changed mid-session | Pin static tool descriptions outside the cached prefix |
|
|
126
|
+
| `VOLATILE_SYSTEM` | A timestamp, UUID, or mutable status string landed inside `$.system` / `$.systemInstruction` | Move dynamic metadata into the trailing user turn |
|
|
127
|
+
| `TTL_EXPIRED` | Bytes matched 100%, but `>5m` elapsed between turns | Keep interactive sessions warm or opt into extended TTL |
|
|
128
|
+
| `MODEL_CHANGED` | Model name switched mid-session | Keep one model per cache prefix |
|
|
129
|
+
| `PREFIX_TOO_SHORT` | Prompt fell under OpenAI (`1,024`) or Gemini (`1,024`/`2,048`) implicit-cache floor | Expected until context grows past threshold |
|
|
130
|
+
|
|
131
|
+
---
|
|
132
|
+
|
|
133
|
+
## Honest Comparison
|
|
134
|
+
|
|
135
|
+
| Feature | `kvdiff` | `ccusage` / `codexbar` | `ContextSpy` | Langfuse / Phoenix |
|
|
136
|
+
|---|---|---|---|---|
|
|
137
|
+
| **Install & Infra** | 1 cmd (`npx`), built-in `node:sqlite` | 1 cmd (`npx`/binary) | 1 cmd (`npx`) | Docker + Postgres/ClickHouse |
|
|
138
|
+
| **Wire-level UTF-8 byte LCP + JSONPath diff** | **Yes** | No (reads post-hoc logs) | No (token category pies) | No |
|
|
139
|
+
| **Opt-in live prefix healing (`--heal`)** | **Yes** | No | No | No |
|
|
140
|
+
| **Multi-provider (Anthropic + OpenAI + Gemini/Antigravity + `llama.cpp`)** | **Yes** | Single-provider focus | Anthropic/OpenAI | Yes |
|
|
141
|
+
| **Zero-mutation default passthrough** | **Yes** (OAuth/beta headers intact) | N/A (offline log reader) | Yes | SDK instrumentation |
|
|
142
|
+
| **Where competitors win** | - | Zero proxy setup; great historical billing totals across past months | High-level token breakdown visualizations | Production team evals, prompt versioning, multi-user cloud traces |
|
|
143
|
+
|
|
144
|
+
---
|
|
145
|
+
|
|
146
|
+
## Architecture
|
|
147
|
+
|
|
148
|
+
```mermaid
|
|
149
|
+
flowchart LR
|
|
150
|
+
Agent["Coding Agent / IDE\n(Claude Code · Codex · Antigravity · Gemini · llama.cpp)"] -->|"HTTP (127.0.0.1:4318)"| Proxy["kvdiff Proxy\nZero-mutation default\n(opt-in --heal)"]
|
|
151
|
+
Proxy -->|"Immediate chunked SSE flush"| Upstream["Upstream Provider\nAnthropic · OpenAI · Gemini · Local"]
|
|
152
|
+
Proxy -.->|"Non-blocking SSE tee"| Engine["UTF-8 Byte LCP +\nJSONPath Mapper +\nBreak Classifier"]
|
|
153
|
+
Engine --> DB[("~/.kvdiff/kvdiff.db\nNode 24 node:sqlite WAL")]
|
|
154
|
+
DB --> UI["Localhost Waterfall UI\n127.0.0.1:4318"]
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### Safety & Privacy Guarantees
|
|
158
|
+
- **100% Local:** Binds to `127.0.0.1` by default. Stores traffic only in `~/.kvdiff/kvdiff.db`. Zero telemetry, zero cloud accounts.
|
|
159
|
+
- **Strict Zero-Mutation Default:** Without `--heal`, `kvdiff` forwards your exact raw request bytes and every non-hop-by-hop header (`Authorization`, `x-api-key`, `x-goog-api-key`, `anthropic-beta`, `openai-beta`) unmodified.
|
|
160
|
+
- **Zero-Buffering SSE:** Streaming responses flush downstream chunk-by-chunk as they arrive from upstream.
|
|
161
|
+
- **Safe `--heal` Invariant:** `--heal` sorts tools and tool-definition keys, normalizes JSON formatting, and keeps the trailing Anthropic cache breakpoint on the final tool after sorting. It **never** reorders `messages[]`, `contents[]`, `parts[]`, `content[]`, `input[]`, or JSON Schema `enum[]`.
|
|
162
|
+
|
|
163
|
+
---
|
|
164
|
+
|
|
165
|
+
## Development
|
|
166
|
+
|
|
167
|
+
```bash
|
|
168
|
+
git clone https://github.com/flipcyde101/kvdiff.git
|
|
169
|
+
cd kvdiff
|
|
170
|
+
npm install
|
|
171
|
+
npm run typecheck
|
|
172
|
+
npm run build
|
|
173
|
+
npm test
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
Found a cache-break pattern or provider edge case? Open an issue at [github.com/flipcyde101/kvdiff/issues](https://github.com/flipcyde101/kvdiff/issues).
|
|
177
|
+
|
|
178
|
+
## Changelog
|
|
179
|
+
|
|
180
|
+
See [CHANGELOG.md](./CHANGELOG.md).
|
|
181
|
+
|
|
182
|
+
## License
|
|
183
|
+
|
|
184
|
+
[MIT](./LICENSE)
|