การจัดการข้อผิดพลาดและความทนทาน
พัฒนากลยุทธ์ที่แข็งแกร่งสำหรับคาดการณ์ ดักจับ และจัดการข้อผิดพลาดในเวิร์กโฟลว์เอเจนต์อัตโนมัติอย่างเหมาะสม
การจัดการข้อผิดพลาดและความทนทาน เป็นบทเรียน AI Agents with LangChain & Autonomous Workflows ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน AI Agents with LangChain & Autonomous Workflows และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส AI Agents with LangChain & Autonomous Workflows มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Why Error Handling Matters
Autonomous agents perform complex tasks, often interacting with external services or making decisions based on potentially unreliable information. What happens when things go wrong?
Error handling is crucial for agents to be reliable and robust. It ensures your agent can recover from unexpected issues, prevent crashes, and maintain a consistent user experience.
Common Agent Workflow Errors
Agents can encounter various types of errors during their operation:
- API Failures: Large Language Model (LLM) providers or external tools might experience downtime, rate limits, or authentication issues.
- Tool Execution Issues: A custom or pre-built tool might receive bad input, fail to execute correctly, or return an unexpected format.
- LLM Misinterpretations: The LLM might generate unparseable output, hallucinate, or respond in a way the agent's logic cannot handle.
- Network Issues: Connectivity problems to external services can prevent agents from fetching data or calling APIs.
Catching Errors with Try-Except
In Python, the try-except block is your fundamental mechanism to catch errors. It allows you to attempt an operation and gracefully handle specific exceptions if they occur.
This prevents your entire agent workflow from crashing due to a single failure point.
def perform_risky_operation(value):
try:
# Attempt a potentially failing operation
result = 100 / value
print(f"Operation successful! Result: {result}")
except ZeroDivisionError:
# Handle specific error: division by zero
print("Error: Cannot divide by zero!")
except TypeError as e:
# Handle specific error: incorrect type
print(f"Error: Invalid input type - {e}")
except Exception as e:
# Catch any other unexpected errors
print(f"An unexpected error occurred: {e}")
if __name__ == "__main__":
perform_risky_operation(20) # Works fine
perform_risky_operation(0) # Catches ZeroDivisionError
perform_risky_operation("abc") # Catches TypeErrorHandling LLM API Errors
When your agent interacts with an LLM (e.g., OpenAI, Anthropic), API calls can fail. These failures could be due to rate limits, invalid API keys, or temporary service outages.
It's vital to catch these specific API errors to implement recovery strategies or inform the user.
import random
# Simulate a custom API error for demonstration
class LLMAPIError(Exception):
pass
def call_llm_service(prompt):
# Simulate a 25% chance of API failure
if random.random() < 0.25:
raise LLMAPIError("LLM API call failed: Service unavailable.")
return f"LLM response to '{prompt}': Here's your answer."
if __name__ == "__main__":
print("--- Attempt 1 ---")
try:
response = call_llm_service("Summarize the news.")
print(response)
except LLMAPIError as e:
print(f"Caught LLM API Error: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
print("\n--- Attempt 2 ---")
try:
response = call_llm_service("Write a haiku.")
print(response)
except LLMAPIError as e:
print(f"Caught LLM API Error: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")Robust Tool Execution Errors
Agents use tools to extend their capabilities (e.g., searching the web, executing code). A tool might fail if its external dependency is down, it receives invalid input, or encounters an internal error.
By anticipating and handling these tool-specific errors, your agent can decide on alternative actions or provide helpful feedback.
import random
# Simulate a custom tool execution error
class WebSearchToolError(Exception):
pass
def perform_web_search(query):
# Simulate a 30% chance of tool failure
if random.random() < 0.3:
raise WebSearchToolError(f"Web search for '{query}' failed due to network issues.")
return f"Web search results for: {query}"
if __name__ == "__main__":
print("--- Search 1 ---")
try:
result = perform_web_search("current weather")
print(result)
except WebSearchToolError as e:
print(f"Caught Web Search Tool Error: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
print("\n--- Search 2 ---")
try:
result = perform_web_search("AI agent frameworks")
print(result)
except WebSearchToolError as e:
print(f"Caught Web Search Tool Error: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")Implementing Retries with Backoff
Many errors are transient, meaning they are temporary and might resolve themselves. For these, a simple retry mechanism can be highly effective. Exponential backoff is a common strategy where the delay between retries increases with each attempt.
This prevents overwhelming a failing service and gives it time to recover.
import time
import random
def retry_with_backoff(func, max_retries=3):
for attempt in range(max_retries):
try:
return func() # Try to execute the function
except Exception as e:
print(f"Attempt {attempt + 1} failed: {e}")
if attempt < max_retries - 1:
# Calculate exponential backoff delay
wait_time = 2 ** attempt
print(f"Retrying in {wait_time} seconds...")
time.sleep(wait_time)
else:
# Re-raise error if max retries reached
raise ValueError("Operation failed after multiple retries.")
def unreliable_action():
# Simulate an action that fails 60% of the time
if random.random() < 0.6:
raise ConnectionError("Temporary network issue.")
return "Action completed successfully!"
if __name__ == "__main__":
try:
result = retry_with_backoff(unreliable_action)
print(result)
except ValueError as e:
print(f"Final result: {e}")LangChain Callbacks for Errors
LangChain's Callback system provides a powerful way to inject custom logic into various stages of an agent or chain's execution, including error handling.
- You can define functions that run specifically when an error occurs (e.g.,
on_tool_error,on_chain_error). - This allows for centralized logging, monitoring, or triggering alerts when issues arise.
- Callbacks can capture detailed context about the error, aiding in debugging complex agent workflows.
Graceful Degradation Strategies
Not all errors are recoverable. Sometimes, an agent needs to degrade gracefully rather than completely failing. This means providing a reduced but still functional experience.
- Fallback Mechanisms: If a primary, complex tool fails, switch to a simpler, more reliable alternative (e.g., if a specialized database search fails, fall back to a general web search).
- Partial Completion: Complete as much of the task as possible and inform the user about the limitations or incomplete parts.
- Informative User Messages: Clearly communicate to the user when a specific feature or capability is temporarily unavailable due to an underlying issue.
Logging Errors for Observability
Effective logging is crucial for understanding why an autonomous agent failed, especially in production environments. Good logs provide observability into your agent's internal workings.
- What to Log: Include error messages, stack traces, relevant input parameters, the agent's current state, and timestamps.
- Where to Log: Send logs to a centralized logging system (e.g., ELK stack, Splunk, cloud logging services) for easy analysis and alerting.
- Why it's Important: Helps identify recurring issues, debug complex interactions, and monitor the overall health and reliability of your agent system.
Error Handling Check
Let's test your understanding of error handling and resilience in autonomous agent workflows.
Recap: Building Resilient Agents
We've explored how to make autonomous agent workflows more robust by handling errors effectively.
- We covered using
try-exceptblocks for basic error catching and managing specific types of exceptions. - We discussed specific strategies for handling LLM API and tool execution errors.
- We learned about implementing retries with exponential backoff to overcome transient issues.
- Finally, we touched upon graceful degradation for unrecoverable errors and the importance of logging for observability and debugging.
By applying these techniques, your agents can better withstand unexpected issues and provide a more reliable and stable user experience.
คำถามที่พบบ่อย
บทเรียน “การจัดการข้อผิดพลาดและความทนทาน” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การจัดการข้อผิดพลาดและความทนทาน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ 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 ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “การจัดการข้อผิดพลาดและความทนทาน” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน AI Agents with LangChain & Autonomous Workflows นี้ได้ไหม
ได้ บทเรียน AI Agents with LangChain & Autonomous Workflows ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การออกแบบเวิร์กโฟลว์ที่ซับซ้อน
- การทำงานของเอเจนต์แบบอะซิงโครนัส
- การจัดการข้อผิดพลาดและความทนทาน
- การอนุมัติโดยมนุษย์ในกระบวนการ