Skip to main content
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

Requires Node.js 18+. The SDK has zero required dependencies — it patches whichever LLM SDKs you already have installed.

Quick Start

That’s it. Every call to 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 the create / 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.
The apiKey falls back to the PROMPTGUARD_API_KEY environment variable, so you can omit it in production if the env var is set.

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.
The Bedrock patch intercepts InvokeModel, Converse, and ConverseStream commands. It handles all Bedrock-hosted models: Claude, Titan, Llama, Mistral, and Cohere on Bedrock.

Framework Support

Because auto-instrumentation patches the underlying SDK prototypes, it works automatically with any framework built on top:
  • LangChain.jsChatOpenAI, ChatAnthropic, ChatGoogleGenerativeAI, ChatCohere
  • Vercel AI SDK — OpenAI, Anthropic, Google, and Amazon Bedrock providers
  • AutoGen.js, CrewAI.js, and any other framework using these SDKs
No extra configuration needed. If the framework calls 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

Start with mode: "monitor" in production to observe what would be blocked before switching to mode: "enforce".

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

The PromptGuardCallbackHandler 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

The promptGuardMiddleware 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

The GuardClient 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

The PromptGuard 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-After header 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)
The GuardClient also supports retry configuration via the same maxRetries and retryDelay options.

Proxy Mode (Legacy)

Proxy mode is the original SDK interface. It still works but auto-instrumentation is recommended for new projects. Proxy mode routes requests through the PromptGuard proxy, while auto-instrumentation scans locally and sends directly to your LLM provider.
The PromptGuard 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:
Batch embedding with an array of inputs:

Legacy Completions

The completions API is deprecated and provided only for backward compatibility. Use chat.completions.create() instead for all new code.

Complete Example

A full example combining auto-instrumentation with a GuardClient for custom scanning workflows: