Skip to content
Back to skills

Cache Keepalive

ASecurity

Keep the prompt cache warm during idle sessions when the project opts in.

  • 2 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added November 6, 2026
ai-agentsshellbash

Security analysis

A100/100

Pro scans all 4 files and shows the line behind each finding

Scanned November 6, 2026

npx -y skills add fnodotdev/fno --skill cache-keepalive --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Cache Keepalive?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Cache Keepalive
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/fnodotdev-cache-keepalive/badge)](https://www.skillsdirectory.com/skills/fnodotdev-cache-keepalive)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: cache-keepalive
description: Keep the prompt cache warm during idle sessions when the project opts in.
metadata:
  requires:
    harness:
      - claude
---

# Cache Keepalive

Schedules 4 pings via ScheduleWakeup to keep the prompt cache warm during idle periods. Self-terminates after ~18 minutes.

**Capability gate.** The scheduler is Claude's ScheduleWakeup. Without it, report `schedule unavailable` and stop. Never claim warmth. Never substitute another timer (CronCreate fires new sessions and does not keep this cache warm).

**Warmth is measured, never assumed.** A scheduled ping re-reads cached context, which makes the context cache-ELIGIBLE. It does not prove a cache HIT. Warmth is proven only by usage data (`cache_read_input_tokens > 0` in the transcript's last assistant usage). Report `warmth unverified` unless that number proves a cache read. Report the measured numbers rather than a defined-true claim.

## Configuration

Project opt-in via `.claude/settings.local.json`:

```json
{
  "cacheKeepalive": true
}
```

When enabled, the SessionStart hook auto-activates keepalive silently.
When absent or false, keepalive is manual-only (`/cache-keepalive`).

### Quick Setup

`/cache-keepalive config claude` - enables auto-activation for the current project:

```bash
mkdir -p .claude
SETTINGS=".claude/settings.local.json"
if [[ -f "$SETTINGS" ]]; then
  jq '.cacheKeepalive = true' "$SETTINGS" > "${SETTINGS}.tmp" && mv "${SETTINGS}.tmp" "$SETTINGS"
else
  echo '{"cacheKeepalive": true}' | jq . > "$SETTINGS"
fi
```

Confirm with: `Cache keepalive auto-activation enabled for this project. Takes effect next session.`

To disable: `/cache-keepalive config claude off`

```bash
SETTINGS=".claude/settings.local.json"
if [[ -f "$SETTINGS" ]]; then
  jq '.cacheKeepalive = false' "$SETTINGS" > "${SETTINGS}.tmp" && mv "${SETTINGS}.tmp" "$SETTINGS"
fi
```

## Process

### 1. Get Cache Stats

```bash
SESSION_ID=$(jq -r '.session_id // ""' ~/.claude/.session-context.json 2>/dev/null)
if [ -n "$SESSION_ID" ]; then
  TRANSCRIPT=$(find ~/.claude/projects/ -name "${SESSION_ID}.jsonl" 2>/dev/null | head -1)
  # The last assistant turn that read the cache; a malformed line is skipped.
  jq -Rrn '[inputs | fromjson? | select(.type == "assistant" and (.message.usage.cache_read_input_tokens // 0) > 0)] | last
    | if . == null then "NO_USAGE_DATA" else
        .message.usage as $u
        | "CACHE_READ=\($u.cache_read_input_tokens // 0)",
          "INPUT=\($u.input_tokens // 0)",
          "TOTAL_CTX=\(($u.cache_read_input_tokens // 0) + ($u.input_tokens // 0) + ($u.cache_creation_input_tokens // 0))",
          "MODEL=\(.message.model // "unknown")"
      end' "$TRANSCRIPT" 2>/dev/null
fi
```

Pricing: resolve input and cache-read prices for the reported model from the provider's current pricing page at report time. Say the numbers were looked up then. Prices drift and were not independently measured here. Without a lookup, report `pricing unverified` and skip the cost comparison instead of quoting a stored literal.

Calculate: uncached = TOTAL_CTX / 1M * input_price. cached = TOTAL_CTX / 1M * cache_read_price.

### 2. Report and Schedule First Ping

Report cache stats to user:

```
Cache keepalive active.
  Context: ~[X]K tokens ([MODEL])
  Cache miss would cost: ~$[uncached] | Cached: ~$[cached] | Savings: ~$[diff]

  Schedule: 4 pings at ~270s intervals (~18 min total), armed via ScheduleWakeup
  Keepalive cost: negligible (each ping is a cached-context re-read)

  Cancel: just type anything (user input naturally cancels the loop)
  Only fires when idle. Normal work refreshes cache automatically.
```

Then schedule the first ping:

```
ScheduleWakeup({
  delaySeconds: 270,
  reason: "cache keepalive ping 1/4 - keeping prompt cache warm",
  prompt: "/cache-keepalive"
})
```

### 3. On Each Wake (pings 1-4)

Determine current ping number by checking conversation for the most recent `[cache-keepalive] Ping N/4` message. If none found, this is ping 1.

Output ONLY:

```
[cache-keepalive] Ping N/4 | cache eligible (warmth unverified unless usage proves a cache read)
```

Then:

- **Pings 1-2:** Schedule next ping with ScheduleWakeup at 270s
- **Ping 3:** Send the OS notification warning (the one sanctioned shell action during a ping), then schedule the final ping:
  ```bash
  if [[ "$(uname)" == "Darwin" ]]; then
    osascript -e 'display notification "Return to your session or the cache will expire on next ping" with title "Cache Keepalive"' 2>/dev/null
  elif command -v notify-send &>/dev/null; then
    notify-send "Cache Keepalive" "Return to your session or the cache will expire on next ping" 2>/dev/null
  fi
  ```
  If neither notifier exists, skip it silently. A missing notifier is not a keepalive failure.
- **Ping 4:** Final warning, stop scheduling (let cache expire gracefully):
  ```
  [cache-keepalive] Ping 4/4 | final ping, cache protection ending. Type anything to continue working.
  ```
  Do NOT call ScheduleWakeup after ping 4.

### 4. Cancellation

User input at any point naturally cancels the ScheduleWakeup loop. No explicit cancel handler needed.

## NEVER

- NEVER use CronCreate (fires new sessions, doesn't keep current cache warm)
- NEVER more than 4 pings per activation
- NEVER read files or run other tools during pings. The two exceptions are ScheduleWakeup itself and the ping-3 OS notification
- NEVER block user input (ScheduleWakeup yields to user naturally)
- NEVER activate without project opt-in (when auto-activated via hook)
- NEVER report "cache warm" without usage data proving a cache read

## Known Limitations and Deferred Work

- Keepalive cannot restore an expired cache or session. See [LIMITATIONS.md](LIMITATIONS.md).

Files in this skill

  • LIMITATIONS.md215 B
  • SKILL.md5.6 KB
  • evals/cache-stats-last-warm-turn.yaml922 B
  • install.sh1.1 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.