Designing Reliable LLM Tool Schemas: Strict Validation, Idempotency, and Recovery
A tool schema is a user interface for an LLM. Just like confusing API docs cause human developers to send malformed payloads, ambiguous parameter names and cryptic 400 Bad Request errors cause agents to hallucinate arguments and spiral into retry loops.
Principles of High-Accuracy Tool Design
| Principle | Weak Tool Design | Strong Tool Design |
|---|---|---|
| Granularity | 5 CRUD calls to inspect one order | 1 cohesive get_order_summary returning joined context |
| Parameter Types | Free-text date: string | Explicit ISO YYYY-MM-DD with format note and default |
| Identifiers | Requires raw internal UUIDs the user doesn't know | Accepts search query or returns compact human-readable list |
| Error Messages | "Invalid input" | "endDate must be on or after startDate; received 2026-05-01 < 2026-06-01" |
| Output Size | Dumps 80 KB raw JSON rows | Paginated, projected summary with explicit totalCount and truncated flag |
Returning Actionable Recovery Errors
When schema validation fails, return a concise, self-correcting error payload to the model rather than crashing the entire run. Models recover remarkably well when told exactly which field violated which constraint.
import { z } from "zod";
export const DateRangeQuerySchema = z
.object({
startDate: z.string().regex(/^\d{4}-\d{2}-\d{2}$/, "Use YYYY-MM-DD format"),
endDate: z.string().regex(/^\d{4}-\d{2}-\d{2}$/, "Use YYYY-MM-DD format"),
limit: z.number().int().min(1).max(50).default(20),
})
.refine(v => v.startDate <= v.endDate, {
message: "startDate must be earlier than or equal to endDate",
});
Idempotency Keys for Mutating Tools
Models occasionally emit duplicate tool calls after a timeout or retry. Any mutating tool (creating a draft, tagging a customer, scheduling a follow-up) should enforce a deterministic idempotency key derived from (runId, toolName, normalizedArgs) so repeated invocations return the previously created resource instead of duplicating side effects.
Next: standardize how agents discover and invoke external tools in Model Context Protocol (MCP) Fundamentals.