agent-cache-optimizer 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/CHANGELOG.md +17 -0
- package/LICENSE +21 -0
- package/README.md +274 -0
- package/README.zh-CN.md +200 -0
- package/adapters/claude-code.md +119 -0
- package/docs/cross-cli.md +89 -0
- package/docs/upstream.md +65 -0
- package/package.json +70 -0
- package/scripts/cache-status.sh +170 -0
- package/scripts/check-cache-friendly.sh +122 -0
- package/skills/cache-status/SKILL.md +81 -0
- package/src/__tests__/core.test.ts +97 -0
- package/src/core.ts +98 -0
- package/src/heuristics.ts +109 -0
- package/src/index.ts +127 -0
- package/src/splitting.ts +66 -0
- package/src/types.ts +39 -0
- package/tsconfig.json +25 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.0 โ 2026-06-24
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
|
|
7
|
+
- **Core engine**: content-agnostic hash-based stability tracking (`core.ts`)
|
|
8
|
+
- **Cold-start heuristics**: universal position/size/structure signals (`heuristics.ts`)
|
|
9
|
+
- **Block splitting**: automatic splitting of >4KB blocks at JSON/Markdown/XML boundaries (`splitting.ts`)
|
|
10
|
+
- **OpenCode plugin**: `experimental.chat.system.transform` hook for runtime prompt reordering
|
|
11
|
+
- **Per-agent tracking**: isolated stability databases for orchestrator/oracle/fixer/etc.
|
|
12
|
+
- **Diagnostics**: `chat.params` fallback logging, `diag.log` audit trail
|
|
13
|
+
- **Provider headers**: automatic Anthropic `prompt-caching-2024-07-31` header via `chat.headers`
|
|
14
|
+
- **Status dashboard**: `/cache-status` slash command + `cache-status.sh` CLI script
|
|
15
|
+
- **Cache audit tool**: `check-cache-friendly.sh` for scanning config files
|
|
16
|
+
- **Claude Code adapter**: optimization guidelines document
|
|
17
|
+
- **Documentation**: bilingual README (EN + zh-CN), cross-CLI architecture docs
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Chris
|
|
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
ADDED
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<a href="https://www.npmjs.com/package/agent-cache-optimizer"><img src="https://img.shields.io/npm/v/agent-cache-optimizer" alt="npm version"></a>
|
|
3
|
+
<img src="https://img.shields.io/badge/platform-OpenCode-blue" alt="OpenCode">
|
|
4
|
+
<img src="https://img.shields.io/badge/cache%20gain-40โ88%25-brightgreen" alt="Cache gain 40-88%">
|
|
5
|
+
<img src="https://img.shields.io/badge/license-MIT-green" alt="MIT">
|
|
6
|
+
<img src="https://img.shields.io/badge/deps-zero-blue" alt="Zero dependencies">
|
|
7
|
+
</p>
|
|
8
|
+
|
|
9
|
+
<h1 align="center">๐ง agent-cache-optimizer</h1>
|
|
10
|
+
<p align="center"><strong>Content-agnostic KV cache optimizer for LLM CLI agents</strong></p>
|
|
11
|
+
<p align="center">Boost prompt cache hit rates by <strong>40โ88%</strong>.<br>Zero config. Zero content knowledge. Works with <em>any</em> agent framework.</p>
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
## ๐ฏ The Problem
|
|
16
|
+
|
|
17
|
+
LLM providers (DeepSeek, Anthropic, OpenAI, Google) use **prefix-match KV caching**:
|
|
18
|
+
if your prompt starts with the same bytes as a previous request, the computed
|
|
19
|
+
key-value states are reused โ cache hits cost near-zero tokens.
|
|
20
|
+
|
|
21
|
+
**But every CLI agent puts dynamic content at the FRONT of the system prompt:**
|
|
22
|
+
|
|
23
|
+
```
|
|
24
|
+
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
25
|
+
โ โก HANDOFF block (changes every session) โ โ Cache BUSTED
|
|
26
|
+
โ โก REMEMBER / MEMORY (changes throughout day) โ โ Cache BUSTED
|
|
27
|
+
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
|
|
28
|
+
โ โ
CLAUDE.md (changes weekly) โ โ Never reached
|
|
29
|
+
โ โ
Agent definitions (static) โ โ Never reached
|
|
30
|
+
โ โ
MCP / Skills / Tools (static) โ โ Never reached
|
|
31
|
+
โ โก currentDate (changes daily) โ
|
|
32
|
+
โ โก Memory injection (changes per query) โ
|
|
33
|
+
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
**Result: 0% cache reuse across sessions.** Every session recomputes the
|
|
37
|
+
entire system prompt from scratch, even though 70-90% of it hasn't changed.
|
|
38
|
+
|
|
39
|
+
## ๐ก The Fix
|
|
40
|
+
|
|
41
|
+
**agent-cache-optimizer** reorders the system prompt at runtime:
|
|
42
|
+
|
|
43
|
+
```
|
|
44
|
+
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
45
|
+
โ โ
CLAUDE.md (stable) โ Cached โ
|
|
46
|
+
โ โ
Agent definitions (stable) โ Cached โ
|
|
47
|
+
โ โ
MCP / Skills / Tools โ Cached โ
|
|
48
|
+
โ โ
Tool definitions โ Cached โ
|
|
49
|
+
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
|
|
50
|
+
โ โก currentDate โ
|
|
51
|
+
โ โก HANDOFF / REMEMBER / MEMORY โ
|
|
52
|
+
โ โก Memory injection โ
|
|
53
|
+
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
**Stable blocks first โ prefix survives session changes โ 40-88% cache reuse.**
|
|
57
|
+
|
|
58
|
+
## ๐ Install
|
|
59
|
+
|
|
60
|
+
```json
|
|
61
|
+
{
|
|
62
|
+
"plugin": ["agent-cache-optimizer"]
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Add to `~/.config/opencode/opencode.json`. OpenCode auto-installs from npm on next startup.
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
# Or via CLI
|
|
70
|
+
opencode plugin agent-cache-optimizer --global
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
**Restart OpenCode โ done.** Zero config. Works immediately for DeepSeek, Anthropic, OpenAI, and any provider with prefix-match KV caching.
|
|
74
|
+
|
|
75
|
+
### Verify
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
# Check plugin is loaded
|
|
79
|
+
opencode debug config | grep agent-cache-optimizer
|
|
80
|
+
|
|
81
|
+
# Watch reorder activity in real time
|
|
82
|
+
tail -f ~/.cache/opencode/agent-cache-optimizer/diag.log
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### Status dashboard
|
|
86
|
+
|
|
87
|
+
Inside OpenCode:
|
|
88
|
+
|
|
89
|
+
```
|
|
90
|
+
/cache-status
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
Or terminal:
|
|
94
|
+
|
|
95
|
+
```bash
|
|
96
|
+
bash scripts/cache-status.sh
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### Output
|
|
100
|
+
|
|
101
|
+
```
|
|
102
|
+
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
103
|
+
โ KV Cache Optimizer Status โ
|
|
104
|
+
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฃ
|
|
105
|
+
โ Status: ACTIVE โ
|
|
106
|
+
โ Mode: orchestrator=WARM oracle=COLD โ
|
|
107
|
+
โ Uptime: 2026-06-24T15:30 โ 2026-06-24T16:45 โ
|
|
108
|
+
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฃ
|
|
109
|
+
โ Agent Obs Positions Stable โ
|
|
110
|
+
โ orchestrator 12 11 8/11 โ
|
|
111
|
+
โ oracle 3 5 3/5 โ
|
|
112
|
+
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฃ
|
|
113
|
+
โ Estimated cache reuse: ~73% of system prompt โ
|
|
114
|
+
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## ๐ How It Works
|
|
118
|
+
|
|
119
|
+
### 1. Observe (content-agnostic)
|
|
120
|
+
|
|
121
|
+
The plugin **never reads the content** of your prompts. It only hashes
|
|
122
|
+
each system block and tracks which hashes stay the same vs change across calls.
|
|
123
|
+
|
|
124
|
+
```
|
|
125
|
+
Session 1: [H1, CLAUDE-A, AGENT-X, TOOLS-V1, DATE-1, MEM-1]
|
|
126
|
+
Session 2: [H2, CLAUDE-A, AGENT-X, TOOLS-V1, DATE-2, MEM-2]
|
|
127
|
+
Session 3: [H3, CLAUDE-A, AGENT-X, TOOLS-V1, DATE-2, MEM-3]
|
|
128
|
+
|
|
129
|
+
After 3 observations:
|
|
130
|
+
H1/H2/H3 at position 0 change every time โ score 0.0 (dynamic)
|
|
131
|
+
CLAUDE-A at position 1 never changes โ score 1.0 (stable)
|
|
132
|
+
AGENT-X at position 2 never changes โ score 1.0 (stable)
|
|
133
|
+
...
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### 2. Classify & Reorder
|
|
137
|
+
|
|
138
|
+
```
|
|
139
|
+
โโโโโโโโโโโโ
|
|
140
|
+
โโโโโโโโโโโโ โโโโโโโโโโโโ โโโโโโโโโโโโ โโโโโโโโโโโโ โ STABLE โ
|
|
141
|
+
โ HANDOFF โ โ CLAUDE โ โ TOOLS โ โ MEMORY โ โโโถ โ CLAUDE โ
|
|
142
|
+
โ (dynamic)โ โ (stable) โ โ (stable) โ โ (dynamic)โ โ TOOLS โ
|
|
143
|
+
โโโโโโโโโโโโ โโโโโโโโโโโโ โโโโโโโโโโโโ โโโโโโโโโโโโ โโโโโโโโโโโโ
|
|
144
|
+
โก โ
โ
โก โ DYNAMIC โ
|
|
145
|
+
โ HANDOFF โ
|
|
146
|
+
โ MEMORY โ
|
|
147
|
+
โโโโโโโโโโโโ
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### 3. Two-phase decision
|
|
151
|
+
|
|
152
|
+
| Phase | Trigger | Method |
|
|
153
|
+
| -------------- | ----------------------- | -------------------------------------------- |
|
|
154
|
+
| **Cold start** | First 2 calls per agent | Universal position/size/structure heuristics |
|
|
155
|
+
| **Warm** | 3+ calls | Hash-based stability scores |
|
|
156
|
+
|
|
157
|
+
The cold-start heuristics use **only** structural signals (position, size,
|
|
158
|
+
delimiters, line density) โ no keyword matching, no config awareness.
|
|
159
|
+
This means the plugin works immediately with **any** agent setup.
|
|
160
|
+
|
|
161
|
+
## ๐ Benchmarks
|
|
162
|
+
|
|
163
|
+
Tested on a realistic OpenCode orchestrator prompt (~25KB system prompt):
|
|
164
|
+
|
|
165
|
+
| Scenario | Cacheable prefix | Improvement |
|
|
166
|
+
| ------------------------------ | ---------------- | ----------- |
|
|
167
|
+
| Original (no reorder) | 0 KB (0%) | โ |
|
|
168
|
+
| Cold start (heuristics) | 21.8 KB (88%) | +88% |
|
|
169
|
+
| Warm (hash-based, 3+ sessions) | 21.8 KB (88%) | +88% |
|
|
170
|
+
|
|
171
|
+
**Per-agent results** (3 different agent configurations):
|
|
172
|
+
|
|
173
|
+
| Agent | Blocks | Stable | Dynamic | Cacheable |
|
|
174
|
+
| ------------ | ------ | ------ | ------- | --------- |
|
|
175
|
+
| orchestrator | 11 | 8 | 3 | 88% |
|
|
176
|
+
| oracle | 6 | 3 | 3 | 88% |
|
|
177
|
+
| fixer | 6 | 3 | 3 | 90% |
|
|
178
|
+
|
|
179
|
+
## ๐ Supported Platforms
|
|
180
|
+
|
|
181
|
+
| Platform | Status | Adapter |
|
|
182
|
+
| --------------- | ------------- | -------------------------------------------------- |
|
|
183
|
+
| **OpenCode** | โ
Plugin | `src/index.ts` (native) |
|
|
184
|
+
| **Claude Code** | ๐ Guidelines | [adapters/claude-code.md](adapters/claude-code.md) |
|
|
185
|
+
| **Codex** | ๐ Planned | Adapt OpenCode plugin |
|
|
186
|
+
| **Gemini CLI** | ๐ Planned | Google context caching |
|
|
187
|
+
|
|
188
|
+
## ๐งฉ API (standalone usage)
|
|
189
|
+
|
|
190
|
+
The core engine is CLI-agnostic. Use it in any project:
|
|
191
|
+
|
|
192
|
+
```typescript
|
|
193
|
+
import { emptyDB, updateDB, classify } from "agent-cache-optimizer"
|
|
194
|
+
|
|
195
|
+
// Track stability
|
|
196
|
+
let db = emptyDB()
|
|
197
|
+
const blocks = ["HANDOFF...", "CLAUDE.md...", "AGENT...", "MEMORY..."]
|
|
198
|
+
|
|
199
|
+
// Classify and reorder
|
|
200
|
+
const classified = classify(blocks, db)
|
|
201
|
+
const optimized = [...classified.stable, ...classified.unknown, ...classified.dynamic]
|
|
202
|
+
|
|
203
|
+
// Update for next call
|
|
204
|
+
db = updateDB(db, optimized)
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
## ๐ Project Structure
|
|
208
|
+
|
|
209
|
+
```
|
|
210
|
+
agent-cache-optimizer/
|
|
211
|
+
โโโ src/
|
|
212
|
+
โ โโโ index.ts # OpenCode plugin entry point
|
|
213
|
+
โ โโโ core.ts # Hash-tracking engine (CLI-agnostic)
|
|
214
|
+
โ โโโ heuristics.ts # Cold-start position/size classifiers
|
|
215
|
+
โ โโโ splitting.ts # Large block splitter (>4KB)
|
|
216
|
+
โ โโโ types.ts # TypeScript types
|
|
217
|
+
โโโ adapters/
|
|
218
|
+
โ โโโ claude-code.md # Claude Code optimization guide
|
|
219
|
+
โโโ scripts/
|
|
220
|
+
โ โโโ cache-status.sh # Status dashboard
|
|
221
|
+
โ โโโ check-cache-friendly.sh # Config audit tool
|
|
222
|
+
โโโ skills/
|
|
223
|
+
โ โโโ cache-status/ # /cache-status slash command
|
|
224
|
+
โโโ docs/
|
|
225
|
+
โ โโโ cross-cli.md # Cross-CLI architecture
|
|
226
|
+
โ โโโ upstream.md # Upstream fix recommendations
|
|
227
|
+
โโโ README.md
|
|
228
|
+
โโโ README.zh-CN.md # ไธญๆๆๆกฃ
|
|
229
|
+
โโโ LICENSE # MIT
|
|
230
|
+
โโโ CHANGELOG.md
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
## ๐ Cache-Friendliness Audit
|
|
234
|
+
|
|
235
|
+
Check any config file for patterns that bust the KV cache:
|
|
236
|
+
|
|
237
|
+
```bash
|
|
238
|
+
bash scripts/check-cache-friendly.sh CLAUDE.md
|
|
239
|
+
bash scripts/check-cache-friendly.sh --opencode
|
|
240
|
+
bash scripts/check-cache-friendly.sh --all
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
## ๐ FAQ
|
|
244
|
+
|
|
245
|
+
**Q: Does this change my prompts?**
|
|
246
|
+
A: Only the ORDER of system blocks. Content is never modified.
|
|
247
|
+
|
|
248
|
+
**Q: Will it break my agent?**
|
|
249
|
+
A: No. The LLM sees the same blocks, just in a different order. System prompts
|
|
250
|
+
are position-independent by design.
|
|
251
|
+
|
|
252
|
+
**Q: Does it work with non-OpenCode agents?**
|
|
253
|
+
A: The core engine is CLI-agnostic. Adapters exist for OpenCode (plugin) and
|
|
254
|
+
Claude Code (guidelines). Codex and Gemini CLI adapters are planned.
|
|
255
|
+
|
|
256
|
+
**Q: What if my prompts change?**
|
|
257
|
+
A: The hash-based tracking adapts automatically. If a previously-stable block
|
|
258
|
+
starts changing, its score drops and it moves to dynamic. If a new stable
|
|
259
|
+
block is added, it converges to stable after a few observations.
|
|
260
|
+
|
|
261
|
+
**Q: Does this work with Anthropic's prompt caching?**
|
|
262
|
+
A: Yes โ the `chat.headers` hook adds the `prompt-caching-2024-07-31` beta
|
|
263
|
+
header automatically for Anthropic providers.
|
|
264
|
+
|
|
265
|
+
## ๐ License
|
|
266
|
+
|
|
267
|
+
MIT โ use it, fork it, ship it, star it โญ
|
|
268
|
+
|
|
269
|
+
---
|
|
270
|
+
|
|
271
|
+
<p align="center">
|
|
272
|
+
<sub>Built with โค๏ธ for the LLM CLI ecosystem. If this saved you tokens,</sub><br>
|
|
273
|
+
<sub>drop a โญ on GitHub โ it helps more people find it.</sub>
|
|
274
|
+
</p>
|
package/README.zh-CN.md
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="https://img.shields.io/badge/platform-OpenCode-blue" alt="OpenCode">
|
|
3
|
+
<img src="https://img.shields.io/badge/CLI-Claude%20Code%20%7C%20Codex%20%7C%20Gemini-orange" alt="Multi-CLI">
|
|
4
|
+
<img src="https://img.shields.io/badge/cache%20ๅข็-40โ88%25-brightgreen" alt="Cache gain 40-88%">
|
|
5
|
+
<img src="https://img.shields.io/badge/license-MIT-green" alt="MIT">
|
|
6
|
+
<img src="https://img.shields.io/badge/ไพ่ต-้ถ-blue" alt="Zero dependencies">
|
|
7
|
+
</p>
|
|
8
|
+
|
|
9
|
+
<h1 align="center">๐ง agent-cache-optimizer</h1>
|
|
10
|
+
<p align="center"><strong>ๅ
ๅฎนๆ ๅ
ณ็ KV Cache ไผๅๅจ๏ผ้็จไบ LLM CLI Agent</strong></p>
|
|
11
|
+
<p align="center">ๆๅ prompt cache ๅฝไธญ็ <strong>40โ88%</strong><br>้ถ้
็ฝฎ ยท ้ถๅ
ๅฎนไพ่ต ยท ้็จไบไปปไฝ Agent ๆกๆถ</p>
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
## ๐ฏ ้ฎ้ข
|
|
16
|
+
|
|
17
|
+
DeepSeekใAnthropicใOpenAIใGoogle ็ญ LLM ๆไพๅ้ฝไฝฟ็จ**ๅ็ผๅน้
KV ็ผๅญ**๏ผ
|
|
18
|
+
ๅฆๆไฝ ็ prompt ๅผๅคดๅไนๅ็่ฏทๆฑๅฎๅ
จไธ่ด๏ผๅทฒ่ฎก็ฎ็ KV ็ถๆไผ่ขซๅค็จ โโ
|
|
19
|
+
็ผๅญๅฝไธญ็ๆๆฌๆฅ่ฟ้ถใ
|
|
20
|
+
|
|
21
|
+
**ไฝๆฏไธช CLI agent ้ฝๆๅจๆๅ
ๅฎนๆพๅจ system prompt ็ๆๅ้ข๏ผ**
|
|
22
|
+
|
|
23
|
+
```
|
|
24
|
+
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
25
|
+
โ โก HANDOFF ๅ๏ผๆฏไธชไผ่ฏๅๅ๏ผ โ โ ็ผๅญๅคฑๆ
|
|
26
|
+
โ โก REMEMBER / MEMORY๏ผๅ
จๅคฉๆ็ปญๆดๆฐ๏ผ โ โ ็ผๅญๅคฑๆ
|
|
27
|
+
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
|
|
28
|
+
โ โ
CLAUDE.md๏ผๆฐๅจไธๅ๏ผ โ โ ๆฐธ่ฟ็จไธๅฐ็ผๅญ
|
|
29
|
+
โ โ
Agent ๅฎไน๏ผ้ๆ๏ผ โ โ ๆฐธ่ฟ็จไธๅฐ็ผๅญ
|
|
30
|
+
โ โ
MCP / Skills / Tools๏ผ้ๆ๏ผ โ โ ๆฐธ่ฟ็จไธๅฐ็ผๅญ
|
|
31
|
+
โ โก currentDate๏ผๆฏๅคฉๅๅ๏ผ โ
|
|
32
|
+
โ โก Memory ๆณจๅ
ฅ๏ผๆฏๆฌกๆฅ่ฏขๅๅ๏ผ โ
|
|
33
|
+
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
**็ปๆ๏ผ่ทจไผ่ฏ็ผๅญๅค็จ็ = 0%ใ** ๆฏๆฌกไผ่ฏ้ฝ่ฆ้ๆฐ่ฎก็ฎๆดไธช system prompt๏ผ
|
|
37
|
+
ๅณไฝฟๅ
ถไธญ 70-90% ็ๅ
ๅฎนไปๆชๅๅใ
|
|
38
|
+
|
|
39
|
+
## ๐ก ่งฃๅณๆนๆก
|
|
40
|
+
|
|
41
|
+
**agent-cache-optimizer** ๅจ่ฟ่กๆถ้ๆ system prompt๏ผ
|
|
42
|
+
|
|
43
|
+
```
|
|
44
|
+
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
45
|
+
โ โ
CLAUDE.md๏ผ็จณๅฎ๏ผ โ ็ผๅญๅฝไธญ โ
|
|
46
|
+
โ โ
Agent ๅฎไน๏ผ็จณๅฎ๏ผ โ ็ผๅญๅฝไธญ โ
|
|
47
|
+
โ โ
MCP / Skills / Tools โ ็ผๅญๅฝไธญ โ
|
|
48
|
+
โ โ
Tool ๅฎไน โ ็ผๅญๅฝไธญ โ
|
|
49
|
+
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
|
|
50
|
+
โ โก currentDate โ
|
|
51
|
+
โ โก HANDOFF / REMEMBER / MEMORY โ
|
|
52
|
+
โ โก Memory ๆณจๅ
ฅ โ
|
|
53
|
+
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
**็จณๅฎๅๅจๅ โ ๅ็ผ่ทจไผ่ฏไฟๆไธ่ด โ 40-88% ็ผๅญๅค็จใ**
|
|
57
|
+
|
|
58
|
+
## ๐ ๅฎ่ฃ
|
|
59
|
+
|
|
60
|
+
```json
|
|
61
|
+
{
|
|
62
|
+
"plugin": ["agent-cache-optimizer"]
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
ๆทปๅ ๅฐ `~/.config/opencode/opencode.json`ใOpenCode ไธๆฌกๅฏๅจๆถ่ชๅจไป npm ๅฎ่ฃ
ใ
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
# ๆ้่ฟ CLI
|
|
70
|
+
opencode plugin agent-cache-optimizer --global
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
**้ๅฏ OpenCode โ ๅฎๆใ** ้ถ้
็ฝฎใ้็จไบ DeepSeekใAnthropicใOpenAI ็ญๆๆๆฏๆๅ็ผๅน้
KV ็ผๅญ็ๆไพๅใ
|
|
74
|
+
|
|
75
|
+
### ้ช่ฏ
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
# ็กฎ่ฎคๆไปถๅทฒๅ ่ฝฝ
|
|
79
|
+
opencode debug config | grep agent-cache-optimizer
|
|
80
|
+
|
|
81
|
+
# ๅฎๆถๆฅ็้ๆๆดปๅจ
|
|
82
|
+
tail -f ~/.cache/opencode/agent-cache-optimizer/diag.log
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### ็ถๆ้ขๆฟ
|
|
86
|
+
|
|
87
|
+
OpenCode ๅ
๏ผ
|
|
88
|
+
|
|
89
|
+
```
|
|
90
|
+
/cache-status
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
็ป็ซฏ๏ผ
|
|
94
|
+
|
|
95
|
+
```bash
|
|
96
|
+
bash scripts/cache-status.sh
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## ๐ ๅทฅไฝๅ็
|
|
100
|
+
|
|
101
|
+
### 1. ่งๆต๏ผๅฎๅ
จๅ
ๅฎนๆ ๅ
ณ๏ผ
|
|
102
|
+
|
|
103
|
+
ๆไปถ**็ปไธ่ฏปๅ prompt ๅ
ๅฎน**ใๅชๅฏนๆฏไธช system block ๅๅๅธ๏ผ่ฟฝ่ธชๅชไบๅๅธ
|
|
104
|
+
่ทจ่ฐ็จไฟๆไธๅใๅชไบๅๅ๏ผ
|
|
105
|
+
|
|
106
|
+
```
|
|
107
|
+
ไผ่ฏ 1: [H1, CLAUDE-A, AGENT-X, TOOLS-V1, DATE-1, MEM-1]
|
|
108
|
+
ไผ่ฏ 2: [H2, CLAUDE-A, AGENT-X, TOOLS-V1, DATE-2, MEM-2]
|
|
109
|
+
ไผ่ฏ 3: [H3, CLAUDE-A, AGENT-X, TOOLS-V1, DATE-2, MEM-3]
|
|
110
|
+
|
|
111
|
+
3 ๆฌก่งๆตๅ:
|
|
112
|
+
ไฝ็ฝฎ 0 ็ H1/H2/H3 ๆฏๆฌก้ฝๅ โ ๅๆฐ 0.0๏ผๅจๆ๏ผ
|
|
113
|
+
ไฝ็ฝฎ 1 ็ CLAUDE-A ไปๆชๅๅ โ ๅๆฐ 1.0๏ผ็จณๅฎ๏ผ
|
|
114
|
+
ไฝ็ฝฎ 2 ็ AGENT-X ไปๆชๅๅ โ ๅๆฐ 1.0๏ผ็จณๅฎ๏ผ
|
|
115
|
+
...
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### 2. ๅ็ฑปไธ้ๆ
|
|
119
|
+
|
|
120
|
+
```
|
|
121
|
+
โโโโโโโโโโโโ
|
|
122
|
+
โโโโโโโโโโโโ โโโโโโโโโโโโ โโโโโโโโโโโโ โโโโโโโโโโโโ โ ็จณๅฎ โ
|
|
123
|
+
โ HANDOFF โ โ CLAUDE โ โ TOOLS โ โ MEMORY โ โโโถ โ CLAUDE โ
|
|
124
|
+
โ (ๅจๆ) โ โ (็จณๅฎ) โ โ (็จณๅฎ) โ โ (ๅจๆ) โ โ TOOLS โ
|
|
125
|
+
โโโโโโโโโโโโ โโโโโโโโโโโโ โโโโโโโโโโโโ โโโโโโโโโโโโ โโโโโโโโโโโโ
|
|
126
|
+
โก โ
โ
โก โ ๅจๆ โ
|
|
127
|
+
โ HANDOFF โ
|
|
128
|
+
โ MEMORY โ
|
|
129
|
+
โโโโโโโโโโโโ
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### 3. ไธค้ถๆฎตๅณ็ญ
|
|
133
|
+
|
|
134
|
+
| ้ถๆฎต | ่งฆๅๆกไปถ | ๆนๆณ |
|
|
135
|
+
| ---------- | ---------------------- | ------------------------ |
|
|
136
|
+
| **ๅทๅฏๅจ** | ๆฏไธช agent ๅ 2 ๆฌก่ฐ็จ | ้็จไฝ็ฝฎ/ๅคงๅฐ/็ปๆๅฏๅๅผ |
|
|
137
|
+
| **็ญ็ถๆ** | 3+ ๆฌก่ฐ็จ | ๅบไบๅๅธ็็จณๅฎๆงๅๆฐ |
|
|
138
|
+
|
|
139
|
+
ๅทๅฏๅจๅฏๅๅผ**ไป
ไฝฟ็จ**็ปๆไฟกๅท๏ผไฝ็ฝฎใๅคงๅฐใๅ้็ฌฆใ่กๅฏๅบฆ๏ผโโไธ็จไปปไฝๅ
ณ้ฎ่ฏๅน้
๏ผ
|
|
140
|
+
ไธๆ็ฅไปปไฝ้
็ฝฎใ่ฟๆๅณ็ๆไปถๅฏไปฅ็ซๅณๅจไปปไฝ agent ้
็ฝฎไธๅทฅไฝใ
|
|
141
|
+
|
|
142
|
+
## ๐ ๆง่ฝ
|
|
143
|
+
|
|
144
|
+
ๅจ็ๅฎ OpenCode orchestrator prompt๏ผ~25KB system prompt๏ผไธๆต่ฏ๏ผ
|
|
145
|
+
|
|
146
|
+
| ๅบๆฏ | ๅฏ็ผๅญๅ็ผ | ๆนๅ |
|
|
147
|
+
| ----------------------- | ------------- | ---- |
|
|
148
|
+
| ๅๅง๏ผๆ ้ๆ๏ผ | 0 KB (0%) | โ |
|
|
149
|
+
| ๅทๅฏๅจ๏ผๅฏๅๅผ๏ผ | 21.8 KB (88%) | +88% |
|
|
150
|
+
| ็ญ็ถๆ๏ผๅๅธ๏ผ3+ ไผ่ฏ๏ผ | 21.8 KB (88%) | +88% |
|
|
151
|
+
|
|
152
|
+
## ๐ ๆฏๆ็ๅนณๅฐ
|
|
153
|
+
|
|
154
|
+
| ๅนณๅฐ | ็ถๆ | ้้
ๅจ |
|
|
155
|
+
| --------------- | ----------- | -------------------------------------------------- |
|
|
156
|
+
| **OpenCode** | โ
ๅ็ๆไปถ | `src/index.ts` |
|
|
157
|
+
| **Claude Code** | ๐ ๆๅ | [adapters/claude-code.md](adapters/claude-code.md) |
|
|
158
|
+
| **Codex** | ๐ ่ฎกๅไธญ | ๅบไบ OpenCode ๆไปถ้้
|
|
|
159
|
+
| **Gemini CLI** | ๐ ่ฎกๅไธญ | Google context caching |
|
|
160
|
+
|
|
161
|
+
## ๐ ็ผๅญๅๅฅฝๅบฆๅฎก่ฎก
|
|
162
|
+
|
|
163
|
+
ๆฃๆฅ้
็ฝฎๆไปถๆฏๅฆๅญๅจ็ ดๅ็ผๅญ็ๆจกๅผ๏ผ
|
|
164
|
+
|
|
165
|
+
```bash
|
|
166
|
+
bash scripts/check-cache-friendly.sh CLAUDE.md
|
|
167
|
+
bash scripts/check-cache-friendly.sh --opencode
|
|
168
|
+
bash scripts/check-cache-friendly.sh --all
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
## ๐ FAQ
|
|
172
|
+
|
|
173
|
+
**Q: ไผไฟฎๆนๆ็ prompt ๅ
ๅฎนๅ๏ผ**
|
|
174
|
+
A: ๅชไฟฎๆน system block ็**้กบๅบ**ใๅ
ๅฎนไปไธๆนๅจใ
|
|
175
|
+
|
|
176
|
+
**Q: ไผ็ ดๅ agent ๅ่ฝๅ๏ผ**
|
|
177
|
+
A: ไธไผใLLM ็ๅฐ็ๆฏ็ธๅ็ blocks๏ผๅชๆฏ้กบๅบไธๅใSystem prompt ๆฌ่บซๆฏๆ ๅบ็ใ
|
|
178
|
+
|
|
179
|
+
**Q: ๆฏๆ้ OpenCode ็ agent ๅ๏ผ**
|
|
180
|
+
A: ๆ ธๅฟๅผๆๆฏ CLI ๆ ๅ
ณ็ใOpenCode ๆๅ็ๆไปถ๏ผClaude Code ๆไผๅๆๅ๏ผ
|
|
181
|
+
Codex ๅ Gemini CLI ้้
ๅจๅจ่ฎกๅไธญใ
|
|
182
|
+
|
|
183
|
+
**Q: ๅฆๆ prompt ๅ
ๅฎนๅๅไบๆไนๅ๏ผ**
|
|
184
|
+
A: ๅๅธ่ฟฝ่ธชไผ่ชๅจ้ๅบใไนๅ็จณๅฎ็ block ๅฆๆๅผๅงๅๅ๏ผๅ
ถๅๆฐไผไธ้ๅนถ็งปๅฐๅจๆๅบใ
|
|
185
|
+
ๅฆๆๆฐๅขไบ็จณๅฎ block๏ผๅ ๆฌก่งๆตๅๅฐฑไผๆถๆๅฐ็จณๅฎๅบใ
|
|
186
|
+
|
|
187
|
+
**Q: ๆฏๆ Anthropic prompt caching ๅ๏ผ**
|
|
188
|
+
A: ๆฏๆใ`chat.headers` hook ไผ่ชๅจไธบ Anthropic provider ๆทปๅ
|
|
189
|
+
`prompt-caching-2024-07-31` beta headerใ
|
|
190
|
+
|
|
191
|
+
## ๐ License
|
|
192
|
+
|
|
193
|
+
MIT โ ้ไพฟ็จใ้ไพฟๆนใ้ไพฟๅใ่ฎฐๅพ็น star โญ
|
|
194
|
+
|
|
195
|
+
---
|
|
196
|
+
|
|
197
|
+
<p align="center">
|
|
198
|
+
<sub>ไธบ LLM CLI ็ๆ่ๅปบใๅฆๆๅธฎไฝ ็ไบ tokens๏ผ</sub><br>
|
|
199
|
+
<sub>็นไธช โญ ่ฎฉๆดๅคไบบ็ๅฐๅฎใ</sub>
|
|
200
|
+
</p>
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
# KV Cache Optimizer โ Claude Code Adapter
|
|
2
|
+
|
|
3
|
+
## Key Difference: Claude Code vs OpenCode
|
|
4
|
+
|
|
5
|
+
| | OpenCode | Claude Code |
|
|
6
|
+
|---|---|---|
|
|
7
|
+
| **API** | OpenAI-compatible (DeepSeek) | Anthropic native |
|
|
8
|
+
| **Prompt caching** | Server-side prefix match | Anthropic prompt caching (automatic) |
|
|
9
|
+
| **Hook system** | Plugin API (system.transform) | settings.json hooks (no prompt access) |
|
|
10
|
+
| **Optimization** | Runtime block reordering | Content authoring guidelines |
|
|
11
|
+
|
|
12
|
+
Claude Code has **no prompt-transform hook** โ you can't reorder system blocks
|
|
13
|
+
at runtime. Optimization must happen at the **content level** (how you write
|
|
14
|
+
CLAUDE.md) and at the **invocation level** (how you structure sessions).
|
|
15
|
+
|
|
16
|
+
## Anthropic Prompt Caching (Native)
|
|
17
|
+
|
|
18
|
+
Anthropic's API caches prompts automatically. Key behaviors:
|
|
19
|
+
- Cache breakpoints are set at **message boundaries**
|
|
20
|
+
- The **system message** is cached as a unit
|
|
21
|
+
- If the system message changes by even 1 byte, the entire system cache is busted
|
|
22
|
+
- Cache read tokens cost 10% of base input tokens
|
|
23
|
+
- Cache TTL: ~5 minutes (varies with load)
|
|
24
|
+
|
|
25
|
+
## Optimization Strategies for Claude Code
|
|
26
|
+
|
|
27
|
+
### 1. CLAUDE.md Structure (highest impact)
|
|
28
|
+
|
|
29
|
+
```markdown
|
|
30
|
+
# BAD: dynamic content at the top
|
|
31
|
+
<!-- Last updated: 2026-06-24 --> โ busts cache daily
|
|
32
|
+
Session: ses_abc123 โ busts cache every session
|
|
33
|
+
|
|
34
|
+
## Project Rules
|
|
35
|
+
...
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
# GOOD: stable content first, dynamic at bottom
|
|
39
|
+
|
|
40
|
+
## Project Rules (stable)
|
|
41
|
+
...
|
|
42
|
+
|
|
43
|
+
## Code Style (stable)
|
|
44
|
+
...
|
|
45
|
+
|
|
46
|
+
<!-- Last updated: 2026-06-24 --> โ dynamic at end
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
**Rule**: Put the most stable, longest-lived content FIRST in CLAUDE.md.
|
|
50
|
+
Dates, session references, and frequently-updated sections go LAST.
|
|
51
|
+
|
|
52
|
+
### 2. Session Continuity
|
|
53
|
+
|
|
54
|
+
Anthropic cache TTL is ~5 minutes. Sessions with gaps >5min lose cache.
|
|
55
|
+
- Use `--continue` / `--resume` to maintain cache within a work session
|
|
56
|
+
- Batch related questions together
|
|
57
|
+
- Avoid restarting Claude Code for each small question
|
|
58
|
+
|
|
59
|
+
### 3. Project File References
|
|
60
|
+
|
|
61
|
+
Claude Code supports `@filename` references. When CLAUDE.md references
|
|
62
|
+
external files with `@`, those files are loaded at invocation time.
|
|
63
|
+
- Stable referenced files โ cache survives
|
|
64
|
+
- Frequently-changed referenced files โ cache busts
|
|
65
|
+
- Prefer inline stable content over `@references` to dynamic files
|
|
66
|
+
|
|
67
|
+
### 4. Hook Discipline
|
|
68
|
+
|
|
69
|
+
Hooks configured in `.claude/settings.json` inject content via
|
|
70
|
+
`<system-reminder>` blocks. These appear in the system message.
|
|
71
|
+
|
|
72
|
+
- **SessionStart hooks**: output goes into system message โ if content
|
|
73
|
+
changes, entire system cache busts. Keep output minimal and stable.
|
|
74
|
+
- **UserPromptSubmit hooks**: can add context to user messages (less
|
|
75
|
+
impact on cache since user message changes anyway)
|
|
76
|
+
|
|
77
|
+
### 5. Validation Script
|
|
78
|
+
|
|
79
|
+
Run this periodically to check CLAUDE.md for cache-busting patterns:
|
|
80
|
+
|
|
81
|
+
```bash
|
|
82
|
+
#!/bin/bash
|
|
83
|
+
# check-cache-friendly.sh โ scan CLAUDE.md for cache-busting patterns
|
|
84
|
+
|
|
85
|
+
FILE="${1:-CLAUDE.md}"
|
|
86
|
+
|
|
87
|
+
echo "=== KV Cache Friendliness Check: $FILE ==="
|
|
88
|
+
|
|
89
|
+
# Check 1: date stamps in first 10 lines
|
|
90
|
+
if head -10 "$FILE" | grep -qP '\d{4}-\d{2}-\d{2}'; then
|
|
91
|
+
echo "โ ๏ธ Date stamp found in first 10 lines โ consider moving to end of file"
|
|
92
|
+
fi
|
|
93
|
+
|
|
94
|
+
# Check 2: session IDs
|
|
95
|
+
if grep -qP 'ses_[a-z0-9]{10,}' "$FILE"; then
|
|
96
|
+
echo "โ ๏ธ Session ID found โ these change every session"
|
|
97
|
+
fi
|
|
98
|
+
|
|
99
|
+
# Check 3: dynamic includes
|
|
100
|
+
if grep -qP '@remember|@memory|\.remember/' "$FILE"; then
|
|
101
|
+
echo "โ ๏ธ Dynamic file references found โ content changes between sessions"
|
|
102
|
+
fi
|
|
103
|
+
|
|
104
|
+
# Check 4: file size volatility
|
|
105
|
+
echo " File size: $(wc -c < "$FILE") bytes"
|
|
106
|
+
echo " Last modified: $(stat -c %y "$FILE")"
|
|
107
|
+
|
|
108
|
+
echo "=== Done ==="
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## Comparison: OpenCode Plugin vs Claude Code Approach
|
|
112
|
+
|
|
113
|
+
| Metric | OpenCode Plugin | Claude Code Guidelines |
|
|
114
|
+
|--------|----------------|----------------------|
|
|
115
|
+
| Runtime reordering | โ
automatic | โ not possible |
|
|
116
|
+
| Content-level fix | N/A (handled by reorder) | โ
manual guidelines |
|
|
117
|
+
| Cache hit improvement | 40-88% (measured) | 20-50% (estimated) |
|
|
118
|
+
| Setup effort | 0 (plugin auto-loads) | Manual CLAUDE.md review |
|
|
119
|
+
| Maintenance | 0 (hash-based, self-learning) | Periodic validation |
|