The widget agent switches tool_choice from auto to none on its last turns, after the tools have run.
The Anthropic caching doc says a tool_choice change invalidates the cached messages, so that turn reads none of the conversation from cache and writes it again.
- What to change
- Keep tool_choice at auto for the whole request. The loop already rejects any tool call made while finalizing (ais-relay.cjs:16266) and sends a 'Tools are now disabled' message, so enforcement does not need the switch.
- Why it matters
- On the turn after the switch, the system prompt and tools still hit the cache but the search and page results, history and any modified HTML are processed again at the cache-write price instead of the cache-read price. That prefix can be 5k to 40k tokens.
- How often it happens
- The code shows the problem; how often it happens is assumed. To measure it: On a widget request that reaches a tool_choice none turn, whether usage.cacheRead drops to the system and tools prefix only; the request already logs cacheWrite and cacheRead per request at ais-relay.cjs:16134.
- Estimate
- about 10,000 prompt tokens per widget request that reaches a tool_choice none turn, read at the write price instead of the read price (estimate)
What the estimate assumes
one affected turn per request; tool results are capped at 20,000 characters each (ais-relay.cjs:15440), up to 3 or 4 results, plus up to 12 history lines of 500 characters; requests that finish before the tool budget or the last two turns never switch; widget requests are a small share of the app's prompt tokens, so share is 0.01 (estimate).