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.
Every public class, function and method, with signatures and parameters: Node.js SDK reference.

Installation

Requires Node.js 20+. The SDK has zero required dependencies — it patches whichever LLM SDKs you already have installed. Pin the version you tested in your own lockfile. Moving from 1.x? See Upgrade to SDK and CLI 2.0.

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.

What gets patched

The provider SDKs and call surfaces init() 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

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

Wrap your model with promptguard() 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.
The wrapped model keeps the original’s provider, model ID and specification version, so it type-checks wherever the original did. Frozen models wrap too. On 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

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. 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 a GuardClient 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 not

Streaming

How streamed responses are buffered, scanned and released

Vercel AI SDK

The Node.js SDK middleware for the Vercel AI SDK
Last modified on October 31, 2026