护栏与安全的代理行为
为人工智能代理实施实用的安全护栏,包括输入和输出验证、内容过滤以及受限的工具访问,防止有害或意外的操作。
护栏与安全的代理行为 是 CoddyKit 上的免费 AI Agents with LangChain & Autonomous Workflows 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 AI Agents with LangChain & Autonomous Workflows 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 AI Agents with LangChain & Autonomous Workflows 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
From Ethics to Engineering
Ethical principles need enforcement in code. Guardrails are the concrete controls that keep an agent within safe, intended boundaries at runtime.
They act on three points: the input, the model's reasoning, and the output or actions.
Input Guardrails
Check user input before it reaches the agent. Catch:
- Prompt-injection attempts
- Requests for disallowed topics
- Personal data that should be redacted
Detecting Prompt Injection
Prompt injection tries to override your instructions (e.g. ignore previous rules). A simple guard flags suspicious phrases before processing.
BAD = ['ignore previous', 'disregard instructions']
if any(p in user_input.lower() for p in BAD):
raise ValueError('Possible prompt injection')Output Guardrails
Validate what the agent produces before showing it. Block unsafe content, leaked secrets, or off-policy answers, replacing them with a safe fallback message.
if contains_sensitive(answer):
answer = 'I cannot share that information.'Structured Output Validation
When an agent must return JSON, validate it against a schema. Reject or repair malformed output so downstream systems never receive bad data.
from pydantic import BaseModel
class Ticket(BaseModel):
priority: str
summary: str
Ticket.model_validate_json(agent_output)Constraining Tool Access
The most dangerous actions come from tools. Give an agent only the tools it needs, and scope each one — read-only where possible, with limits on what it can affect.
Allowlists Over Blocklists
Define what is permitted rather than chasing every bad case. An allowlist of approved domains, tables, or operations is far safer than trying to enumerate everything to forbid.
ALLOWED_DOMAINS = {'docs.company.com'}
if domain not in ALLOWED_DOMAINS:
raise PermissionError('Domain not allowed')Moderation Models
Provider moderation endpoints classify text for harmful categories. Run inputs and outputs through them as an extra safety layer.
result = client.moderations.create(input=text)
if result.results[0].flagged:
block()Limiting Autonomy
Cap how much an agent can do unattended: max iterations, max tool calls, spending limits, and human approval for high-impact actions. Bounded autonomy prevents runaway behavior.
agent = create_agent(llm, tools, max_iterations=8)Fail Safe, Not Open
When a guardrail is uncertain or a check errors, default to the safe choice — refuse or escalate — rather than letting the action through. A blocked safe request is better than an executed harmful one.
Logging and Review
Log every guardrail trigger. Reviewing these reveals attack patterns and false positives, letting you tune rules over time without weakening safety.
Quick Check
Test your guardrails knowledge.
Recap
You learned to engineer safe agent behavior:
- Add input and output guardrails
- Detect prompt injection and validate structured output
- Constrain tool access with allowlists and scoping
- Use moderation, limit autonomy, and fail safe
- Log and review every trigger
Guardrails turn ethical intent into enforced, trustworthy agents.
用 AI 导师学习 AI Agents with LangChain & Autonomous Workflows — 免费
在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。
- 课程
- 12
- 课程
- 50
常见问题解答
「护栏与安全的代理行为」课时是免费的吗?
是的 — 「护栏与安全的代理行为」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 AI Agents with LangChain & Autonomous Workflows 课程的其余内容,请升级到 CoddyKit PRO。 AI Agents with LangChain & Autonomous Workflows 课程共包含 4 节课。
「护栏与安全的代理行为」这节课中我会学到什么?
为人工智能代理实施实用的安全护栏,包括输入和输出验证、内容过滤以及受限的工具访问,防止有害或意外的操作。 你通过在浏览器中直接运行的动手代码来练习 AI Agents with LangChain & Autonomous Workflows,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 AI Agents with LangChain & Autonomous Workflows 需要有经验吗?
无需任何先前经验。CoddyKit 上的 AI Agents with LangChain & Autonomous Workflows 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「护栏与安全的代理行为」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 AI Agents with LangChain & Autonomous Workflows 课中编写并运行代码吗?
能。每节 AI Agents with LangChain & Autonomous Workflows 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 人工智能代理中的伦理考量
- 偏见、公平与透明度
- 新兴趋势与研究
- 护栏与安全的代理行为