逐日AI

面试题库

共 328 题,当前筛选 1 题。

Claude 高效使用:从对话到 Claude Code

D2 长文档、多模态与 API 初见:大上下文怎么用、prompt caching 省钱、PDF 与图片输入、带引用回答;Messages API 最小调用

  • prompt caching 省在哪?什么情况下反而不省?线上发现缓存命中率是零,你怎么排查?Where does prompt caching save money, when does it cost more, and how do you debug a zero cache-hit rate in production?
    国内高频海外高频进阶#prompt-caching#cost

    分析过程 · 先想清楚再作答

    1. 三问对应三层:原理、边界、排查。只答第一层是背文档,第三层才体现有没有真的上过线。
    2. 原理一句话:缓存匹配的是请求开头到 cache_control 标记为止的精确前缀(顺序是工具、system、messages),命中时这段只收正常输入价的 0.1 倍;代价是写入那一次收 1.25 倍(1 小时档 2 倍)。
    3. 不省的情况由此推出:同一前缀只用一次(多付 25%);前缀里有每次都变的内容(时间戳、随机 id、未排序 JSON、用户名),导致每次都在写永远用不上的缓存;前缀短于最小门槛(主力模型 1024 token,Haiku 4.5 是 4096)根本不会缓存;两次请求间隔超过 TTL。
    4. 排查清单按发生概率排:一看 system 或工具定义开头有没有动态内容;二看两次请求的模型 id 是否一致;三看前缀长度是否过门槛;四看间隔是否超 5 分钟;五看工具列表顺序是否稳定。判据只有一个字段:usage.cache_read_input_tokens 是否大于 0。
    5. 可预期的追问:断点应该打在哪?不变的末尾——工具定义末尾、system 末尾、长文档末尾、多轮对话倒数第二条消息,最多四个;打在每轮都变的内容上等于白写。

    How to reason about it · think before answering

    1. Three questions, three layers: mechanism, boundaries, debugging. The third layer is what shows production experience.
    2. Mechanism: the cache matches the exact byte prefix from the start of the request to the cache_control marker (tools, then system, then messages). A hit bills that prefix at 0.1x input price; the write costs 1.25x (2x for the one-hour TTL).
    3. When it costs more: a prefix used only once (+25%); volatile content inside the prefix — timestamps, random ids, unsorted JSON, user names — so every call writes a cache nothing will read; a prefix below the minimum (1024 tokens on current flagship models, 4096 on Haiku 4.5) that silently never caches; requests spaced beyond the TTL.
    4. Debug order by likelihood: dynamic content at the head of system or tool definitions; model id mismatch between calls; prefix under the minimum; gap over five minutes; unstable tool ordering. The single signal is usage.cache_read_input_tokens greater than zero.
    5. Follow-up: where do breakpoints go? At the end of stable sections — tools, system, the long document, the second-to-last message in a multi-turn chat — at most four; a breakpoint on per-turn content is a wasted write.

    答题要点

    • 匹配精确前缀(工具 → system → messages 到标记为止);命中 0.1 倍,写入 1.25 倍
    • 不省:前缀只用一次、前缀含动态内容、前缀短于最小门槛、间隔超过 TTL
    • 排查:动态内容、模型不一致、长度不够、间隔太久、工具顺序变了;看 cache_read_input_tokens
    • 断点打在不变部分的末尾,最多四个

    Key points

    • Matches the exact prefix (tools → system → messages up to the marker); hits bill 0.1x, writes 1.25x
    • Costs more when the prefix is used once, contains volatile content, is under the minimum length, or requests exceed the TTL
    • Debug: dynamic content, model mismatch, length, gap, tool ordering; verify via cache_read_input_tokens
    • Place breakpoints at the end of stable sections, at most four