prompt-caching-patterns
Use this skill when implementing caching for LLM applications. Activate when the user wants to reduce API costs through caching, implement semantic caching, cache LLM responses, optimize repeated prompts, or set up efficient caching strategies for AI applications.
What this skill does
# Prompt Caching Patterns
Implement effective caching strategies to reduce LLM costs by up to 90%.
## When to Use
- Same or similar prompts are sent repeatedly
- Large system prompts are reused across requests
- Responses can be reused for identical queries
- Need to reduce latency for common requests
- Optimizing costs for high-volume applications
## Caching Strategies
### 1. Provider-Level Caching (Anthropic)
Anthropic offers built-in prompt caching with 90% cost reduction.
```typescript
import Anthropic from '@anthropic-ai/sdk';
const client = new Anthropic();
// Large system context that will be reused
const systemContext = `
[Your long system prompt, documentation, examples, etc.]
This can be many thousands of tokens that you want to cache.
`;
async function queryWithCache(userQuestion: string) {
const response = await client.messages.create({
model: 'claude-sonnet-4-6',
max_tokens: 1024,
system: [
{
type: 'text',
text: systemContext,
cache_control: { type: 'ephemeral' } // Cache for 5 minutes
}
],
messages: [
{ role: 'user', content: userQuestion }
]
});
// Check cache usage
console.log('Cache read tokens:', response.usage.cache_read_input_tokens);
console.log('Cache creation tokens:', response.usage.cache_creation_input_tokens);
return response;
}
```
**Pricing with cache:**
- Cache write: 25% more than base input price
- Cache read: 90% less than base input price
- Break-even: ~2 requests with same cached content
### 2. Response Caching
Cache LLM responses for identical or similar queries.
```typescript
interface CacheEntry {
response: string;
createdAt: number;
ttlMs: number;
metadata: {
model: string;
inputTokens: number;
outputTokens: number;
};
}
class ResponseCache {
private cache = new Map<string, CacheEntry>();
private hashPrompt(prompt: string): string {
// Simple hash for exact matching
return crypto.createHash('sha256').update(prompt).digest('hex');
}
get(prompt: string): string | null {
const key = this.hashPrompt(prompt);
const entry = this.cache.get(key);
if (!entry) return null;
// Check TTL
if (Date.now() - entry.createdAt > entry.ttlMs) {
this.cache.delete(key);
return null;
}
return entry.response;
}
set(prompt: string, response: string, options: { ttlMs?: number; metadata?: any } = {}): void {
const key = this.hashPrompt(prompt);
this.cache.set(key, {
response,
createdAt: Date.now(),
ttlMs: options.ttlMs || 3600000, // 1 hour default
metadata: options.metadata
});
}
}
// Usage
const cache = new ResponseCache();
async function cachedQuery(prompt: string): Promise<string> {
// Check cache first
const cached = cache.get(prompt);
if (cached) {
console.log('Cache hit!');
return cached;
}
// Make API call
const response = await llm.complete(prompt);
// Cache the response
cache.set(prompt, response, { ttlMs: 3600000 });
return response;
}
```
### 3. Semantic Caching
Cache based on meaning, not exact match.
```typescript
import { OpenAIEmbeddings } from 'langchain/embeddings/openai';
class SemanticCache {
private entries: { embedding: number[]; response: string; prompt: string }[] = [];
private embeddings: OpenAIEmbeddings;
private similarityThreshold = 0.95;
constructor() {
this.embeddings = new OpenAIEmbeddings();
}
async get(prompt: string): Promise<string | null> {
const queryEmbedding = await this.embeddings.embedQuery(prompt);
// Find most similar cached prompt
let bestMatch: { similarity: number; response: string } | null = null;
for (const entry of this.entries) {
const similarity = this.cosineSimilarity(queryEmbedding, entry.embedding);
if (similarity > this.similarityThreshold) {
if (!bestMatch || similarity > bestMatch.similarity) {
bestMatch = { similarity, response: entry.response };
}
}
}
return bestMatch?.response || null;
}
async set(prompt: string, response: string): Promise<void> {
const embedding = await this.embeddings.embedQuery(prompt);
this.entries.push({ embedding, response, prompt });
}
private cosineSimilarity(a: number[], b: number[]): number {
let dotProduct = 0;
let normA = 0;
let normB = 0;
for (let i = 0; i < a.length; i++) {
dotProduct += a[i] * b[i];
normA += a[i] * a[i];
normB += b[i] * b[i];
}
return dotProduct / (Math.sqrt(normA) * Math.sqrt(normB));
}
}
// Usage
const semanticCache = new SemanticCache();
// These would hit the cache:
// "What is the capital of France?" -> cached
// "What's France's capital city?" -> semantic match!
```
### 4. Template Caching
Cache static parts, vary dynamic parts.
```typescript
interface PromptTemplate {
staticPart: string;
dynamicParts: string[];
}
class TemplateCache {
private templates = new Map<string, {
staticPartHash: string;
responses: Map<string, string>; // dynamicHash -> response
}>();
generateKey(template: PromptTemplate, values: Record<string, string>): {
templateKey: string;
valuesKey: string;
} {
const templateKey = this.hash(template.staticPart);
const valuesKey = this.hash(JSON.stringify(values));
return { templateKey, valuesKey };
}
get(template: PromptTemplate, values: Record<string, string>): string | null {
const { templateKey, valuesKey } = this.generateKey(template, values);
return this.templates.get(templateKey)?.responses.get(valuesKey) || null;
}
set(template: PromptTemplate, values: Record<string, string>, response: string): void {
const { templateKey, valuesKey } = this.generateKey(template, values);
if (!this.templates.has(templateKey)) {
this.templates.set(templateKey, {
staticPartHash: templateKey,
responses: new Map()
});
}
this.templates.get(templateKey)!.responses.set(valuesKey, response);
}
}
// Usage
const template: PromptTemplate = {
staticPart: `You are a helpful assistant that translates text.
Translate the following to the target language.
Be accurate and natural.`,
dynamicParts: ['text', 'targetLanguage']
};
// Cache hit for same text + language combo
const cached = templateCache.get(template, {
text: 'Hello world',
targetLanguage: 'Spanish'
});
```
## Redis-Based Distributed Cache
```typescript
import Redis from 'ioredis';
class DistributedPromptCache {
private redis: Redis;
private prefix = 'llm:cache:';
constructor(redisUrl: string) {
this.redis = new Redis(redisUrl);
}
private key(prompt: string): string {
const hash = crypto.createHash('sha256').update(prompt).digest('hex');
return `${this.prefix}${hash}`;
}
async get(prompt: string): Promise<string | null> {
const cached = await this.redis.get(this.key(prompt));
if (cached) {
await this.redis.hincrby(`${this.prefix}stats`, 'hits', 1);
} else {
await this.redis.hincrby(`${this.prefix}stats`, 'misses', 1);
}
return cached;
}
async set(prompt: string, response: string, ttlSeconds: number = 3600): Promise<void> {
await this.redis.setex(this.key(prompt), ttlSeconds, response);
}
async getStats(): Promise<{ hits: number; misses: number; hitRate: number }> {
const stats = await this.redis.hgetall(`${this.prefix}stats`);
const hits = parseInt(stats.hits || '0');
const misses = parseInt(stats.misses || '0');
const total = hits + misses;
return {
hits,
misses,
hitRate: total > 0 ? hits / total : 0
};
}
}
```
## Cache Invalidation
```typescript
interface CachePolicy {
ttlMs: number;
invalidateOn: string[]; // Events that invalidate cache
tags: string[]; // For tag-based invalidation
}
class SmartCache {
private cache = new Map<string, { value: string; policy: CachePolicy; createdAt: number }>();
private tagIndeRelated in Backend & APIs
jfrog
IncludedInteract with the JFrog Platform via the JFrog CLI and REST/GraphQL APIs. Use this skill when the user wants to manage Artifactory repositories, upload or download artifacts, manage builds, configure permissions, manage users and groups, work with access tokens, configure JFrog CLI servers, search artifacts, manage properties, set up replication, manage JFrog Projects, run security audits or scans, look up CVE details, query exposures scan results from JFrog Advanced Security, manage release bundles and lifecycle operations, aggregate or export platform data, or perform any JFrog Platform administration task. Also use when the user mentions jf, jfrog, artifactory, xray, distribution, evidence, apptrust, onemodel, graphql, workers, mission control, curation, advanced security, exposures, or any JFrog product name.
cupynumeric-migration-readiness
IncludedPre-migration readiness assessor for porting NumPy to cuPyNumeric. Use BEFORE substantial porting work begins when the user asks whether code will scale on GPU, whether they should migrate to cuPyNumeric, which NumPy patterns transfer cleanly, what must be refactored before porting, or mentions pre-port assessment, scaling analysis, or refactor planning. Inspect the user's source code, look up NumPy usage, cross-reference the cuPyNumeric API support manifest, and distinguish distributed-scaling-friendly patterns from blockers such as unsupported APIs, scalar synchronization, host round-trips, Python/object-heavy control flow, shape/data-dependent branching, and in-place mutation hazards. Produce a verdict of READY, LIGHT REFACTOR, SIGNIFICANT REFACTOR, or NOT RECOMMENDED, with concrete refactor pointers.
alibabacloud-data-agent-skill
IncludedInvoke Alibaba Cloud Apsara Data Agent for Analytics via CLI to perform natural language-driven data analysis on enterprise databases. Data Agent for Analytics is an intelligent data analysis agent developed by Alibaba Cloud Database team for enterprise users. It automatically completes requirement analysis, data understanding, analysis insights, and report generation based on natural language descriptions. This tool supports: discovering data resources (instances/databases/tables) managed in DMS, initiating query or deep analysis sessions, real-time progress tracking, and retrieving analysis conclusions and generated reports. Use this Skill when users need to query databases, analyze data trends, generate data reports, ask questions in natural language, or mention "Data Agent", "data analysis", "database query", "SQL analysis", "data insights".
token-optimizer
IncludedReduce OpenClaw token usage and API costs through smart model routing, heartbeat optimization, budget tracking, and native 2026.2.15 features (session pruning, bootstrap size limits, cache TTL alignment). Use when token costs are high, API rate limits are being hit, or hosting multiple agents at scale. The 4 executable scripts (context_optimizer, model_router, heartbeat_optimizer, token_tracker) are local-only — no network requests, no subprocess calls, no system modifications. Reference files (PROVIDERS.md, config-patches.json) document optional multi-provider strategies that require external API keys and network access if you choose to use them. See SECURITY.md for full breakdown.
resend-cli
IncludedUse this skill when the task is specifically about operating Resend from an AI agent, terminal session, or CI job via the official resend CLI: installing/authenticating the CLI, sending/listing/updating/cancelling emails, batch sends, domains and DNS, webhooks and local listeners, inbound receiving, contacts, topics, segments, broadcasts, templates, API keys, profiles, or debugging Resend CLI/API failures. Trigger on mentions of Resend CLI, `resend`, `resend doctor`, `resend emails send`, `resend domains`, `resend webhooks listen`, `resend emails receiving`, or agent-friendly terminal automation.
alibabacloud-odps-maxframe-coding
IncludedUse this skill for MaxFrame SDK development and documentation navigation on Alibaba Cloud MaxCompute (ODPS). Helps answer MaxFrame API, concept, official example, and supported pandas API questions; create data processing programs; read/write MaxCompute tables; debug jobs (remote or local); and build custom DPE runtime images. Trigger when users mention MaxFrame, MaxCompute with MaxFrame, ODPS table processing, DPE runtime, MaxFrame docs/examples, DataFrame/Tensor operations, or GPU runtime setup. Works for both English and Chinese queries about Alibaba Cloud data processing with MaxFrame.