Course contentsShow
← AI Engineering
Lesson 578 of 1,88614. Function Calling and Tool UseFree lesson

Error Messages for LLMs

Craft structured error messages that help the model understand what went wrong and how to fix its next attempt.

Error Messages for LLMs

What you'll learn: How to write error messages that guide LLMs to self-correct when function calling goes wrong.

When an LLM makes a mistake calling a function—wrong argument type, missing required field, invalid value—your error message becomes part of the conversation history. The model reads it and tries again. Generic errors like "Invalid input" waste tokens and leave the model guessing. Structured, actionable error messages dramatically improve second-attempt success rates.

Why LLM-Friendly Error Messages Matter

Think of it like giving feedback to a colleague who doesn't see your screen. Instead of saying "That didn't work," you'd say "The date parameter needs format YYYY-MM-DD, but you provided MM/DD/YYYY."

LLMs benefit from the same clarity:

  • What went wrong — which parameter or constraint failed
  • Why it failed — the specific validation rule or type mismatch
  • How to fix it — the expected format, range, or valid options

Crafting Structured Error Messages

When a function argument validation fails (concept 576), return error messages that reference the function schema (concepts 555-559):

Error: Parameter 'temperature' validation failed.
Expected: number between 0 and 1
Received: 1.5
Please retry with a value in the valid range.

For missing required fields:

Error: Missing required parameter 'location'.
This function requires both 'location' (string) and 'units' (enum: celsius|fahrenheit).

This structure gives the model everything it needs to construct a valid retry without human intervention—key for graceful degradation (concept 577).

Key Takeaway: Error messages for LLMs should be structured, specific, and actionable—clearly stating what failed, why, and how to fix it so the model can self-correct on the next attempt.