Skip to content

fix(anthropic): reuse shared system prompt across sessions - #196471

Merged
vincentkoc merged 1 commit into
mainfrom
fix/pr-196461-anthropic-cache
Nov 10, 2026
Merged

vincentkoc merged 1 commit into
mainfrom
fix/pr-196461-anthropic-cache

Conversation

@vincentkoc

@vincentkoc vincentkoc commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

What Problem This Solves

Fixes: starting a new Anthropic session rewrites agent-shared system instructions to the prompt cache when only the Runtime facts differ.

Related: #196461

User Impact

User impact: short sessions for the same agent can reuse the shared system text within Anthropic's cache lifetime, reducing cache writes. Other providers are unaffected. The opening request uses one history checkpoint; later requests retain two.

Why This Change Was Made

This carries Scott Fan's proposed Anthropic transport checkpoint into an editable maintainer branch. The source PR cannot be edited by maintainers, and its split removed the newline before Runtime facts. This version preserves that newline in the session-owned block and verifies both prompt text and a shared prefix across two sessions. Contributor credit is retained in the commit.

Evidence

  • Node 24.22.1 provider test: 45 passed (2.35 s Vitest, 4.27 s wrapper). The provider regression checks opening/later checkpoint allocation, contiguous dynamic prompt text, and identical shared blocks with distinct Runtime facts across two sessions.
  • Anthropic policy and cache parity tests: 47 passed across three files (1.19 s Vitest, 2.37 s wrapper).
  • git diff --check, oxfmt, pre-commit freshness/line-cap hook, and independent P1 autoreview: passed.
  • Exact-target CI run: https://github.com/openclaw/openclaw/actions/runs/42765462095 (in progress at this update).
  • Direct Anthropic API cost measurement was reported on the source PR; this replacement has not repeated a live authenticated cost trial.

@clawsweeper

clawsweeper Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@vincentkoc vincentkoc self-assigned this Nov 10, 2026
@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. P2 Normal backlog priority with limited blast radius. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Nov 10, 2026
@clawsweeper

clawsweeper Bot commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

This PR adds an opening-request cache checkpoint before session-specific Runtime facts.

Example: Sessions alpha and beta send identical agent instructions within the cache lifetime.

  • Before: The dynamic system block includes session=alpha or session=beta, preventing reuse of that entire block across sessions.
  • After: The opening request checkpoints the shared instructions separately from Runtime facts; follow-up requests retain the existing history-checkpoint allocation.

Review scores

Measure Result What it means
Overall readiness 🦐 gold shrimp (3/6) Useful, focused implementation with meaningful payload coverage and supporting API measurements; branch-specific end-to-end evidence remains limited.
Proof confidence 🦐 gold shrimp (3/6) This maintainer-authored PR is outside the contributor proof gate; patch-specific evidence captures production-built provider payloads before network I/O, and the linked direct-API trial supports the mechanism without measuring this replacement through a running agent.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: Starting many short Anthropic sessions repeatedly pays cache-write costs for otherwise identical agent instructions.
Reason: The maintainer-authored direction addresses measured cache waste while preserving later history checkpoints and existing configuration.

Merge readiness

✅ Ready for maintainer review

Keep open: this maintainer-authored replacement addresses avoidable Anthropic cache writes still present on main; no concrete correctness or security defect was found.

Priority: P2
Reviewed head: 2c28f16d43cde9c4b4ee7bdba9ed37e70220978e

Before merge

None.

Findings

None.

Tests

  • Missing end-to-end proof: No branch-built agent measurement covers two fresh sessions and a same-session follow-up; the source PR reports its regression red on main, while the replacement reports passing focused suites.
Agent review details

How this fits together

The shared Anthropic payload policy converts prepared system prompts, tools, and conversation history into provider request blocks and allocates their cache checkpoints.

flowchart LR
  A[Agent system prompt] --> B[Prepared prompt revision]
  B --> C[Anthropic system block policy]
  D[Tools and conversation history] --> E[Checkpoint allocation]
  C --> E
  E --> F[Anthropic request]
Loading

Technical review

Best possible solution:

Keep the focused checkpoint change in the existing Anthropic policy owner and land this credited replacement when its validation is settled.

Do we have a high-confidence way to reproduce the issue?

Current main visibly combines shared dynamic instructions with session-specific Runtime facts; the linked source PR supplies released-build observations and a direct-API mechanism check, but this review executed no target code.

Is this the best way to solve the issue?

The change uses the existing boundary parser, prepared-prompt lifetime, and checkpoint allocator; it avoids moving instructions between roles or adding a competing cache owner.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 46589542ffb6.

Provenance checked

Testing

Proof path: in-process harness.

Security

None.

Evidence

What I checked:

Likely related people:

  • Vincent Koc: Raw commit 64f2890 adds src/agents/anthropic-payload-policy.ts:78 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 64f28906de09; files: src/agents/anthropic-payload-policy.ts)
  • Peter Steinberger: Raw commit f1c9357 adds packages/ai/src/transports/anthropic-payload-policy.ts:213 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: f1c935770852; files: packages/ai/src/transports/anthropic-payload-policy.ts)
  • georg wiltschek: Raw commit 5ae9013 adds packages/ai/src/transports/anthropic-payload-policy.ts:274 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 5ae901326b85; files: packages/ai/src/transports/anthropic-payload-policy.ts)

PR surface

Source +42, Tests 0. Total +42 across 2 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 1 59 17 +42
Tests 1 27 27 0
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 86 44 +42

Preflight source

Source unknown. Source comparison at the observed PR snapshot only. Completion is self-reported; body, policy, tests, execution, and report bytes are not verified.

Review metrics

Metric Value Why it matters
Production versus test LOC Production +42; tests net 0 Production growth implements the additional split and carries eligibility through existing editable copies; regression coverage replaces an existing case without new scaffolding.

Labels

Label changes:

  • add P2: Repeated short Anthropic sessions incur avoidable cache-write costs without breaking message delivery or runtime availability.
  • add rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦐 gold shrimp and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.

Label justifications:

  • P2: Repeated short Anthropic sessions incur avoidable cache-write costs without breaking message delivery or runtime availability.
  • rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦐 gold shrimp and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.

Rank-up moves

Optional improvements that raise the rating; they are not merge blockers.

  • Provide branch-built agent usage and request evidence for two fresh sessions and a same-session follow-up.

Rating scale

6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

Reviewed November 10, 2026, 1:24 AM ET / 06:24 UTC.

@vincentkoc
vincentkoc marked this pull request as ready for review November 10, 2026 06:27
@vincentkoc

Copy link
Copy Markdown
Member Author

Exact-head proof update for 2c28f16d43cde9c4b4ee7bdba9ed37e70220978e:

ClawSweeper's optional rank-up move asks for branch-built agent usage evidence across two fresh sessions and a follow-up. The source PR reports a live Anthropic API trial of the same checkpoint mechanism, while this replacement verifies exact outbound blocks, prompt-text continuity, cross-session shared-prefix identity, and follow-up allocation in its provider test. I am skipping a second authenticated cost experiment for this replacement. Its branch-built cost savings remain unmeasured; the external API cache mechanism and branch-built request shape are separately evidenced.

@vincentkoc
vincentkoc merged commit e952893 into main Nov 10, 2026
186 checks passed
@vincentkoc
vincentkoc deleted the fix/pr-196461-anthropic-cache branch November 10, 2026 06:41
@vincentkoc

Copy link
Copy Markdown
Member Author

Merged via squash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P2 Normal backlog priority with limited blast radius. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. size: S status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant