Gestione degli errori ed esecuzione sicura degli strumenti
Rendete gli strumenti degli agenti resilienti e sicuri gestendo correttamente i guasti, validando gli input e limitando ciò che gli strumenti possono fare durante l’esecuzione autonoma.
Gestione degli errori ed esecuzione sicura degli strumenti è una lezione AI Agents with LangChain & Autonomous Workflows gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento AI Agents with LangChain & Autonomous Workflows, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso AI Agents with LangChain & Autonomous Workflows include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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
Domande Frequenti
La lezione «Gestione degli errori ed esecuzione sicura degli strumenti» è gratuita?
Sì — il testo completo di «Gestione degli errori ed esecuzione sicura degli strumenti» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso AI Agents with LangChain & Autonomous Workflows, passa a CoddyKit PRO. Il corso AI Agents with LangChain & Autonomous Workflows include 4 lezioni in totale.
Cosa imparerò in «Gestione degli errori ed esecuzione sicura degli strumenti»?
Rendete gli strumenti degli agenti resilienti e sicuri gestendo correttamente i guasti, validando gli input e limitando ciò che gli strumenti possono fare durante l’esecuzione autonoma. Eserciti AI Agents with LangChain & Autonomous Workflows con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare AI Agents with LangChain & Autonomous Workflows?
Non è richiesta alcuna esperienza precedente. AI Agents with LangChain & Autonomous Workflows su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Gestione degli errori ed esecuzione sicura degli strumenti»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione AI Agents with LangChain & Autonomous Workflows?
Sì. Ogni lezione AI Agents with LangChain & Autonomous Workflows include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Definire e utilizzare gli strumenti
- Tipi di agenti e processo decisionale
- Sfruttare i toolkit preconfigurati
- Gestione degli errori ed esecuzione sicura degli strumenti