Skip to content

Prompt cache & tool tiers

promptCache marks the stable prefix:

  • Anthropic: cache_control: {type: "ephemeral"} after the tools, on the system prompt and on the latest message; longTTL adds ttl: "1h".
  • OpenAI: sends prompt_cache_key = key (use one key per session).
  • Gemini: nothing yet (explicit cachedContent is not built; implicit prefix caching still benefits from the stable prefix).

Usage then reports cacheReadTokens and cacheWriteTokens, so a drop in hits is visible.

toolTiers sends the core tools with full schemas, plus two meta tools:

  • tool_search {query, limit?} — its description lists every other tool (name and one line, or name only for deferred ones). select:a,b loads exact names; anything else is a keyword search. The loaded schemas come back as a tool result, so the tools array never changes.
  • call_tool {name, arguments} — calls a loaded tool. The loop unwraps it before hooks, so beforeTool permission gates and hints see the real tool, and streamed events carry the real name with via: "call_tool". The run result’s tool calls record the real tool and its inner arguments too; only the transcript keeps the call_tool wrapper.

An optional budget (budgetTokens, default 5% of contextWindow) moves the least-used core tools down a tier when the plan is built. The plan is then frozen for that toolkit until resetToolTiers (call it at session start); toolTierPlan shows it.

const agent = createClient({
baseUrl: "https://api.anthropic.com", style: "anthropic", model: "claude-sonnet-4-5",
promptCache: { longTTL: true },
toolTiers: { core: ["read", "grep", "edit"], deferred: ["deploy"], contextWindow: 200_000 },
})
agent.resetToolTiers(tk) // session start
const res = await agent.run("fix the test", { toolkit: tk })
console.log(agent.toolTierPlan(tk), res.usage?.cacheReadTokens)

Each port has a hermetic, runnable example against a mock provider that simulates the cache: js/examples/prompt_cache.ts, python/examples/prompt_cache_tool_tiers.py, golang/examples/promptcache, java/.../examples/PromptCacheTiers.java, csharp/examples/Toolnexus.Examples/Cache.cs (dotnet run -- cache), elixir/examples/prompt_cache_tiers.exs, and the Clojure example under clojure/examples/.

API pages with every field: prompt cache (JS · Python · Go · Java · C# · Elixir · Clojure) and tool tiers (JS · Python · Go · Java · C# · Elixir · Clojure).

  • Gemini explicit cachedContent (openspec/changes/add-prompt-cache, task 2.3).
  • A tool registered mid-session without tool tiers still changes the tools array, which breaks the cached prefix. With tiers it is deferred and found by tool_search.