Vì sao lần gọi thứ hai lại rẻ hơn?
Prompt caching (bộ nhớ đệm prompt) · Thực hành · 5 phút · tokens, llm · học sau Token
Prompt caching dùng lại phần việc đã làm trên đoạn mở đầu của prompt khi lần gọi sau bắt đầu bằng đúng văn bản đó. Token trong cache tính giá thấp hơn nhiều so với input thường, nhưng chỉ khi đoạn đầu đủ dài và được gửi lại trong vài phút.
1 Đoán trước
2 Xem
The same start, billed once
1 / 5call 1system prompt + 20-page doc · 10,000 tokensQ: summary? · 50call 2system prompt + 20-page doc · 10,000 tokensQ: risks? · 50Two calls about the same document. Only the question at the end differs.
token counts illustrative
Two calls about the same long document. Only the question at the end changes.
Chỉ đoạn mở đầu y hệt mới được cache: ghi một lần với giá nhỉnh hơn, rồi đọc lại với giá khoảng một phần mười, nếu lần gọi sau đến sớm và prefix đủ dài.
Prefix là đoạn mở đầu của prompt: tool, system prompt, tài liệu. Nhà cung cấp giữ phần việc trên một prefix dài trong vài phút. Nếu lần gọi sau bắt đầu bằng đúng văn bản đó, các token ấy được tính là cached input (input trong cache). Vậy hãy đặt phần không bao giờ đổi lên đầu, câu hỏi xuống cuối.
4 Bạn là model
Bạn là cache. Với mỗi lần gọi, hãy quyết định: hit (trúng) hay miss (trượt)?
call 1 of 5 · Claude Sonnet 4.6, default cache
Same 10,000-token document, new question at the end, 2 minutes later
Anthropic prompt-caching docs (checked 2026-10-21)
You are the cache: hit or miss?5 Kiểm tra
6 Nói lại bằng lời bạn
7 Nhiệm vụ · 5 phút
Với một API key Gemini miễn phí, gửi cho Gemini 3.8 Flash cùng một tài liệu dài (hơn 4.096 token) kèm hai câu hỏi khác nhau ở cuối, cách nhau một phút. Đọc usage.total_cached_tokens trong cả hai response. Rồi chuyển một câu từ cuối lên đầu tài liệu và thử lại.
Ba câu nhanh
Rút ngẫu nhiên từ bộ câu hỏi của bài; lần sau sẽ khác.
Xong chưa?
Nguồn
- https://platform.claude.com/docs/en/build-with-claude/prompt-caching (checked 2026-10-21; no update date): 5-minute cache writes 1.25×, 1-hour 2×, reads 0.1× base input (0.05× on Opus/Sonnet 5.5, 0.025× on Fable/Mythos 5.1); default lifetime 5 minutes, refreshed on use; minimum 1,024 tokens on Claude Sonnet 4.6 (512–4,096 by model); “Cache hits require 100% identical prompt segments”; usage cache_read_input_tokens / cache_creation_input_tokens
- https://developers.openai.com/api/docs/guides/prompt-caching (checked 2026-10-21; no update date): on by default; minimum 1,024 tokens on GPT-5.6 and later; reads 0.1×, writes 1.25× (30-minute default); usage.input_tokens_details.cached_tokens
- https://ai.google.dev/gemini-api/docs/caching (updated 2026-10-13, checked 2026-10-21): implicit caching on by default for Gemini 2.5 and newer; minimum 4,096 tokens on Gemini 3.8 Flash; hits in usage.total_cached_tokens; the page does not say whether the free tier gets cache hits
- https://ai.google.dev/gemini-api/docs/pricing (updated 2026-10-19, checked 2026-10-21): Gemini 3.8 Flash input free of charge on the free tier
- Token counts (10,000 + 50) and the 1-unit base price in the picture are illustrative; the bills are arithmetic on Anthropic’s multipliers
Đăng Oct 21, 2026 · cập nhật Oct 21, 2026