Skip to content

Designing Reliable LLM Tool Schemas: Strict Validation, Idempotency, and Recovery

2 min read

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

PrincipleWeak Tool DesignStrong Tool Design
Granularity5 CRUD calls to inspect one order1 cohesive get_order_summary returning joined context
Parameter TypesFree-text date: stringExplicit ISO YYYY-MM-DD with format note and default
IdentifiersRequires raw internal UUIDs the user doesn't knowAccepts 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 SizeDumps 80 KB raw JSON rowsPaginated, 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.

ts
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.

Was this helpful?