Function Signature
Parameters
An instance of the Anthropic SDK client (from
@anthropic-ai/sdk package >= 0.20.0)Configuration options for Shield protection
ShieldAnthropicOptions
System prompt used for sanitization. When omitted, Shield automatically derives it from the
system parameter in your request.Options for prompt hardening. Set to
false to disable hardening. See harden() for available options.Options for injection detection. Set to
false to disable detection. See detect() for available options.Options for output sanitization. Set to
false to disable sanitization. See sanitize() for available options.Streaming sanitization strategy:
"buffer": Accumulate the full stream, then sanitize (higher memory, more accurate)"chunked": Process in 8KB chunks (lower memory for long streams)"passthrough": Skip sanitization entirely (use when you accept the risk)
Chunk size in bytes for
"chunked" mode. Only applies when streamingSanitize is set to "chunked".Behavior when injection is detected:
"block": ThrowInjectionDetectedError(request fails)"warn": Only invokeonInjectionDetectedcallback (request continues)
When
true, throw LeakDetectedError instead of redacting leaked content. Use for strict security policies where any leak should abort the request.Callback invoked when an injection is detected. Receives the full
DetectResult with risk level and matched patterns.Callback invoked when a prompt leak is detected in the output. Receives the full
SanitizeResult with confidence score and leaked fragments.Return Type
Returns the same client typeT with Shield protection applied. All methods work identically to the original client.
Examples
Basic Usage
Streaming with Chunked Sanitization
Custom Detection Callbacks
Strict Mode (Throw on Any Leak)
Notes
- Multi-part system prompts: Anthropic supports
systemasstring | Array<{ type: string; text: string }>. Shield extracts text from all blocks for hardening and sanitization. - Multi-part messages: Message
contentcan bestring | Array<{ type: string; text: string }>. Shield extracts text from all parts for injection detection. - Tool use: Shield automatically sanitizes the
inputobject in tool use blocks to prevent leaks in structured outputs. - Auto-derived system prompt: When
systemPromptis not provided, Shield extracts it from thesystemparameter in your request.