How do you make AI agents safe in production?
Learning path, stage 5 of 5: Shipping to production · question 2 of 10Assume the model can be wrong or manipulated, and build so that mistakes are limited and recoverable: least-privilege tools, sandboxed execution, a person approving consequential actions, checks in code on every tool call, limits on spend and steps, and full audit logs. The OWASP Top 10 for LLM Applications is a good checklist to walk through.
OWASP LLM risks to know
Prompt injection · sensitive information disclosure · supply chain · data and model poisoning · improper output handling · excessive agency · system prompt leakage · vector and embedding weaknesses · misinformation · unbounded consumption.
Controls
- Least privilege: scoped credentials per tool, read-only by default, separate service accounts.
- Sandbox: run code in containers or VMs with no secrets and an outbound network allowlist.
- People in the loop for payments, deletes, external messages and production deploys.
- Validate outputs: never pass model output to
eval, SQL, a shell or HTML without validating or escaping it. - Rate and budget limits per user and per run, plus timeouts.
- Audit trail: log prompts, tool calls, arguments, results and who approved what.
- Kill switch: be able to turn off a tool or an agent instantly.
- Red-team: jailbreak and injection suites in CI.
Sources
Code example
1// A guarded tool executor: allowlist, argument validation, approval, budget, audit.2import { z } from "zod";34const Tools = {5 read_file: { schema: z.object({ path: z.string().regex(/^docs\/[\w\-/]+\.md$/) }), risky: false },6 send_email: { schema: z.object({ to: z.string().email(), body: z.string().max(5000) }), risky: true },7} as const;89type Ctx = { spentUsd: number; budgetUsd: number; approve: (msg: string) => Promise<boolean>; audit: (e: object) => void };1011export async function guardedCall(name: string, rawInput: unknown, ctx: Ctx) {12 const tool = Tools[name as keyof typeof Tools];13 if (!tool) return { error: `Tool ${name} is not allowed` };1415 const parsed = tool.schema.safeParse(rawInput);16 if (!parsed.success) return { error: `Invalid arguments: ${parsed.error.message}` };1718 if (ctx.spentUsd > ctx.budgetUsd) return { error: "Budget exhausted; stopping." };19 if (tool.risky && !(await ctx.approve(`${name} ${JSON.stringify(parsed.data)}`))) {20 ctx.audit({ name, input: parsed.data, approved: false });21 return { error: "User declined" };22 }23 ctx.audit({ name, input: parsed.data, approved: true, at: new Date().toISOString() });24 return { ok: await dispatch(name, parsed.data) };25}2627declare function dispatch(name: string, input: unknown): Promise<string>;
To run it: follow the one-time setup guide, add npm i @anthropic-ai/sdk zod @opentelemetry/api vitest, save the code as a .ts file and run npx tsx file.ts. Anything marked declare is a part you supply.
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the AI Agents & LLMs set.