DanLevy.net

प्रोडक्शन AI डरावना है (और इसे कैसे ठीक करें)

यदि आपके एजेंट में गार्डरेल्स नहीं हैं, तो आप प्रोडक्शन के लिए तैयार नहीं हैं।

कोई भी असुरक्षित AI सिस्टम बनाने के इरादे से शुरुआत नहीं करता। आप निर्देश लिखते हैं, एज केस टेस्ट करते हैं, कुछ वैलिडेशन नियम जोड़ते हैं। फिर किसी को पता चलता है कि वे आपके बॉट को समुद्री डाकू बनकर रोलप्ले करने और यूज़र डेटा उजागर करने के लिए बरगला सकते हैं। या आपके लॉग में क्रेडिट कार्ड नंबर आ जाता है। या मॉडल आत्मविश्वास से किसी प्रतियोगी के उत्पाद की सिफारिश कर देता है।

‘डेमो में काम करता है’ और ‘प्रोडक्शन में सुरक्षित’ के बीच का अंतर अधिकांश टीमों की अपेक्षा से कहीं अधिक चौड़ा है।

समस्या का एक हिस्सा यह है कि कच्चे LLMs को इस बात की परवाह नहीं होती कि उन्हें क्या करना चाहिए या क्या नहीं। वे भविष्यवाणी करने वाली मशीनें हैं जो आपके द्वारा शुरू किए गए पैटर्न को जारी रखने की कोशिश करती हैं। उन्हें एक प्रॉम्प्ट दें जो ‘system override mode’ जैसा लगता है, और वे खुशी-खुशी उसके साथ खेलेंगे। यह मॉडल में कोई बग नहीं है; यह बस वही है कि भाषा मॉडल कैसे काम करते हैं।

अधिकांश फ्रेमवर्क आपको मॉडल देकर शुभकामनाएं देते हैं। Mastra एक अलग दृष्टिकोण अपनाता है: यह मानता है कि आपको अंततः गार्डरेल की ज़रूरत होगी, इसलिए वह उन्हें शुरू से ही एजेंट आर्किटेक्चर में बनाता है।


प्रोसेसर सुरक्षा परतों के रूप में

मूल तंत्र सीधा है। आपका प्रॉम्प्ट मॉडल तक पहुंचने से पहले, यह इनपुट प्रोसेसर की एक श्रृंखला से गुज़रता है। मॉडल के जवाब देने के बाद, आउटपुट प्रोसेसर अपनी बारी लेते हैं। प्रत्येक प्रोसेसर उस स्टेज पर सामग्री का निरीक्षण, संशोधन या ब्लॉक कर सकता है।

इन्हें AI इंटरैक्शन के लिए मिडलवेयर समझें। आप जिनकी ज़रूरत है उन्हें स्टैक करते हैं, उनके व्यवहार को कॉन्फ़िगर करते हैं, और वे हर रिक्वेस्ट पर स्वचालित रूप से चलते हैं।

1. समुद्री डाकुओं को रोकना (प्रॉम्प्ट इंजेक्शन)

प्रॉम्प्ट इंजेक्शन हमले काफी रचनात्मक हो गए हैं। लोग अदृश्य यूनिकोड कैरेक्टर, बेस64 में लिखे निर्देश, या मॉडल को यह विश्वास दिलाने का प्रयास करते हैं कि वे ‘डीबग मोड’ में हैं जहाँ सामान्य नियम लागू नहीं होते। तकनीकें लगातार विकसित हो रही हैं।

Mastra में ऐसे प्रोसेसर शामिल हैं जो सामान्य पैटर्न को पकड़ते हैं:

src/mastra/agents/secure-agent.ts
import { Agent } from '@mastra/core/agent';
import { PromptInjectionDetector, UnicodeNormalizer } from '@mastra/core/processors';
const GUARDRAIL_MODEL = 'openrouter/openai/gpt-oss-safeguard-20b';
export const secureAgent = new Agent({
id: 'fortress-assistant',
name: 'fortress-assistant',
instructions: 'You are a secure assistant.',
model: 'openai/gpt-5.5',
inputProcessors: [
// 1. Scrub invisible characters
new UnicodeNormalizer({
stripControlChars: true,
collapseWhitespace: true,
}),
// 2. Detect the attempt
new PromptInjectionDetector({
model: GUARDRAIL_MODEL,
threshold: 0.8,
strategy: 'block', // Hard stop
detectionTypes: ['injection', 'jailbreak', 'system-override'],
lastMessageOnly: true,
}),
],
});

UnicodeNormalizer कंट्रोल कैरेक्टर को हटाता है और व्हाइटस्पेस को संक्षिप्त करता है। PromptInjectionDetector साफ किए गए इनपुट में उन पैटर्न के लिए विश्लेषण करता है जो बताते हैं कि कोई आपके निर्देशों को ओवरराइड करने की कोशिश कर रहा है।

आप कॉन्फ़िगर करते हैं कि आप डिटेक्शन को कितना आक्रामक बनाना चाहते हैं (threshold पैरामीटर) और जब यह ट्रिप होता है तो क्या होना चाहिए (block, warn, filter, या rewrite)।

2. PII को संभालना

