ガードレールと安全なエージェント動作
入力・出力の検証、コンテンツフィルタリング、ツールアクセスの制限といった実践的な安全策をAIエージェントに実装し、有害または意図しない操作を防ぎます。
「ガードレールと安全なエージェント動作」はCoddyKit上の無料AI Agents with LangChain & Autonomous Workflowsレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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 — 無料
ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。
- コース
- 12
- レッスン
- 50
よくある質問
「ガードレールと安全なエージェント動作」レッスンは無料ですか?
はい。「ガードレールと安全なエージェント動作」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、AI Agents with LangChain & Autonomous Workflowsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 AI Agents with LangChain & Autonomous Workflowsコースには全4レッスンが含まれています。
「ガードレールと安全なエージェント動作」で何を学びますか?
入力・出力の検証、コンテンツフィルタリング、ツールアクセスの制限といった実践的な安全策をAIエージェントに実装し、有害または意図しない操作を防ぎます。 ブラウザで直接実行するハンズオンコードでAI Agents with LangChain & Autonomous Workflowsを演習し、24時間対応の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フィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- AIエージェントにおける倫理的考察
- バイアス、公平性、透明性
- 最新の動向と研究
- ガードレールと安全なエージェント動作