jackwener/maka-agent

feat(runtime): enable Anthropic automatic prompt caching

Geschlossen

#1.380 geöffnet am 23.07.2026

 (1 Kommentar) (0 Reaktionen) (1 zugewiesene Person)TypeScript (0 Forks)github user discovery
enhancementgood first issue

Repository-Metriken

Stars
 (1 Stern)
PR-Merge-Metriken
 (PR-Metriken ausstehend)

Beschreibung

Summary

Enable Anthropic's top-level automatic prompt caching for native Anthropic API requests.

Maka already records cache reads, cache writes, and cache misses, but standard Anthropic requests do not currently enable prompt caching. The installed @ai-sdk/anthropic version supports the current API shape:

providerOptions: {
  anthropic: {
    cacheControl: { type: 'ephemeral' },
  },
}

The provider places the cache breakpoint at the last cacheable block and advances it as the conversation grows. The default five-minute lifetime covers the repeated requests in a normal agent loop without requiring Maka to place or maintain explicit breakpoints.

Current behavior

buildProviderOptions() in packages/runtime/src/model-factory.ts sets Anthropic thinking and effort options, but it does not set cacheControl for normal Anthropic API-key connections.

Maka already normalizes and records:

  • cache read input tokens;
  • cache write input tokens;
  • cache misses and their source;
  • the resulting usage cost.

The Claude subscription cloak is different. packages/runtime/src/subscription-cloaked-request.ts already places an explicit cache marker on the required Claude Code system prefix. That route should keep its existing request contract rather than mixing automatic and explicit cache modes in this change.

Proposed change

For connections where providerType === 'anthropic', merge the following into the existing Anthropic provider options:

cacheControl: { type: 'ephemeral' }

This condition is based on the provider identity, not on whether another provider uses an Anthropic-compatible protocol. The current shared switch branch must be split or guarded accordingly.

Use Anthropic's default five-minute TTL. Do not add a user setting or model-specific policy.

Protocol-compatible providers such as MiniMax and Kimi Coding Plan are not Anthropic API routes. They must not inherit this option without provider-specific evidence that their wire accepts it.

Acceptance criteria

  1. Prepared requests for native Anthropic API-key connections contain top-level cache_control: { type: "ephemeral" } on the Anthropic Messages wire.
  2. Existing thinking and effort options are preserved.
  3. Claude subscription requests keep their existing explicit prefix cache marker and request shape.
  4. MiniMax, Kimi Coding Plan, OpenAI, Google, and other providers receive no new cache-control field from this change.
  5. Cache read and cache write usage continues to flow through the existing telemetry and cost records.
  6. Tests cover the prepared provider request, not only the intermediate providerOptions object.

Explicitly not included

  • explicit tool, system, or message breakpoints;
  • one-hour cache TTL;
  • a prompt-cache settings surface;
  • changes to system prompt assembly;
  • provider-loop ownership changes.

Reference

Anthropic prompt caching

概述

为原生 Anthropic API 请求默认开启顶层自动提示词缓存。

Maka 已经会记录缓存读取、写入和未命中,但普通 Anthropic 请求还没有真正打开缓存。当前安装的 @ai-sdk/anthropic 已支持下面的配置:

providerOptions: {
  anthropic: {
    cacheControl: { type: 'ephemeral' },
  },
}

Anthropic 会自动把缓存点放到最后一个可缓存 block,并随着对话增长向后移动。默认五分钟足以覆盖 agent loop 里的连续请求,不需要 Maka 自己维护 tool、system、message 三套断点。

现状

packages/runtime/src/model-factory.ts 里的 buildProviderOptions() 已经负责 Anthropic 的 thinking 和 effort 配置,但普通 API key 连接没有设置 cacheControl

缓存用量的后半段已经齐全,Maka 会归一化并记录:

  • cache read input tokens;
  • cache write input tokens;
  • cache miss 及其来源;
  • 对应的使用成本。

Claude subscription 是一条特殊路径。packages/runtime/src/subscription-cloaked-request.ts 已在 Claude Code 要求的 system prefix 上放置显式 cache marker。本 issue 不混用自动缓存与显式断点,保持这条请求契约不变。

改动

只对原生 anthropic connection 合并:

cacheControl: { type: 'ephemeral' }

使用官方默认的五分钟 TTL,不增加用户设置,也不按模型维护策略表。

MiniMax、Kimi Coding Plan 虽然走 Anthropic 兼容协议,但不是 Anthropic API。没有各自的协议证据前,不应顺带下发这个字段。

验收条件

  1. 原生 Anthropic API key 请求在最终 Messages wire 上带有顶层 cache_control: { type: "ephemeral" }
  2. 现有 thinking 和 effort 配置不受影响。
  3. Claude subscription 继续使用现有的显式 prefix cache marker,请求形状不变。
  4. MiniMax、Kimi Coding Plan、OpenAI、Google 等 provider 不会因为本改动收到新的 cache-control 字段。
  5. cache read、cache write 继续进入现有 usage、telemetry 和 cost 记录。
  6. 测试断言最终 prepared request,而不只检查中间的 providerOptions 对象。

明确不做

  • tool、system、message 显式断点;
  • 一小时 TTL;
  • prompt cache 设置项;
  • system prompt 组装改造;
  • provider loop ownership 改造。

参考

Anthropic prompt caching

Contributor Guide