Openclaw Cache Hit Issue

ๆœชๅˆ†็ฑป ยท 2026-06-25 19:48

OpenClaw DeepSeek Cache Hit Rate Issue

Date: 2026-06-25 OpenClaw version: 2026.6.5 (5181e4f) Model: deepseek-v4-pro (1000k ctx)

Symptom

In long conversational sessions (>50 turns), DeepSeek KV cache hit rate alternates between ~20% and ~99% on consecutive API calls within the same user message turn.

Overall session cache hit: ~51%, wasting ~15M input tokens (~$27).

Root Cause (Code Trace)

/opt/homebrew/lib/node_modules/openclaw/dist/run-attempt-DXkrH0fF.js

Line 201-207: Turn-scoped workspace developer context files:

const CODEX_TURN_SCOPED_WORKSPACE_DEVELOPER_CONTEXT_BASENAMES = new Set([
    "identity.md",
    "soul.md",
    "user.md"
]);

Line 318: These files are selectively loaded per turn:

const turnScopedDeveloperInstructionFiles = shouldInjectCodexOpenClawPromptContext(params.params)
    ? selectCodexWorkspaceTurnScopedDeveloperInstructionFiles(contextFiles) : [];

Line 8250-8256: Turn collaboration developer instructions are built differently for full turns vs tool-continuation turns:

const buildCodexTurnCollaborationDeveloperInstructions = () =>
    buildTurnCollaborationMode(params, {
        turnScopedDeveloperInstructions: workspaceBootstrapContext.turnScopedDeveloperInstructions,
        ...
    }).settings.developer_instructions ?? void 0;

const buildRenderedCodexDeveloperInstructions = () =>
    joinPresentSections(promptBuild.developerInstructions,
        buildCodexTurnCollaborationDeveloperInstructions());

Mechanism: The developer instructions (containing SOUL.md, IDENTITY.md, USER.md) are injected at the FRONT of the messages array differently between full-turn calls and tool-continuation calls. This creates two different message prefixes, so DeepSeek's KV cache can never serve the full conversation history on full-turn calls.

A full-turn call writes cache entry for prefix [dev_instr_v1, system, history...]. The subsequent tool-continuation call matches this prefix (99% hit). But the NEXT full-turn call has a different dev_instr_v2 โ†’ prefix mismatch โ†’ 20% hit.

Impact

Session Data

Session ID: bb84ac8e-55f3-4024-acac-d5bd7f60d242 Key: agent:main:telegram:direct:1128617346 Turns: 340, Total input: 28M, Cached: 43M, Hit rate: 50.98%

Detailed turn-by-turn analysis available in session file: ~/.openclaw/agents/main/sessions/bb84ac8e-55f3-4024-acac-d5bd7f60d242.jsonl