Prompt Caching
How crabcode reuses prompt prefixes across providers to cut cost and latency.
Reuse stable prefixes
Prompt caching reuses the stable part of a request (system prompt, tool schemas, conversation prefix) so multi-step tool loops do not re-pay full input cost every turn.
Most providers either cache automatically or need only a sticky session key. Anthropic is the exception: it needs explicit cache breakpoints (or a gateway that inserts them).
Provider matrix
What crabcode does today for each routing path:
Anthropic is the unfriendly one. Without
cache_control(or Gatewaycaching: auto), Claude traffic does not cache-read. That was the main gap crabcode fixed.
What gets marked (direct Anthropic)
Aligned with OpenCode's auto policy:
- Last tool — tool schemas are large and stable across a tool loop
- Last system block — instructions / project context
- Latest user content block — conversation prefix through the current turn (including tool-result groups)
Breakpoints are applied after message regrouping so adjacent tool_use / tool_result blocks stay valid.
How to verify it works
Run with logs enabled and watch app.log for [prompt-cache] lines:
crabcode --emit-logsLogging writes to app.log in the working directory when --emit-logs is set.
Direct Anthropic
[prompt-cache] anthropic input=… output=… cache_read=… cache_creation=…
AI Gateway / OpenAI-compatible
[prompt-cache] openai-compatible prompt=… completion=… cached_tokens=… cache_read=… cache_creation=…
Healthy multi-step session
Notes:
- Anthropic's
input_tokensis non-cached only. Total input ≈input+cache_read+cache_creation. - Gateway may surface cache as
prompt_tokens_details.cached_tokensand/or forwarded Anthropic fields — crabcode logs both when present. - No dashboard required: stream finals carry usage; crabcode logs them when logging is enabled.
What we intentionally do not do
Related
- AI Gateway automatic caching: Vercel docs
- Anthropic prompt caching: provider docs for
cache_control/ephemeral