C.W.K.
Stream
Lesson 02 of 06 · published

깊이 있는 prompt caching 전략

~16 min · prompt-caching, breakpoints, ttl

Level 0Observer
0 XP0/64 lessons0/13 achievements
0/150 XP to next level150 XP to go0% complete

Breakpoint 넷, 현명하게

요청당 cache_control: ephemeral breakpoint 4개까지. 클래식 분리 — (1) 시스템 프롬프트 persona/policy, (2) tools 리스트, (3) static 문서(이 세션엔 안 변하는 RAG 컨텍스트), (4) 초기 대화 history. 각 breakpoint가 다음에 가장 자주 변하는 거 자르는 곳.

TTL과 5분 윈도우

Ephemeral cache 디폴트 TTL 5분. 호출이 bursty면 충분. 호출 sparse면 read보다 write 더 자주 — caching 도움 주장 전 측정.

순서가 사람들 생각보다 중요

Cache가 안정 prefix 보상. Assistant 마지막 응답이 같은 길이지만 살짝 reorder된 텍스트면 cache hit 잃음. 프롬프트 구성을 deterministic으로 다뤄 — 같은 input이면 same byte-for-byte prefix out.

원칙: 안정 prefix cache. 프롬프트 앞을 의도적으로 boring하게. Cache hit을 usage telemetry로 verify, 믿음 X.

Code

4-breakpoint 레이아웃·python
resp = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    system=[
        {"type": "text", "text": PERSONA, "cache_control": {"type": "ephemeral"}},  # bp 1
    ],
    tools=[
        # ... 마지막 도구가 bp 2
        {**LAST_TOOL_SPEC, "cache_control": {"type": "ephemeral"}},
    ],
    messages=[
        {"role": "user", "content": [
            {"type": "text", "text": SESSION_DOCUMENT, "cache_control": {"type": "ephemeral"}},  # bp 3
        ]},
        # ... 옛 history
        {"role": "assistant", "content": [
            {"type": "text", "text": LAST_STABLE_ASSISTANT, "cache_control": {"type": "ephemeral"}},  # bp 4
        ]},
        # 가장 새로운 user 턴 (uncached, dynamic)
        {"role": "user", "content": current_user_text},
    ],
)
프로덕션에서 cache hit ratio verify·python
def cache_hit_ratio(usage) -> float:
    total_input = usage.input_tokens + usage.cache_creation_input_tokens + usage.cache_read_input_tokens
    if total_input == 0:
        return 0.0
    return usage.cache_read_input_tokens / total_input

# 호출 사이 aggregate; ratio 떨어지면 alert
# (예: 배포가 페르소나 파일 바꿔서 cache prefix 깸).

External links

Exercise

프로덕션 엔드포인트 하나에 cache_hit_ratio 메트릭 추가. 일주일 추적. Cacheable 워크로드에서 50% 아래 떨어지면 prefix 변화 찾아 fix.
Hint
대부분 cache-ratio 떨어짐은 최근 배포와 correlate — 메트릭 떨어진 날의 페르소나/시스템 파일 git log 봐.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.