fix(agent): enable prompt caching for the Anthropic CUA system prompt#2304
fix(agent): enable prompt caching for the Anthropic CUA system prompt#2304aditya-silna wants to merge 2 commits into
Conversation
Send the system prompt as a content block array with cache_control: ephemeral instead of a plain string, so the tools + system prefix is cached across agent steps. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
This PR is from an external contributor and must be approved by a stagehand team member with write access before CI can run. |
|
There was a problem hiding this comment.
1 issue found across 1 file
Confidence score: 4/5
- In
packages/core/lib/v3/agent/AnthropicCUAClient.ts, the newsystemcontent-block payload (includingcache_control) is currently untested, so a malformed request shape could slip through and cause Anthropic API call failures at runtime. Add a unit test around the mockedgetActionSDK request assertions to verify this exact payload before merging.
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="packages/core/lib/v3/agent/AnthropicCUAClient.ts">
<violation number="1" location="packages/core/lib/v3/agent/AnthropicCUAClient.ts:560">
P2: The new `system` content-block array with `cache_control` is not verified by any unit test, even though the codebase already mocks `getAction`'s Anthropic SDK call and asserts on request parameters. Because the API call is typed as `Record<string, unknown>` and uses `@ts-expect-error`, a regression in this shape would compile cleanly but fail at Anthropic at runtime. Adding a focused test that instantiates `AnthropicCUAClient` with non-empty `userProvidedInstructions` and asserts on the `system` field passed to `mockCreate` would be straightforward given the existing test infrastructure.</violation>
</file>
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
| // tools + system prefix is cached across agent steps | ||
| if (this.userProvidedInstructions) { | ||
| requestParams.system = this.userProvidedInstructions; | ||
| requestParams.system = [ |
There was a problem hiding this comment.
P2: The new system content-block array with cache_control is not verified by any unit test, even though the codebase already mocks getAction's Anthropic SDK call and asserts on request parameters. Because the API call is typed as Record<string, unknown> and uses @ts-expect-error, a regression in this shape would compile cleanly but fail at Anthropic at runtime. Adding a focused test that instantiates AnthropicCUAClient with non-empty userProvidedInstructions and asserts on the system field passed to mockCreate would be straightforward given the existing test infrastructure.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At packages/core/lib/v3/agent/AnthropicCUAClient.ts, line 560:
<comment>The new `system` content-block array with `cache_control` is not verified by any unit test, even though the codebase already mocks `getAction`'s Anthropic SDK call and asserts on request parameters. Because the API call is typed as `Record<string, unknown>` and uses `@ts-expect-error`, a regression in this shape would compile cleanly but fail at Anthropic at runtime. Adding a focused test that instantiates `AnthropicCUAClient` with non-empty `userProvidedInstructions` and asserts on the `system` field passed to `mockCreate` would be straightforward given the existing test infrastructure.</comment>
<file context>
@@ -554,9 +554,16 @@ export class AnthropicCUAClient extends AgentClient {
+ // tools + system prefix is cached across agent steps
if (this.userProvidedInstructions) {
- requestParams.system = this.userProvidedInstructions;
+ requestParams.system = [
+ {
+ type: "text",
</file context>
…lient Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Why
In
AnthropicCUAClient.getAction(), the system prompt was sent as a plain string:A CUA agent loop re-sends the identical tools + system prefix on every step, so without a cache breakpoint the full prefix is re-processed at full input price each step.
What changed
The system prompt is now sent as a content block array with
cache_control:Since the Anthropic API renders the prompt as
tools→system→messages, this single breakpoint caches the computer-use tool definition and the system prompt together across steps. Cache reads bill at ~0.1× input price (writes at 1.25×), so this pays for itself from the second step of any run. If the prefix is below the model's minimum cacheable size, the marker is silently ignored — no error, no premium.Caching is transparent to model behavior; outputs are unchanged.
Testing
tsc --noEmitonpackages/corepasses.🤖 Generated with Claude Code
Summary by cubic
Enable prompt caching for Anthropic CUA by sending the system prompt as a content block array with
cache_control: { type: "ephemeral" }in getAction(). This caches the tools + system prefix across agent steps to cut input cost and latency, and adds a unit test to verify thesystempayload shape (cached array when instructions exist, omitted otherwise).Written for commit b6feabf. Summary will update on new commits.