Skip to content
Docs ·Prompt caching

Prompt caching

Reuse long prompts at a lower cost

It becomes cheaper when the same front part is sent again.

If you need to send a long document or a long instruction, it’s better to use a cache. The price at which the text is recalculated is much lower than the initial price.

ModelsMethods
ClaudeWe apply cache_control to the places where caching is necessary. When we first use it, the input price is slightly higher than the actual price.
GPTIt doesn’t need to be sewn together; it’ll be found by itself.
messages=[
    {"role": "system", "content": [
        {"type": "text", "text": long_document, "cache_control": {"type": "ephemeral"}},
    ]},
    {"role": "user", "content": "Summarize this document"},
]
  • The token that was read from the cache is usage.prompt_tokens_details.cached_tokens, while the token that was written to the cache is listed under cache_write_tokens.
  • The cache prices are available for each model under Model Screen.
  • Claude’s cache lasts for 5 minutes. With the Messages API, it’s possible to use a cache that lasts for 1 hour. When using this feature, the cost is twice the input price.