Skip to content

Support CachePoint for OpenRouter models (Anthropic/Gemini prompt caching) #4392

Description

@Adversarian

Description

OpenRouter supports explicit prompt caching via cache_control breakpoints for Anthropic and Gemini models (docs). However, OpenRouterChatModel currently inherits the OpenAI base behavior which silently drops CachePoint items:

# In OpenAIChatModel._map_content_item:
elif isinstance(item, CachePoint):
    # OpenAI doesn't support prompt caching via CachePoint, so we filter it out
    return None

Since OpenRouterChatModel doesn't override _map_content_item, any CachePoint passed in messages is silently ignored — even when the underlying model (e.g. Anthropic via OpenRouter) supports it.

Expected behavior

OpenRouterChatModel should translate CachePoint into the cache_control format that OpenRouter expects, e.g.:

{"type": "text", "text": "...", "cache_control": {"type": "ephemeral"}}

This would allow users to benefit from cached token pricing (0.25x input cost) when using Anthropic or Gemini models through OpenRouter.

References


I'm willing to submit a PR for this myself if the proposal is accepted.

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureNew feature request, or PR implementing a feature (enhancement)openrouter

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions