The PromptGuard Node.js SDK secures your LLM calls automatically. Call
init() once and every request through OpenAI, Anthropic, Google AI, Cohere, or AWS Bedrock is scanned for prompt injection, data leaks, and policy violations — no code changes required.GitHub Repository
Open source - MIT license. Star the repo, report issues, or contribute.
Installation
Quick Start
client.chat.completions.create() is now scanned by PromptGuard before reaching OpenAI. If a threat is detected, a PromptGuardBlockedError is thrown.
Auto-Instrumentation
Auto-instrumentation is the recommended way to use the SDK. It monkey-patches thecreate / generateContent methods on supported LLM SDKs so that every call is scanned transparently.
init(options)
Call once at application startup, before making any LLM calls.
What gets patched
The provider SDKs and call surfacesinit() patches, the libraries it does not (the Vercel AI SDK’s providers among them), and how to check at runtime are on the auto-instrumentation page.
shutdown()
Remove all patches and clean up. Call when your application is shutting down.
Enforce vs. Monitor Mode
Framework Integrations
For deeper integration with specific frameworks, the SDK provides dedicated adapters. These are useful when you want framework-level context (chain names, tool calls, agent steps) in your threat logs.LangChain.js
ThePromptGuardCallbackHandler implements the LangChain BaseCallbackHandler interface to scan prompts before LLM calls, responses after, and tool inputs/outputs.
The handler automatically captures rich context: chain names, parent run IDs, tags, metadata, and tool names. This context is sent to the Guard API for more accurate threat detection.
Vercel AI SDK
Wrap your model withpromptguard() and use the wrapped model wherever you used the original — generateText, streamText and agents all call it. It wraps the models of ai 5, 6 and 7; on ai 4, use promptGuardMiddleware() below.
ai 4, or if you already compose AI SDK middleware, promptGuardMiddleware() does the same scanning, with the same options, as a LanguageModelMiddleware for wrapLanguageModel:
Input is scanned before every call. In enforce mode a block throws
PromptGuardBlockedError before the model is called, and a redaction rewrites the prompt the model receives. With scanResponses: true, streamed text is held until it has been scanned, a window at a time. The SDK README describes exactly what is held and when.
generateText() rejects with PromptGuardBlockedError on any block. With streamText(), how a block reaches you depends on where it happened and on the model’s AI SDK specification version (checked against ai 5, 6 and 7). Nothing in it hangs:
On
ai 5 the text alone looks like a short answer. If you read only the text, check await result.finishReason === 'error', which is how a blocked stream finishes there, or use onError or fullStream. The V2 row also applies to a V2 model (a provider package still on V2) under ai 6 or 7, through promptguard() and through ai 6’s wrapLanguageModel; such a stream does not throw. ai 7’s wrapLanguageModel upgrades a V2 model to V4 first, so the middleware there follows the V4 row.
Auto-instrumentation vs. framework integrations: Use auto-instrumentation when you want zero-config protection across your entire app. Use framework integrations when you need per-chain or per-model control, or want richer context in your threat logs.
Guard Client
TheGuardClient provides standalone access to the PromptGuard Guard API for manual content scanning. Use this when you need direct control over what gets scanned and how results are handled.
Setup
guard.scan(messages, direction?, model?, context?)
Scan messages for threats via the Guard API.
GuardMessage type:
GuardContext type:
GuardDecision
The scan() method returns a GuardDecision object:
ThreatDetail type:
Error Handling
The SDK defines two error classes for different failure modes.PromptGuardBlockedError
Thrown when a request is blocked in enforce mode. Contains the full GuardDecision with threat details.
GuardApiError
Thrown on API or network errors when failOpen is false. When failOpen is true (default), API errors are silently swallowed and the LLM call proceeds.
Legacy PromptGuardError
The proxy-mode PromptGuard class throws PromptGuardError for API failures:
Retry Configuration
ThePromptGuard proxy client automatically retries requests that fail with 429 (rate limited), 5xx (server error), or transient network errors (connection resets, DNS failures, timeouts). Retries use exponential backoff with jitter.
Retry behavior:
- 429 responses — retried after the
Retry-Afterheader value (if present), otherwise exponential backoff - 500, 502, 503, 504 responses — retried with exponential backoff
- Network errors (connection reset, DNS failure, socket timeout) — retried with exponential backoff
- 4xx responses (other than 429) — not retried (these indicate client errors)
Proxy Mode (Legacy)
ThePromptGuard class provides an OpenAI-compatible client with additional security namespaces. It sends only your PromptGuard key, so chat completions reach the provider only when you have stored a provider key for it. It does not stream: stream: true throws a PromptGuardError with code streaming_not_supported.
Chat Completions
Security Scanning
PII Redaction
Agent Tool Validation
Red Team Testing
Complete Example
A full example combining auto-instrumentation with aGuardClient for custom scanning workflows:
Next steps
Node.js SDK reference
Every export, option and type in the Node.js SDK
Auto-instrumentation
Which provider calls
init() patches, and which it does notStreaming
How streamed responses are buffered, scanned and released
Vercel AI SDK
The Node.js SDK middleware for the Vercel AI SDK