Skip to content

perf(provider): prompt-cache breakpoints for Anthropic Messages and Bedrock Converse (C4) - #61

Merged
lg2m merged 2 commits into
fix/context-usability-3-audit-defaultfrom
perf/context-usability-4-cache-breakpoints
Sep 17, 2026
Merged

lg2m merged 2 commits into
fix/context-usability-3-audit-defaultfrom
perf/context-usability-4-cache-breakpoints

Conversation

@lg2m

@lg2m lg2m commented Sep 17, 2026

Copy link
Copy Markdown
Collaborator

C4: cache_control / cachePoint on system, last tool, last message block

Spec: docs/plans/context-usability.md § C4. Stacked on #59 (C3); review against that base.

User-visible behavior

Anthropic and Bedrock-Anthropic sessions get provider prompt caching. Before, qq-provider emitted no cache markers, so every turn re-processed the entire transcript: TTFT grew linearly with context and input tokens were billed at full price each turn. pi, fx, OpenCode, and Codex all mark the same three positions.

What changed

  • providers/anthropic.rs: system is one text block with cache_control: {type: ephemeral}; the last tools[] entry and the last content block of the last message carry the same marker; nothing else does, so turn N+1 extends turn N's cached prefix. Wire change: single-text messages serialize as [{type: text, …}] arrays rather than bare strings (both accepted by the API).
  • providers/bedrock.rs: SystemContentBlock::CachePoint, Tool::CachePoint, ContentBlock::CachePoint at the same three positions, only when the model id has an anthropic segment (anthropic., us.anthropic., global.anthropic., inference-profile ARNs). Nova/Llama/Mistral/OpenAI-OSS are untouched because Converse rejects cachePoint for them.
  • src/runtime.rs: ResolvedModel.prompt_cache.control = Native for AnthropicMessages and BedrockConverse.

How verified

  • cargo test --workspace; cargo test -p qq-provider --no-default-features --features test-support (minimal profile); fmt; strict all-target clippy.
  • New: anthropic::cache_breakpoints_mark_exactly_the_system_last_tool_and_last_block (exactly three markers on a two-tool, three-message request); bedrock::maps_the_system_prompt_to_converse_system_blocks_with_a_cache_point; bedrock::cache_points_follow_the_anthropic_family_and_no_other; root resolved_model asserts Native for Bedrock.
  • Protocol fixtures unchanged (no wire type change; control is an existing enum).

Measurements

Gate Before After Budget
provider_encode anthropic, 1 MiB + 32 schemas 195 µs, heap 1.39x 204 µs, heap 1.44x ≤ 10 ms, ≤ 2x

Live cache_read_input_tokens > 0 on turn 2 is a paid check; not run here.

Docs

docs/design/providers.md (codec breakpoint rule); ledger.

…and Bedrock Converse (C4)

Neither codec asked the provider to cache anything, so every turn of a
long Anthropic or Bedrock session re-processed the full transcript
uncached: time to first token grew linearly with context and input cost
was roughly an order of magnitude higher than it needed to be. Both
providers require explicit breakpoints.

Anthropic Messages now sends the system prompt as one text block with
`cache_control: ephemeral`, marks the last tool declaration and the last
block of the last message, and leaves everything else unmarked so
consecutive turns extend one cached prefix. Bedrock Converse places the
equivalent `cachePoint` blocks at the same three positions, gated to the
Anthropic model family (regional, global, and inference-profile ids)
because Converse rejects the block for families without prompt caching.
`ResolvedModel.prompt_cache.control` reports `Native` for both APIs.

Wire change: single-text Anthropic messages serialize as block arrays.
`provider_encode` anthropic 195 → 204 µs on a 1.1 MiB body.
…r-turn call cap (C5) (#62)

A turn with one edit and five reads ran all six in series; a turn naming
seventeen calls killed the run with a provider-protocol failure. Every
reference harness overlaps read-only calls and none fails a run for
batching well.

The tool phase now splits the turn at the first call that is not an
overlappable read: the leading reads run under MAX_PARALLEL_READS, the rest
in request order, so a read after a mutation is still ordered against it.
Calls past the sixteenth are admitted with a not-executed tool error that
tells the model to re-issue them; a turn naming more than 64 calls remains
a protocol failure. Prompt version 14 asks the model to batch independent
calls and names the cap.
@lg2m
lg2m force-pushed the perf/context-usability-4-cache-breakpoints branch from d595eee to 55ea7a7 Compare September 17, 2026 01:50
@lg2m
lg2m merged commit 49d4a03 into main Sep 17, 2026
4 checks passed
@lg2m
lg2m deleted the perf/context-usability-4-cache-breakpoints branch September 17, 2026 01:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant