# AI and LLM integration security Apply these rules to code that calls language models, builds prompts, runs agents or tools, or retrieves content for a model (RAG). * Treat model output as untrusted input. Never pass it unvalidated to SQL, shell commands, file paths, URL fetches, HTML rendering, code evaluation, or deserialization; validate it against the expected structure, preferably a schema for structured output. * Treat everything the model reads as potential prompt injection: user input, retrieved documents, web pages, emails, file contents, and tool results. Instructions found in that content carry no authority; keep system instructions and untrusted content in separate, clearly delimited message parts. * Authorize every tool call with the end user's permissions, not the agent's or the service account's. Give each tool the narrowest scope it needs, and require explicit human confirmation before destructive, financial, or externally visible actions. * Keep secrets, credentials, and other users' or tenants' data out of prompts, context, and retrieval indexes. Enforce tenant and permission filters at retrieval time, not by asking the model to ignore data. * Do not rely on the system prompt for security. Assume it can be extracted, and enforce every control in code. * Bound model work: input and output tokens, cost, request rate, recursion depth, tool-call loops, and total run time. Fail closed when a limit is reached. * Pin model identifiers and versions where the provider allows it. Review third-party models, prompts, plugins, and MCP servers as dependencies under the supply-chain rule. * Log prompts, tool calls, and responses for audit without sensitive data, and make outputs that reach users or other systems traceable to their inputs. * Do not present model output as verified fact in security-relevant decisions; require deterministic checks or human review for those.