Skip to main content

Layerup Security

The Layerup Security integration allows you to secure your calls to any LangChain LLM, LLM chain or LLM agent. The LLM object wraps around any existing LLM object, allowing for a secure layer between your users and your LLMs.

While the Layerup Security object is designed as an LLM, it is not actually an LLM itself, it simply wraps around an LLM, allowing it to adapt the same functionality as the underlying LLM.


First, you'll need a Layerup Security account from the Layerup website.

Next, create a project via the dashboard, and copy your API key. We recommend putting your API key in your project's environment.

Install the Layerup Security SDK:

pip install LayerupSecurity

And install LangChain Community:

pip install langchain-community

And now you're ready to start protecting your LLM calls with Layerup Security!

from langchain_community.llms.layerup_security import LayerupSecurity
from langchain_openai import OpenAI

# Create an instance of your favorite LLM
openai = OpenAI(

# Configure Layerup Security
layerup_security = LayerupSecurity(
# Specify a LLM that Layerup Security will wrap around

# Layerup API key, from the Layerup dashboard

# Custom base URL, if self hosting

# List of guardrails to run on prompts before the LLM is invoked

# List of guardrails to run on responses from the LLM

# Whether or not to mask the prompt for PII & sensitive data before it is sent to the LLM

# Metadata for abuse tracking, customer tracking, and scope tracking.
metadata={"customer": ""},

# Handler for guardrail violations on the prompt guardrails
lambda violation: {
"role": "assistant",
"content": (
"There was sensitive data! I cannot respond. "
"Here's a dynamic canned response. Current date: {}"
if violation["offending_guardrail"] == "layerup.sensitive_data"
else None

# Handler for guardrail violations on the response guardrails
lambda violation: {
"role": "assistant",
"content": (
"Custom canned response with dynamic data! "
"The violation rule was {}."

response = layerup_security.invoke(
"Summarize this message: my name is Bob Dylan. My SSN is 123-45-6789."

API Reference:

Help us out by providing feedback on this documentation page: