When the agent invents tool args: strict schemas + one repair turn

I’ve been debugging coding/agent loops where the model calls tools with almost-right JSON: wrong enum casing, extra keys, numbers as strings. The tool throws, the model apologizes, and you burn a turn.

What reduced repair churn:

  • Prefer enums over free-string “mode” fields. If the runtime only accepts patch | create, put that in the schema; don’t describe it in prose alone.
  • Use additionalProperties false (or equivalent) so mystery keys fail fast.
  • Validate with the same JSON Schema the model saw, in your tool proxy, before side effects.
  • On validation failure, return a structured tool error: { "error": "schema_validation", "path": "$.mode", "expected": ["patch","create"], "got": "Patch" } — not a stack trace.
  • Allow exactly one automated repair turn: resend tools + the validation error as the tool result. If it fails again, stop and ask the user / escalate. Infinite repair loops hide prompt bugs.
  • Log schema-fail rate per tool name. A hot tool with >10% validation fails usually means the description contradicts the schema (I had “optional repo” in text and required in schema).

Made-up example: apply_diff failed 18/100 calls on path separators; after enum for path_style and normalizing in the proxy, failures dropped to 2/100 and those two were genuinely ambiguous.

Do you normalize in the proxy silently, or force the model to resubmit valid args so the transcript stays honest for evals?