0Pricing
AI Agents with LangChain & Autonomous Workflows · Lekcja

Obsługa błędów i bezpieczne wykonywanie narzędzi

Zwiększaj odporność i bezpieczeństwo narzędzi agentów, łagodnie obsługując awarie, walidując dane wejściowe i ograniczając działania dozwolone narzędziom podczas autonomicznego wykonywania.

Obsługa błędów i bezpieczne wykonywanie narzędzi to bezpłatna lekcja AI Agents with LangChain & Autonomous Workflows na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej AI Agents with LangChain & Autonomous Workflows, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs AI Agents with LangChain & Autonomous Workflows zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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

Często zadawane pytania

Czy lekcja „Obsługa błędów i bezpieczne wykonywanie narzędzi” jest bezpłatna?

Tak — pełny tekst „Obsługa błędów i bezpieczne wykonywanie narzędzi” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu AI Agents with LangChain & Autonomous Workflows, przejdź na CoddyKit PRO. Kurs AI Agents with LangChain & Autonomous Workflows zawiera 4 lekcji w sumie.

Co nauczysz się w „Obsługa błędów i bezpieczne wykonywanie narzędzi”?

Zwiększaj odporność i bezpieczeństwo narzędzi agentów, łagodnie obsługując awarie, walidując dane wejściowe i ograniczając działania dozwolone narzędziom podczas autonomicznego wykonywania. Ćwiczysz AI Agents with LangChain & Autonomous Workflows z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć AI Agents with LangChain & Autonomous Workflows?

Nie wymagamy żadnego doświadczenia. AI Agents with LangChain & Autonomous Workflows w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.

Ile czasu zajmuje lekcja „Obsługa błędów i bezpieczne wykonywanie narzędzi”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji AI Agents with LangChain & Autonomous Workflows?

Tak. Każda lekcja AI Agents with LangChain & Autonomous Workflows zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Definiowanie i używanie tools
  2. Typy agentów i podejmowanie decyzji
  3. Wykorzystanie gotowych toolkitów
  4. Obsługa błędów i bezpieczne wykonywanie narzędzi
← Powrót do AI Agents with LangChain & Autonomous Workflows