이 스킬
Cache Strategy — prompt cache + dartlab cache key
Claude prompt cache + dartlab 자체 cache (BoundedCache / lru_cache / disk parquet) 전략 SSOT. cache hit 율 측정 + cache key 설계 표준.
이어 가기
절차
실행 순서
- 1
5 분 TTL (Anthropic 기본).
- 2
system prompt + tool definitions 고정 → cache breakpoint.
- 3
효과: ~90% cost 감축 (cached input).
- 4
Polars heap 가드 (CLAUDE.md 강행규칙).
- 5
per-Company 200~500MB → max 2 Company 동시 (사용자 메모리).
- 6
fixture scope `module` (test).
- 7
영구 저장 (`data/cache/*.parquet` 또는 HF dataset).
- 8
key: `<axis>_<target>_<period>.parquet`.
- 9
invalidation: schema 변경 시 manual delete.
- 10
cache hit 율 ≥ 80% (production).
- 11
size 한계 명시 (BoundedCache max_entries).
- 12
Claude API 측 `cache_creation_input_tokens` 측정 (실 cache 작동 확인).
3 cache layer
1. Claude prompt cache
- 5 분 TTL (Anthropic 기본).
- system prompt + tool definitions 고정 → cache breakpoint.
- 효과: ~90% cost 감축 (cached input).
2. dartlab BoundedCache (in-memory)
- Polars heap 가드 (CLAUDE.md 강행규칙).
- per-Company 200~500MB → max 2 Company 동시 (사용자 메모리).
- fixture scope
module(test).
3. Disk parquet cache
- 영구 저장 (
data/cache/*.parquet또는 HF dataset). - key:
<axis>_<target>_<period>.parquet. - invalidation: schema 변경 시 manual delete.
cache key 설계
def cache_key(axis, target, period, **kwargs):
"""axis-target-period + sorted kwargs hash"""
base = f"{axis}_{target}_{period}"
extra = "_".join(f"{k}={v}" for k, v in sorted(kwargs.items()))
return f"{base}_{hash(extra)}" 측정
from dartlab.cache.metrics import getCacheStats
print(getCacheStats())
# {"hits": 1234, "misses": 56, "hit_rate": 0.957, "size_mb": 234} 강행 룰
- 모든 새 cache →
BoundedCache사용 (heap 가드). - Claude prompt cache breakpoint → system prompt 변경 최소화.
- disk cache → schema 변경 시 invalidation 절차 박힘.
기본 검증
- cache hit 율 ≥ 80% (production).
- size 한계 명시 (BoundedCache max_entries).
- Claude API 측
cache_creation_input_tokens측정 (실 cache 작동 확인).
런타임
실행 환경별 호환성
| 환경 | 상태 | 비고 / 제한 |
|---|---|---|
| Local Python | supported | — |
| Server | supported | — |
| MCP | supported | — |
| Web AI | supported | — |
| Pyodide | limited | — |