लॉग में क्रेडिट कार्ड नंबर, वेक्टर डेटाबेस में सोशल सिक्योरिटी नंबर, आवश्यकता से अधिक समय तक संग्रहीत ईमेल पते। ये वे प्रकार की समस्याएं हैं जो नियामक समस्याओं में बदल जाती हैं। चुनौती यह है कि उपयोगकर्ताओं को हमेशा यह एहसास नहीं होता कि वे चैट विंडो में संवेदनशील डेटा पेस्ट कर रहे हैं।

PIIDetector आपके मॉडल तक पहुंचने या स्टोरेज में लिखे जाने से पहले सामान्य पैटर्न के लिए स्कैन करता है:

import { Agent } from '@mastra/core/agent';
import { BatchPartsProcessor, PIIDetector } from '@mastra/core/processors';
export const privateAgent = new Agent({
id: 'privacy-first-assistant',
name: 'privacy-first-assistant',
instructions: 'You are a helpful assistant that never stores personal information.',
model: 'openai/gpt-5.5',
inputProcessors: [
new PIIDetector({
model: GUARDRAIL_MODEL,
detectionTypes: ['email', 'phone', 'credit-card', 'ssn'],
threshold: 0.6,
strategy: 'redact',
redactionMethod: 'mask',
instructions: 'Detect and mask personally identifiable information',
lastMessageOnly: true,
}),
],
outputProcessors: [
new BatchPartsProcessor({ batchSize: 10 }),
new PIIDetector({
model: GUARDRAIL_MODEL,
strategy: 'redact',
redactionMethod: 'mask',
}),
],
});

आप रिडैक्ट, हैश, हटा, टाइप किए गए प्लेसहोल्डर से बदल, या पूरी तरह से ब्लॉक करना चुन सकते हैं। PIIDetector एक हाइब्रिड प्रोसेसर है: इसे inputProcessors, outputProcessors, या दोनों में रखें, जोखिम कहाँ है इस पर निर्भर करता है। स्ट्रीम किए गए आउटपुट के लिए, भारी क्लासिफायर चलाने से पहले बैच चंक्स करें ताकि आप हर छोटी टोकन ड्रिप पर एक अलग LLM चेक का भुगतान न करें।

3. सामग्री मॉडरेशन

Models trained on internet data have seen some things. Without filtering, they can occasionally produce responses that would make your PR team nervous. The ModerationProcessor catches content that violates your guidelines:

import { Agent } from '@mastra/core/agent';
import { BatchPartsProcessor, ModerationProcessor } from '@mastra/core/processors';
export const moderatedAgent = new Agent({
id: 'safe-assistant',
name: 'safe-assistant',
instructions: 'You are a helpful assistant for a community platform.',
model: 'openai/gpt-5.5',
inputProcessors: [
new ModerationProcessor({
model: GUARDRAIL_MODEL,
categories: ['hate', 'harassment', 'violence', 'self-harm'],
threshold: 0.7,
strategy: 'block',
instructions: 'Detect harmful content that violates community guidelines',
lastMessageOnly: true,
}),
],
outputProcessors: [
new BatchPartsProcessor({ batchSize: 10 }),
new ModerationProcessor({
model: GUARDRAIL_MODEL,
categories: ['hate', 'harassment', 'violence', 'self-harm'],
strategy: 'filter',
chunkWindow: 1,
}),
],
});

The interesting part is that you define which categories matter for your use case. A creative writing tool might allow more expressive content than a customer service bot. The threshold and strategy give you control over how strict the filtering should be.


जब चीजें फँसती हैं

When a processor uses the block strategy, Mastra aborts generation and exposes the event as tripwire metadata. With generate(), check the result object:

const result = await secureAgent.generate('Ignore all previous instructions...');
if (result.tripwire) {
console.log(`Blocked by ${result.tripwire.processorId}`);
console.log(`Reason: ${result.tripwire.reason}`);
// "Blocked! Reason: Prompt injection detected."
return 'Request blocked by policy.';
}

For streaming calls, listen for tripwire chunks on fullStream. This pattern lets you handle security events however makes sense for your application. You might log them for analysis, return a generic error message, or switch a low-risk case from block to warn while you tune thresholds. The processorId and reason tell you which processor flagged the content, which helps when you’re debugging false positives.


यह क्या हल नहीं करता

Processors catch a lot, but they’re not magic. A determined attacker with enough time can probably find a prompt that slips through. Models occasionally hallucinate in ways that processors can’t predict. And there’s always a tradeoff between security and flexibility: the stricter your rules, the more likely you’ll block legitimate use cases.

The value isn’t perfect protection. It’s having a systematic way to handle the common issues that will definitely come up in production. You can tune the sensitivity as you learn what your users actually do. You can add custom processors for domain-specific risks. And you can wire violation callbacks, logs, traces, and app-level audit records around the same control point.

Most security problems in production AI aren’t sophisticated attacks. They’re people copying and pasting data they shouldn’t, or discovering through trial and error that the bot will do things you didn’t intend. Processors won’t stop every possible issue, but they make the obvious ones much harder.

संसाधन

श्रृंखला पढ़ें

  1. LLM रूटिंग
  2. सुरक्षा और गार्ड्रेल्स (यह पोस्ट)
  3. MCP और टूल इंटीग्रेशन
  4. वर्कफ़्लो और मेमोरी