Skip to content

[provider-anthropic] Support 1h prompt-cache TTL — verified working against the live API, no beta header required #337

Description

@Joi

Summary

provider-anthropic hardcodes cache_control: {"type": "ephemeral"} (5-minute TTL) at all three breakpoint sites (__init__.py:2136, :3719, :3745, verified on main 2026-07-20). There is no config to request the 1-hour TTL.

For human-paced interactive sessions, any gap >5 minutes between turns expires the cache and the next call re-writes the FULL context at cache-write rates. Observed: a single 282k-token cache-write event after one thinking pause (session data in the companion loop-streaming issue). With large system prompts (bundles + MCP tool schemas commonly reach 130k+ tokens base), these expiry re-writes dominate cost.

Verified against the live API (2026-07-20)

ttl: "1h" is accepted today with no beta header:

  • cache_control: {type: ephemeral, ttl: "1h"} -> HTTP 200
  • usage returns a per-TTL breakdown: cache_creation: {ephemeral_5m_input_tokens: 0, ephemeral_1h_input_tokens: 4419}

This also means the comment in _cost.py:131 ("Anthropic's usage object returns a single cache_creation_input_tokens count") is stale — the API now distinguishes tiers, so 1h writes (2x base input) can be cost-tracked correctly.

Suggested change

  1. New provider config key cache_ttl: "5m" | "1h" (default "5m", preserving current behavior), applied at all three breakpoint sites.
  2. _cost.py: read the cache_creation per-TTL breakdown and price 1h writes at 2x base input.

Economics: 1h writes cost 2x vs 1.25x, but for sessions with several >5min gaps per hour, converting full-context re-writes (1.25x each) into cache reads (0.1x) is a large net win.

Happy to submit the PR — small, config-gated, default-off.

Related

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