Comprehensive security guide for building and deploying AI applications safely
AI applications introduce unique security challenges that traditional software doesn't face. A single compromised API key can result in:
In January 2024, a developer accidentally committed an OpenAI API key to a public GitHub repository. The key was discovered by automated scrapers and used to generate fraudulent API charges. This scenario is preventable.
# NEVER DO THIS
const apiKey = "sk-proj-abc123..."; // Hardcoded = security breach
# DO THIS - Environment Variables
const apiKey = process.env.OPENAI_API_KEY;
# BEST - Secrets Manager (Production)
import { SecretsManager } from '@aws-sdk/client-secrets-manager';
const secret = await secretsManager.getSecretValue({
SecretId: 'prod/openai/api-key'
});Prompt injection is to LLMs what SQL injection is to databases: a critical vulnerability that lets attackers manipulate system behavior through malicious input. Unlike SQL injection, there's no perfect defense—only layered mitigation strategies.
System Prompt:
Attacker Input:
Vulnerable Response:
Strip dangerous patterns before they reach the model:
Use XML-style delimiters to separate instructions from user input:
Scan model responses for leaked sensitive information:
Never give LLMs direct database access or system execution capabilities. Use function calling with strict parameter validation and approval workflows for sensitive operations.
Never expose LLM APIs directly to clients. Route through a backend gateway with:
Assume all components are potentially compromised:
If you're fine-tuning models on user-generated data, implement safeguards against poisoning attacks where malicious actors inject harmful training examples.
Despite best efforts, security incidents will occur. Prepare a response plan before you need it.
The main security risks for AI applications are: exposing API keys in client-side code or public repos, prompt injection (user input hijacking your system prompt), insecure handling of LLM output (treating AI responses as trusted HTML/SQL), over-permissioned API access, and logging sensitive user data that gets ingested back into training pipelines. Treat AI APIs the same way you treat any external service: authenticate securely, validate all output, and never trust user-supplied input blindly.
API keys must stay server-side. In Next.js, only variables prefixed with NEXT_PUBLIC_ are exposed to the browser — your OpenAI or Anthropic key should never have that prefix. Route all AI API calls through a server-side API route or backend endpoint. Use environment secrets in your deployment platform (Vercel, Cloudflare) rather than .env files in version control. Rotate keys immediately if you suspect exposure.
Prompt injection is when a user crafts input that overrides or manipulates your system prompt — for example, 'Ignore all previous instructions and...' This can cause the model to leak your system prompt, bypass restrictions, or behave maliciously. Mitigations include: keeping system prompts minimal and not secret-dependent, validating and sanitizing user input before passing to the model, using separate system and user message roles, and not granting the LLM permissions to execute actions based solely on user-supplied text.
Apply data minimization: only send the data the model needs to complete the task, not entire user profiles. Strip or redact PII (names, emails, IDs) before logging prompts and responses. Review your AI provider's data retention policy — most providers allow you to opt out of using your data for training. Store conversation history with the same security as any sensitive database: encrypted at rest, access-controlled, and with a defined retention period.
Least privilege means giving your AI system only the minimum access it needs. For tool-calling or agent-based systems: only give the LLM access to specific functions it needs, require human confirmation before destructive actions (delete, send, pay), scope database access to read-only where possible, and never let the LLM execute arbitrary code unless that is the explicit feature. Each expanded capability is an expanded attack surface.