การจัดการข้อผิดพลาดและการเรียกใช้เครื่องมืออย่างปลอดภัย
ทำให้เครื่องมือของเอเจนต์ยืดหยุ่นและปลอดภัยด้วยการจัดการความล้มเหลวอย่างเหมาะสม ตรวจสอบข้อมูลนำเข้า และจำกัดสิ่งที่เครื่องมือทำได้ระหว่างการทำงานอัตโนมัติ
การจัดการข้อผิดพลาดและการเรียกใช้เครื่องมืออย่างปลอดภัย เป็นบทเรียน AI Agents with LangChain & Autonomous Workflows ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน AI Agents with LangChain & Autonomous Workflows และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส AI Agents with LangChain & Autonomous Workflows มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Tools Will Fail
Agents call external tools: APIs, databases, shells. These time out, return errors, or behave unexpectedly. An agent that crashes on the first failure is useless in production.
This lesson covers making tool execution safe and resilient.
Returning Errors as Observations
Instead of throwing, a tool should return the error as a message the agent can read. This lets the agent reason about the failure and try another approach.
def search(q):
try:
return api.search(q)
except Exception as e:
return 'ERROR: search failed: ' + str(e)Validating Tool Inputs
LLMs sometimes pass nonsense arguments. Validate inputs before acting, so a malformed query never reaches a real system.
def get_user(user_id):
if not str(user_id).isdigit():
return 'ERROR: user_id must be numeric'
return db.fetch(user_id)Timeouts and Retries
Wrap slow tools with a timeout and retry transient failures a bounded number of times. Without limits, an agent can hang or loop forever.
for attempt in range(3):
try:
return call(timeout=5)
except Timeout:
continue
return 'ERROR: timed out after 3 attempts'The Danger of Powerful Tools
A tool that runs shell commands or executes SQL can do real damage if the agent is tricked or confused. Power must be paired with constraints.
Least Privilege
Give each tool only the access it needs. A read tool should not be able to write. A query tool should use a read-only database role.
# read-only DB connection for the query tool
conn = connect(user='reader', readonly=True)Allowlists over Blocklists
Trying to block every dangerous action is a losing game. Instead, permit only an explicit set of safe operations and reject everything else by default.
ALLOWED = {'read_file', 'list_dir', 'search'}
if action not in ALLOWED:
return 'ERROR: action not permitted'Human-in-the-Loop Approval
For high-impact actions like sending money or deleting data, pause and require human confirmation before executing. The agent proposes; a person approves.
if action.is_destructive:
return await request_human_approval(action)Preventing Infinite Loops
Agents can get stuck retrying the same failing tool. Cap the number of steps and detect repeated identical actions, stopping with a clear message instead of burning tokens forever.
agent = initialize_agent(tools, llm, max_iterations=8)Logging for Auditing
Record every tool call with its inputs, outputs, and outcome. This audit trail is essential for debugging agent behavior and for catching unsafe actions after the fact.
A Safe Tool Workflow
Putting it together:
- Return errors as observations, not crashes
- Validate inputs and bound timeouts/retries
- Apply least privilege and allowlists
- Require approval for destructive actions
- Cap iterations and log every call
Quick Check
Test your understanding of safe tool execution.
Recap
You learned to make agent tools resilient and safe.
- Surface errors as observations the agent can handle
- Validate inputs and bound timeouts, retries, and iterations
- Apply least privilege and allowlists
- Require approval for destructive actions and log everything
คำถามที่พบบ่อย
บทเรียน “การจัดการข้อผิดพลาดและการเรียกใช้เครื่องมืออย่างปลอดภัย” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การจัดการข้อผิดพลาดและการเรียกใช้เครื่องมืออย่างปลอดภัย” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส AI Agents with LangChain & Autonomous Workflows ให้อัปเกรดเป็น CoddyKit PRO คอร์ส AI Agents with LangChain & Autonomous Workflows มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การจัดการข้อผิดพลาดและการเรียกใช้เครื่องมืออย่างปลอดภัย”
ทำให้เครื่องมือของเอเจนต์ยืดหยุ่นและปลอดภัยด้วยการจัดการความล้มเหลวอย่างเหมาะสม ตรวจสอบข้อมูลนำเข้า และจำกัดสิ่งที่เครื่องมือทำได้ระหว่างการทำงานอัตโนมัติ คุณปฏิบัติ AI Agents with LangChain & Autonomous Workflows ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน AI Agents with LangChain & Autonomous Workflows หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน AI Agents with LangChain & Autonomous Workflows บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การจัดการข้อผิดพลาดและการเรียกใช้เครื่องมืออย่างปลอดภัย” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน AI Agents with LangChain & Autonomous Workflows นี้ได้ไหม
ได้ บทเรียน AI Agents with LangChain & Autonomous Workflows ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การกำหนดและใช้เครื่องมือ
- ประเภทของเอเจนต์และการตัดสินใจ
- การใช้ประโยชน์จากชุดเครื่องมือสำเร็จรูป
- การจัดการข้อผิดพลาดและการเรียกใช้เครื่องมืออย่างปลอดภัย