Skip to content

perf(prompt-caching): cache tool schemas without history loss - #37611

Closed
rodboev wants to merge 1 commit into
NousResearch:mainfrom
rodboev:pr/tool-schema-caching
Closed

perf(prompt-caching): cache tool schemas without history loss#37611
rodboev wants to merge 1 commit into
NousResearch:mainfrom
rodboev:pr/tool-schema-caching

Conversation

@rodboev

@rodboev rodboev commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

Closes #20880

Summary

  • Rebuild tool-schema cache placement as a request-local plan that allocates at most four combined tool and message breakpoints.
  • Send a tool marker only to direct native Anthropic Messages requests, while preserving current message-cache behavior and plain tools for third-party and envelope routes.
  • Replan both sections for normal retry, persistent MoA, and synchronous fallback destinations; configured fallbacks preserve their resolved provider, base URL, API mode, and model.

Validation

  • 228 passed in 12.17s: cache plan, synchronous fallback, failover, MoA, policy, normal kwargs, and continuation compatibility tests.
  • 28 passed in 1.16s: direct and third-party Anthropic-wire fallback payloads, persistent MoA, and cache policy boundaries.
  • Invariant enumeration and proof-matrix validation pass.

Keeps the four-breakpoint budget from #27170 and incorporates @tbanetwork's current breakpoint-budget report.

@alt-glitch alt-glitch added type/perf Performance improvement or optimization comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint provider/anthropic Anthropic native Messages API P3 Low — cosmetic, nice to have labels Jun 2, 2026
@rodboev
rodboev force-pushed the pr/tool-schema-caching branch from 28790e9 to a0323b6 Compare June 2, 2026 20:51
@rodboev
rodboev force-pushed the pr/tool-schema-caching branch 3 times, most recently from 052bff1 to f5ddc91 Compare July 1, 2026 14:53

@teknium1 teknium1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for targeting a real prompt-cost gap. Current main still caches only api_messages at agent/conversation_loop.py:894-899, while agent/chat_completion_helpers.py:777-792 forwards agent.tools to the request.

Problems

  • The current PR commit (f5ddc911e10c) does not wire the new helper into that path: its only conversation_loop.py change is an import, and its chat_completion_helpers.py change only reads agent._tools_for_api. The complete PR diff contains no assignment to that attribute, so agent.tools remains the transmitted list without a marker.
  • The new tests cover helper output but not request construction. They would not detect this missing invocation.
  • The PR conflicts with main, and its prompt_caching.py hunk predates current _can_carry_marker filtering at agent/prompt_caching.py:109-117; salvage should retain that newer protection.

Suggested changes

  • Apply tool marking to a request-local tool list in the actual request-preparation path, reserve a breakpoint only when that list is sent, and add native-Anthropic plus OpenAI-compatible request-kwargs coverage.

Automated hermes-sweeper review.

Comment thread agent/chat_completion_helpers.py
@rodboev
rodboev force-pushed the pr/tool-schema-caching branch from f5ddc91 to 47b3e5f Compare July 13, 2026 22:36
@teknium1 teknium1 added sweeper:risk-caching Sweeper risk: may break/degrade prompt caching or cache-key stability (invariant) sweeper:blast-moderate Sweeper blast radius: moderate — a subsystem or single platform labels Jul 13, 2026
@rodboev
rodboev marked this pull request as draft July 31, 2026 19:34
@rodboev
rodboev force-pushed the pr/tool-schema-caching branch from 107fff0 to f562f52 Compare July 31, 2026 19:34
@rodboev rodboev changed the title perf(prompt-caching): cache tool schemas via Anthropic cache breakpoints perf(prompt-caching): cache tool schemas without history loss Jul 31, 2026
@rodboev
rodboev marked this pull request as ready for review July 31, 2026 19:47
@kshitijk4poor

Copy link
Copy Markdown
Collaborator

Merged via #76032 — your commit was cherry-picked onto current main with authorship preserved (rebase merge), so it lands under your name:

Verified end-to-end before merge: budget invariant (<=4 markers) across adversarial message shapes, prefix stability across turns, byte-identical fallback behavior for non-native routes, and marker survival through the Anthropic wire conversion — plus 294 targeted tests green.

Thanks for the contribution, and for the thorough rework after the sweeper round (the request-local plan + destination replanning design turned out very clean).

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

Labels

comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint P3 Low — cosmetic, nice to have provider/anthropic Anthropic native Messages API sweeper:blast-moderate Sweeper blast radius: moderate — a subsystem or single platform sweeper:risk-caching Sweeper risk: may break/degrade prompt caching or cache-key stability (invariant) type/perf Performance improvement or optimization

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tool-heavy agents pay ~70% input-token overhead — system_and_3 caching skips tools schema

4 participants