Intermediate LevelAI, LLMs, Embeddings & MCP
How do AI Agents handle Tool Calling errors and malformed JSON parameters?
❓ Interview Prompt: Explain tool error feedback loops, malformed JSON recovery, and multi-turn error correction.
When an LLM generates a tool call request, the function call may fail due to malformed JSON arguments, missing required parameters, or external API runtime errors. Agent Tool Error Recovery handles these gracefully.
Recovery Pattern:
1
Catch tool execution exception or JSON validation error.
2
Formulate a
tool role message containing the exact error trace: { role: "tool", name: "query_db", content: "Error: Unknown column 'usr_name' in field list" }
3
Feed the error message back to the LLM. The model reads the error and generates a corrected tool call request!
Code Implementation Example
typescript
async function executeToolWithRecovery(toolCall: any, messages: any[]): Promise<any> {
try {
const result = await runTool(toolCall.name, toolCall.args);
return { role: 'tool', tool_call_id: toolCall.id, content: JSON.stringify(result) };
} catch (err: any) {
// Append error trace back to model messages for self-correction
return {
role: 'tool',
tool_call_id: toolCall.id,
content: "Tool Execution Failed: " + err.message + ". Please fix the arguments and retry."
};
}
}💡Tool execution error recovery returning exception details back to the LLM.
🎯 Interview Key Takeaway
Feed tool execution error tracebacks back into the messages payload so the LLM can self-correct malformed parameters.
#Tool Calling#Error Recovery#Self-Correction#Agents#Intermediate
Back to all AI, LLMs, Embeddings & MCP Questions→