fix: preserve unknown fields in Anthropic schemas (restores prompt caching)#74
Merged
sgasser merged 3 commits intosgasser:mainfrom Feb 27, 2026
Conversation
- Add biome-ignore comments to suppress noExplicitAny in tests (required for testing unknown field preservation) - Add .passthrough() to OpenAI schemas for consistency (OpenAIMessageSchema, OpenAIContentPartSchema) - Format Anthropic schemas to match project style
- Schema tests for name, tool_calls, audio content, unknown fields - Extractor tests for field preservation through applyMasked
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.
Problem
Clients that rely on prompt caching experienced dramatically higher token usage when routing through PasteGuard. Every request was billed as if the full system prompt and tool definitions were new input, rather than being served from cache.
Root Cause
PasteGuard validates incoming requests using Zod schemas. Only the top-level
AnthropicRequestSchemaused.passthrough(), but all nested schemas —TextBlockSchema,AnthropicMessageSchema,ToolSchema, and others — did not.Zod strips unknown fields by default. This silently removed
cache_control: { "type": "ephemeral" }from content blocks, system prompt blocks, tool definitions, and messages before they were forwarded upstream. Without these fields, the API receives no cache breakpoints and prompt caching is disabled entirely.The OpenAI schemas already handled this correctly via
OpenAIMessageSchema.passthrough().Fix
Add
.passthrough()to all nestedz.object()schemas insrc/providers/anthropic/types.ts, matching the OpenAI implementation. This ensures any fields PasteGuard does not explicitly declare are passed through unchanged.Tests
Added regression tests covering:
cache_controlpreservation through Zod parsing on text blocks, system blocks, tools, and messagescache_controlsurvival through the fullapplyMaskedround-trip in the extractor