+
Skip to content

Prompt caching token count discrepancy between turns #155

@ajaypillay

Description

@ajaypillay

Hi, with reference to Example 2 in https://github.com/anthropics/anthropic-cookbook/blob/main/misc/prompt_caching.ipynb:

Turn 1:
...
User input tokens: 4
Output tokens: 22
Input tokens (cache read): 0
Input tokens (cache write): 187354
0.0% of input prompt cached (4 tokens)
Time taken: 20.37 seconds

Turn 2:
...
User input tokens: 4
Output tokens: 297
Input tokens (cache read): 187354
Input tokens (cache write): 36
100.0% of input prompt cached (187358 tokens)
Time taken: 7.53 seconds

Turn 3:
...
User input tokens: 4
Output tokens: 289
Input tokens (cache read): 187390
Input tokens (cache write): 308
100.0% of input prompt cached (187394 tokens)
Time taken: 6.76 seconds

I was under the impression that the number of tokens being written to the cache on Turn N would be the sum of input and output tokens from Turn N-1. But we see a difference of +10 and +7 tokens respectively on Turn 2 and 3. My understanding is that it's due to some extra metadata being cached, but I couldn't find any specifics in the Anthropic docs or anywhere else online. Claude 4 Sonnet seems to agree that this is the case:

https://bench.io/share/artifact/5SaWYiuBgxwwKVY7R5ZC8t (direct from Sonnet through the API)

The discrepancy is not a huge deal, but I'm mainly hoping to get some clarity on how large this discrepancy should be and any insight on what specifically contributes to this. Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载