openclaw plugin: assemble() rebuilds every history message when anything is compressed, invalidating the provider prompt cache
Summary
HeadroomContextEngine.assemble() (plugins/openclaw/src/engine.ts) returns
systemPromptAddition:
result.tokensSaved > 100
? `[Context compressed by Headroom: ${result.tokensSaved} tokens saved. Use headroom_retrieve with the hash to get full details.]`
: undefined,openclaw prepends a context engine's systemPromptAddition to the system prompt (gateway log: context engine: prepended system prompt addition (N chars)). The note carries the per-turn token count, so on every turn that saves more than 100 tokens the system prompt, the very start of the cached prefix, differs from the previous turn's. With provider prompt caching, the whole context then misses the cache and is re-written.
Measured
openclaw 2026.9.6 with headroom 0.39.0 (proxy + plugin), Claude Opus 5.5 on Amazon Bedrock, from Bedrock's model-invocation usage fields:
| call | cache read | cache write |
|---|---|---|
| last call of turn N | 98,624 | 677 |
| first call of turn N+1 (headroom saved 267 tokens) | 0 | 93,290 |
| subsequent calls of turn N+1 | 93K → 105K | small |
The compression saved 267 tokens. The cache miss converted about 93K tokens of cache reads (5% of the input price on Opus 5.5) into cache writes (125% of input). At those prices the turn cost roughly 4,000× what the compression saved. Turns that save 100 tokens or fewer emit no note and don't miss.
Suggested fixes (any of these)
- Make the note opt-out via plugin config.
- Make it static text without the count, so it stays byte-identical across turns and cache-stable.
- Deliver it outside the cached prefix, for example alongside the latest message rather than in the system prompt.
It may also be worth documenting the broader interaction with prompt caching. Under openclaw 9.6's durable-turn contract, tool output is sent raw in the turn it appears and compressed one turn later, so the prompt prefix changes at the first rewritten message. The retrieval hint matters less for routes that emit no CCR hashes (ccr_hashes: []), where there's nothing to retrieve.
We're carrying a local patch that drops the note (false && result.tokensSaved > 100) until this is resolved upstream.
Source: headroomlabs-ai/headroom