[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"skill:01M734Y8H4T2SWTJEDZ0W1TYJW":3,"skill-same:01M734Y8H4T2SWTJEDZ0W1TYJW":47},{"skillNo":4,"name":5,"nameZh":6,"description":6,"descriptionZh":6,"repositoryFullName":7,"ownerLogin":8,"ownerAvatarUrl":9,"ownerAccountNo":10,"ownerDisplayName":11,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":15,"categories":16,"keywords":17,"publishedAt":18,"linkOnly":19,"markdownContent":20,"markdownBody":21,"frontMatter":22,"refName":23,"commitSha":24,"skillDirectory":25,"forksCount":26,"repositoryHtmlUrl":27,"repositoryHomepageUrl":28,"ownerHtmlUrl":29,"ownerBlogUrl":30,"ownerDescription":31,"updatedAt":32,"install":33,"resources":46},"01M734Y8H4T2SWTJEDZ0W1TYJW","kv-cache-optimization",null,"Horonomos-laps\u002FYana-AI","Horonomos-laps","https:\u002F\u002Favatars.githubusercontent.com\u002Fu\u002F355299122?v=4","01M76B3QPXE88ET2SQPDPVATZT"," Horonomos-laps","01M72ZQRJPCYPSSWT4D00VJR2A",13,"Apache-2.0","https:\u002F\u002Fgithub.com\u002FHoronomos-laps\u002FYana-AI\u002Fblob\u002Fmain\u002F.claude\u002Fskills\u002Fkv-cache-optimization\u002FSKILL.md",[],[],"2026-11-11 15:11:14",false,"---\nname: kv-cache-optimization\ndescription: KV-cache optimization patterns for LLM inference. Prefix caching, sliding window attention, cache reuse across turns, static cache for fixed prompts, and TTFT reduction strategies. Sources: huggingface\u002Ftransformers (Apache-2.0).\norigin: yana-ai — synthesized from huggingface\u002Ftransformers (Apache-2.0)\nlicense: Apache-2.0\nversion: 1.0.0\ncompatibility: yana-ai >= 1.3.51\n---\n\n# \u002Fkv-cache-optimization\n\n## When to Use\n\n- Multi-turn agent conversations: reuse KV cache from previous turns\n- Fixed system prompt across 1000s of requests: compute once, cache forever\n- Long-context RAG: cache the document, stream different queries against it\n- Reduce TTFT (Time-to-First-Token) for latency-sensitive tool calls\n\n## Do NOT use for\n\n- Single-shot stateless inference (cache doesn't help)\n- Models without `use_cache` support\n\n---\n\n## KV cache mechanics\n\n```\nWithout cache (turn N):\n  All N turns re-computed from scratch: O(N²) attention cost\n\nWith KV cache:\n  Turn 1: compute KV for all tokens, store in cache\n  Turn 2: only compute KV for NEW tokens, attend over cached KV\n  Cost: O(new_tokens × total_cached_tokens) ← linear, not quadratic\n\nMemory cost:\n  KV cache size = 2 × layers × heads × head_dim × seq_len × dtype_bytes\n  Llama-3-8B, 4096 tokens, fp16: 2 × 32 × 32 × 128 × 4096 × 2 ≈ 2 GB\n```\n\n---\n\n## Multi-turn chat with cache reuse (transformers)\n\n```python\nfrom transformers import AutoModelForCausalLM, AutoTokenizer, DynamicCache\n\nmodel     = AutoModelForCausalLM.from_pretrained('meta-llama\u002FLlama-3-8B-Instruct')\ntokenizer = AutoTokenizer.from_pretrained('meta-llama\u002FLlama-3-8B-Instruct')\n\n# Initialize a reusable cache object\ncache = DynamicCache()\n\nmessages = []\n\ndef chat_turn(user_msg: str) -> str:\n  global cache, messages\n  messages.append({'role': 'user', 'content': user_msg})\n\n  inputs   = tokenizer.apply_chat_template(messages, return_tensors='pt').to('cuda')\n  outputs  = model.generate(\n    inputs,\n    past_key_values = cache,       # pass existing cache\n    max_new_tokens  = 256,\n    do_sample       = False,\n    use_cache       = True,\n  )\n\n  # Update cache for next turn\n  cache = outputs.past_key_values   # type: ignore\n\n  new_tokens = outputs.sequences[0][inputs.shape[1]:]\n  reply      = tokenizer.decode(new_tokens, skip_special_tokens=True)\n  messages.append({'role': 'assistant', 'content': reply})\n  return reply\n```\n\n---\n\n## Static cache for fixed system prompt\n\n```python\nfrom transformers import StaticCache\n\n# Pre-compute system prompt KV once\nsystem_prompt = tokenizer('You are a yamtam security agent.', return_tensors='pt').to('cuda')\n\nstatic_cache = StaticCache(\n  config           = model.config,\n  max_batch_size   = 8,\n  max_cache_len    = 4096,\n  device           = 'cuda',\n  dtype            = torch.float16,\n)\n\n# Run system prompt through model to fill static cache\nwith torch.no_grad():\n  model(**system_prompt, past_key_values=static_cache, use_cache=True)\n\n# Reuse this cache for ALL requests — system prompt never recomputed\n```\n\n---\n\n## Sliding window (long context without full KV growth)\n\n```python\n# SlidingWindowCache: only keep last W tokens in KV\nfrom transformers import SlidingWindowCache\n\ncache = SlidingWindowCache(\n  config         = model.config,\n  max_batch_size = 1,\n  max_cache_len  = 2048,   # window size W\n  device         = 'cuda',\n  dtype          = torch.float16,\n)\n# Older tokens evicted as new tokens arrive → constant memory\n```\n\n---\n\n## Anti-Fake-Pass Checklist\n\n```\n❌ Reusing DynamicCache across different prompts → KV state is prompt-specific; cross-contamination\n❌ use_cache=False accidentally → cache not populated, next turn re-computes everything\n❌ Static cache max_cache_len too small → cache overflow raises IndexError mid-generation\n❌ Not detaching cache tensors before serialization → huge graph kept alive, memory leak\n❌ SlidingWindowCache with model not trained for sliding window → incoherent output on long contexts\n❌ Batch size > StaticCache.max_batch_size → silent shape mismatch error\n```\n","# \u002Fkv-cache-optimization\n\n## When to Use\n\n- Multi-turn agent conversations: reuse KV cache from previous turns\n- Fixed system prompt across 1000s of requests: compute once, cache forever\n- Long-context RAG: cache the document, stream different queries against it\n- Reduce TTFT (Time-to-First-Token) for latency-sensitive tool calls\n\n## Do NOT use for\n\n- Single-shot stateless inference (cache doesn't help)\n- Models without `use_cache` support\n\n---\n\n## KV cache mechanics\n\n```\nWithout cache (turn N):\n  All N turns re-computed from scratch: O(N²) attention cost\n\nWith KV cache:\n  Turn 1: compute KV for all tokens, store in cache\n  Turn 2: only compute KV for NEW tokens, attend over cached KV\n  Cost: O(new_tokens × total_cached_tokens) ← linear, not quadratic\n\nMemory cost:\n  KV cache size = 2 × layers × heads × head_dim × seq_len × dtype_bytes\n  Llama-3-8B, 4096 tokens, fp16: 2 × 32 × 32 × 128 × 4096 × 2 ≈ 2 GB\n```\n\n---\n\n## Multi-turn chat with cache reuse (transformers)\n\n```python\nfrom transformers import AutoModelForCausalLM, AutoTokenizer, DynamicCache\n\nmodel     = AutoModelForCausalLM.from_pretrained('meta-llama\u002FLlama-3-8B-Instruct')\ntokenizer = AutoTokenizer.from_pretrained('meta-llama\u002FLlama-3-8B-Instruct')\n\n# Initialize a reusable cache object\ncache = DynamicCache()\n\nmessages = []\n\ndef chat_turn(user_msg: str) -> str:\n  global cache, messages\n  messages.append({'role': 'user', 'content': user_msg})\n\n  inputs   = tokenizer.apply_chat_template(messages, return_tensors='pt').to('cuda')\n  outputs  = model.generate(\n    inputs,\n    past_key_values = cache,       # pass existing cache\n    max_new_tokens  = 256,\n    do_sample       = False,\n    use_cache       = True,\n  )\n\n  # Update cache for next turn\n  cache = outputs.past_key_values   # type: ignore\n\n  new_tokens = outputs.sequences[0][inputs.shape[1]:]\n  reply      = tokenizer.decode(new_tokens, skip_special_tokens=True)\n  messages.append({'role': 'assistant', 'content': reply})\n  return reply\n```\n\n---\n\n## Static cache for fixed system prompt\n\n```python\nfrom transformers import StaticCache\n\n# Pre-compute system prompt KV once\nsystem_prompt = tokenizer('You are a yamtam security agent.', return_tensors='pt').to('cuda')\n\nstatic_cache = StaticCache(\n  config           = model.config,\n  max_batch_size   = 8,\n  max_cache_len    = 4096,\n  device           = 'cuda',\n  dtype            = torch.float16,\n)\n\n# Run system prompt through model to fill static cache\nwith torch.no_grad():\n  model(**system_prompt, past_key_values=static_cache, use_cache=True)\n\n# Reuse this cache for ALL requests — system prompt never recomputed\n```\n\n---\n\n## Sliding window (long context without full KV growth)\n\n```python\n# SlidingWindowCache: only keep last W tokens in KV\nfrom transformers import SlidingWindowCache\n\ncache = SlidingWindowCache(\n  config         = model.config,\n  max_batch_size = 1,\n  max_cache_len  = 2048,   # window size W\n  device         = 'cuda',\n  dtype          = torch.float16,\n)\n# Older tokens evicted as new tokens arrive → constant memory\n```\n\n---\n\n## Anti-Fake-Pass Checklist\n\n```\n❌ Reusing DynamicCache across different prompts → KV state is prompt-specific; cross-contamination\n❌ use_cache=False accidentally → cache not populated, next turn re-computes everything\n❌ Static cache max_cache_len too small → cache overflow raises IndexError mid-generation\n❌ Not detaching cache tensors before serialization → huge graph kept alive, memory leak\n❌ SlidingWindowCache with model not trained for sliding window → incoherent output on long contexts\n❌ Batch size > StaticCache.max_batch_size → silent shape mismatch error\n```\n",{},"main","481dcd7dd2e288b9d627a2bd017db70e64e6c6fa",".claude\u002Fskills\u002Fkv-cache-optimization",2,"https:\u002F\u002Fgithub.com\u002FHoronomos-laps\u002FYana-AI","https:\u002F\u002Fyana.vutam.link\u002F","https:\u002F\u002Fgithub.com\u002FHoronomos-laps","https:\u002F\u002Fyana.vutam.link","An open-source governed runtime & execution environment for multi-agent AI systems.","2026-11-14 13:24:09",{"source":7,"skillName":5,"commands":34,"prompt":44,"zipAvailable":45},[35,38,41],{"tool":36,"command":37},"npx","npx skills add Horonomos-laps\u002FYana-AI --skill kv-cache-optimization",{"tool":39,"command":40},"bunx","bunx skills add Horonomos-laps\u002FYana-AI --skill kv-cache-optimization",{"tool":42,"command":43},"pnpm","pnpm dlx skills add Horonomos-laps\u002FYana-AI --skill kv-cache-optimization","安装技能，技能地址：https:\u002F\u002Fgithub.com\u002FHoronomos-laps\u002FYana-AI\u002Fblob\u002Fmain\u002F.claude\u002Fskills\u002Fkv-cache-optimization\u002FSKILL.md",true,[],[48,56,63,71,79,87],{"skillNo":49,"name":50,"nameZh":6,"description":51,"descriptionZh":6,"repositoryFullName":7,"ownerLogin":8,"ownerAvatarUrl":9,"ownerAccountNo":10,"ownerDisplayName":11,"repositoryNo":12,"starsCount":13,"licenseSpdxId":52,"licenseFileUrl":6,"sourceHtmlUrl":53,"categories":54,"keywords":55,"publishedAt":18},"01M735GKET5YVYKAY62W2P5YXB","openai--sentry--sentry","Use when the user asks to inspect Sentry issues or events, summarize recent production errors, or pull basic Sentry health data via the Sentry API; perform read-only queries with the bundled script and require `SENTRY_AUTH_TOKEN`.","MIT","https:\u002F\u002Fgithub.com\u002FHoronomos-laps\u002FYana-AI\u002Fblob\u002Fmain\u002F.claude\u002Fskills\u002Fopenai--sentry--sentry\u002FSKILL.md",[],[],{"skillNo":57,"name":58,"nameZh":6,"description":59,"descriptionZh":6,"repositoryFullName":7,"ownerLogin":8,"ownerAvatarUrl":9,"ownerAccountNo":10,"ownerDisplayName":11,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":60,"categories":61,"keywords":62,"publishedAt":18},"01M73DCAP83T17HY7SMGK7MGFE","ito-basket-compare","Compare Itô prediction-market baskets against a user's knowledge base, portfolio notes, financial context, watchlist, or research thesis. Use for read-only basket comparison and gap analysis without investment advice or live trading.","https:\u002F\u002Fgithub.com\u002FHoronomos-laps\u002FYana-AI\u002Fblob\u002Fmain\u002Fcore\u002Fskills\u002Fito-basket-compare\u002FSKILL.md",[],[],{"skillNo":64,"name":65,"nameZh":6,"description":66,"descriptionZh":6,"repositoryFullName":7,"ownerLogin":8,"ownerAvatarUrl":9,"ownerAccountNo":10,"ownerDisplayName":11,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":67,"sourceHtmlUrl":68,"categories":69,"keywords":70,"publishedAt":18},"01M734CYEMZFDR3KH6SW0JP8TD","implementing-network-traffic-baselining","Build network traffic baselines from NetFlow\u002FIPFIX data using Python pandas for statistical analysis, z-score anomaly detection, and hourly\u002Fdaily traffic pattern profiling","https:\u002F\u002Fgithub.com\u002Fyanacuti1121\u002FYana-AI\u002Fblob\u002Fmain\u002F.claude\u002Fskills\u002Fimplementing-network-traffic-baselining\u002FLICENSE","https:\u002F\u002Fgithub.com\u002FHoronomos-laps\u002FYana-AI\u002Fblob\u002Fmain\u002F.claude\u002Fskills\u002Fimplementing-network-traffic-baselining\u002FSKILL.md",[],[],{"skillNo":72,"name":73,"nameZh":6,"description":74,"descriptionZh":6,"repositoryFullName":7,"ownerLogin":8,"ownerAvatarUrl":9,"ownerAccountNo":10,"ownerDisplayName":11,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":75,"sourceHtmlUrl":76,"categories":77,"keywords":78,"publishedAt":18},"01M73ASZ6NJRJM44Z2P84NGQ3Q","detecting-evasion-techniques-in-endpoint-logs","Detects defense evasion techniques used by adversaries in endpoint logs including log tampering, timestomping, process injection, and security tool disabling. Use when investigating suspicious endpoint behavior, building detection rules for evasion tactics, or conducting threat hunting for stealthy adversary activity. Activates for requests involving evasion detection, defense evasion analysis, log tampering detection, or MITRE ATT&CK TA0005.","https:\u002F\u002Fgithub.com\u002Fyanacuti1121\u002FYana-AI\u002Fblob\u002Fmain\u002Fcore\u002Fskills\u002Fdetecting-evasion-techniques-in-endpoint-logs\u002FLICENSE","https:\u002F\u002Fgithub.com\u002FHoronomos-laps\u002FYana-AI\u002Fblob\u002Fmain\u002Fcore\u002Fskills\u002Fdetecting-evasion-techniques-in-endpoint-logs\u002FSKILL.md",[],[],{"skillNo":80,"name":81,"nameZh":6,"description":82,"descriptionZh":6,"repositoryFullName":7,"ownerLogin":8,"ownerAvatarUrl":9,"ownerAccountNo":10,"ownerDisplayName":11,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":83,"sourceHtmlUrl":84,"categories":85,"keywords":86,"publishedAt":18},"01M734AFNXD06Z78QRK0VKGT7Q","implementing-microsegmentation-with-guardicore","Implementing microsegmentation using Akamai Guardicore Segmentation to map application dependencies, create granular network policies, visualize east-west traffic flows, and enforce least-privilege communication between workloads across data centers and cloud.","https:\u002F\u002Fgithub.com\u002Fyanacuti1121\u002FYana-AI\u002Fblob\u002Fmain\u002F.claude\u002Fskills\u002Fimplementing-microsegmentation-with-guardicore\u002FLICENSE","https:\u002F\u002Fgithub.com\u002FHoronomos-laps\u002FYana-AI\u002Fblob\u002Fmain\u002F.claude\u002Fskills\u002Fimplementing-microsegmentation-with-guardicore\u002FSKILL.md",[],[],{"skillNo":88,"name":89,"nameZh":6,"description":6,"descriptionZh":6,"repositoryFullName":7,"ownerLogin":8,"ownerAvatarUrl":9,"ownerAccountNo":10,"ownerDisplayName":11,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":90,"categories":91,"keywords":92,"publishedAt":18},"01M736TK27QMCXK0DVCH25SJN5","ramdisk-tmpfs-patterns","https:\u002F\u002Fgithub.com\u002FHoronomos-laps\u002FYana-AI\u002Fblob\u002Fmain\u002F.claude\u002Fskills\u002Framdisk-tmpfs-patterns\u002FSKILL.md",[],[]]