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.
Supported LLM SDKs
Auto-instrumentation patches the following SDKs when they are installed:SDKs that are not installed are silently skipped — no errors, no warnings. Install only the ones you use.
Framework Support
Because auto-instrumentation patches the underlying SDK prototypes, it works automatically with any framework built on top:- LangChain.js —
ChatOpenAI,ChatAnthropic,ChatGoogleGenerativeAI,ChatCohere - Vercel AI SDK — OpenAI, Anthropic, Google, and Amazon Bedrock providers
- AutoGen.js, CrewAI.js, and any other framework using these SDKs
openai.chat.completions.create() under the hood, PromptGuard intercepts it.
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
ThepromptGuardMiddleware factory returns a Vercel AI SDK LanguageModelMiddleware object that you can use with wrapLanguageModel.
The middleware hooks into
transformParams (pre-call scanning) and wrapGenerate (post-call scanning). Redacted content is automatically applied back into the prompt structure.
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.
Chat Completions
Security Scanning
PII Redaction
Agent Tool Validation
Red Team Testing
Embeddings
Generate embeddings through the PromptGuard proxy:Legacy Completions
Complete Example
A full example combining auto-instrumentation with aGuardClient for custom scanning workflows: