Prompt cache optimizer

From Miosa-osa/canopy · View interactive version

Prompt cache optimizer is an AI agent skill from the GitHub repository Miosa-osa/canopy. Optimize token usage through prompt caching and compression

How to use this skill

Skill file

Prompt Cache Optimizer Skill

Reduces token costs by 50-90% through intelligent caching and compression.

When to Activate

Optimization Layers

Layer 1: Semantic Caching

Query → Embedding → Similarity Search → Cache Hit/Miss
         ↓              ↓
    Vector Store    Return cached or call LLM

Cache hits provide 100% token savings with near-instant response.

Layer 2: Prompt Compression (LLMLingua-2)

Layer 3: Strategic Context Placement

Mitigate "lost in the middle" problem:

Layer 4: Hierarchical Memory Tiering

Working Memory (registers)  → Always in context
FIFO Queue (L1/L2 cache)    → Recent exchanges
Archival Memory (disk)      → Semantic search only

Implementation Workflow

  1. Check semantic cache before any LLM call
  2. Compress context using appropriate level
  3. Structure placement - critical info at boundaries
  4. Tier management - evict low-importance content
  5. Cache response for future queries

Compression Decision Matrix

| Context Size | Latency Need | Accuracy Need | Strategy |

|--------------|--------------|---------------|----------|

| <10K tokens | Any | Any | No compression |

| 10K-50K | Low | High | Light (2-3x) |

| 10K-50K | High | Medium | Moderate (5-7x) |

| 50K-100K | Any | Medium | Aggressive (10-20x) |

| >100K | Any | Any | Hierarchical + Aggressive |

Key Patterns

Attention Sink Preservation

For streaming/long sessions, preserve first 4 tokens as attention sinks:

[attention_sinks (4 tokens)] + [rolling_window (window - 4)]

This maintains model coherence over infinite context.

Hybrid Search for RAG

Hybrid = Dense (semantic) + Sparse (BM25)
Fusion = Reciprocal Rank Fusion (RRF)

Achieves 50-100x document reduction with maintained relevance.

Metrics to Track

Integration Points


Based on LLMLingua, GPTCache, MemGPT, and StreamingLLM research

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.