Add prompt-prefix caching (cache.checkpoint) support#640
Closed
sroussey wants to merge 0 commit into
Closed
Conversation
Coverage Report
File Coverage
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Collaborator
Author
|
✅ CI is fully green on Since the PR opened, the branch gained two commits:
Known deferred (correctness unaffected): AiChatTask doesn't yet reuse a checkpoint's warmed KV on local providers (re-encodes the prefix; needs a cross-provider design pass), and chained-emit KV reuse on HFT relies on the concatenative-template assumption with re-encode fallback. Generated by Claude Code |
sroussey
force-pushed
the
claude/ai-provider-cache-checkpoints-3qtwbm
branch
from
July 17, 2026 00:39
9bf3ee6 to
a90e59c
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements prompt-prefix caching across the AI task framework and provider implementations. This feature allows tasks to eagerly warm provider-side caches (e.g., Anthropic's prompt caching, local KV state) and emit opaque checkpoint handles that downstream tasks can consume to avoid re-processing identical prefixes.
Key Changes
Core Framework
CacheCheckpointTask: Eagerly warms a prompt-prefix cache with system prompt, tools, and messages; outputs an opaque checkpoint handle. Lifecycle is tied to the run'sResourceScope— handles are auto-disposed at run end unless a shared scope is injected.CheckpointRegistry: Main-thread registry mapping checkpoint IDs toCheckpointEntryrecords (provider, model key, prefix content). Supports register/retrieve/delete operations.CheckpointPortsmodule: Shared input/output schema fragments and resolution logic for tasks that consume/emit checkpoints. IncludesresolveCheckpointSession()to validate checkpoint IDs and provider/model compatibility before dispatch.AiSessionContext: ExtendedAiProviderRegistrywith a new session context type passed to provider run functions, carryingsessionId,prefix, andparentIdfor checkpoint support.Task Updates
ToolCallingTask: Added checkpoint input ports (checkpoint,emitCheckpoint,keepParentCheckpoint) and output port. Consumes checkpoints to rewind sessions; emits new checkpoints after turns. Parent checkpoints are superseded (disposed) by default unlesskeepParentis set.TextGenerationTask: Added same checkpoint ports asToolCallingTaskfor consistency.AiTaskbase class: Updated to passsessioncontext (instead of baresessionId) to provider run functions.Provider Implementations
buildAnthropicCheckpointParams()constructs minimalmessages.createparams that write prefixes to server-side prompt cache withcache_control: { type: "ephemeral" }on system block, last tool, and last message.applyAnthropicPrefixReplay()prepends cached prefix to new turns.renderHftPrefixPrompt()andrenderHftContinuationPrompt()render prefixes through the model's chat template to ensure tokenization matches warm-up. Prefix-rewind sessions reuse cached KV state.renderLlamaCppPrefixText()flattens prefix into preloadable text. Sessions track prefix state and rewind on checkpoint consumption.AiSessionContextinstead of bare session ID.Testing
CacheCheckpointTask.test.ts) covering:keepParentbranching behaviorToolCallingTaskNotable Implementation Details
CheckpointPrefixusesreadonlyproperties; providers replay or re-encode it deterministically.keepParent: true, preventing resource leaks in linear workflows while supporting branching.ResourceScopecompletes, ensuring cleanup even if tasks don't explicitly delete them.CheckpointPortsand mixed into task schemas, allowing any task to opt in without duplication.https://claude.ai/code/session_01DsBvrmfhe1aY63cjztn1WW