# Validate tool arguments

Check every tool argument with Zod before running anything. If it's invalid, return a clear error so the model can retry.

AI developers · Part 09: Production · Card 2 of 13 · Last updated 2026-10-13

## Analogy

The same input validation you put on a public API route, because the model is just another untrusted client.

## Code example

`validate.ts`

```typescript
const Args = z.object({ prId: z.number().int().positive() });

const parsed = Args.safeParse(JSON.parse(call.function.arguments));
if (!parsed.success) {
  return { error: "Invalid prId, must be a positive integer" }; // model can retry
}
```

## In an agent

Returning the error as a tool result lets the agent fix its own mistake on the next step.

## Practice

### Flashcards

- **Interview: does the model run your tool?** No. It returns the tool name and arguments; your code validates, runs it, and sends the result back as a tool message.
- **Interview: four guardrails against harmful actions?** Step limits, Zod validation of arguments, least-privilege tools with approval for writes, and treating tool output as untrusted.

### Interview questions

**How does function / tool calling work?**

- Describe each tool with a name, a description and a JSON schema for its parameters.
- The model returns which tool to call and with what arguments. It never executes code itself.
- Your code validates the arguments, runs the function, and sends the result back as a tool message.
- The model then continues with the result in context.

## Questions people ask

### How do I validate tool arguments in an agent?

Parse the arguments string and check it with a Zod schema using safeParse before running anything. If it fails, skip the tool and return a clear error as the tool result, for example invalid prId, must be a positive integer. The model usually corrects itself on the next step.

### Why treat the model like an untrusted client?

Because its arguments can be wrong, malformed, or shaped by text an attacker planted in a document or web page. You would never trust a public API request without validation; tool calls deserve the same care. Validation stops bad input before it reaches your database or another service.

## Related concepts

- [Guardrails](https://studyvisually.com/learn/tech/ai-developers/production/guardrails)
- [Least privilege](https://studyvisually.com/learn/tech/ai-developers/production/least-privilege)
- [The tool_calls reply](https://studyvisually.com/learn/tech/ai-developers/tool-calling/tool-calls-response)
- [The tool message](https://studyvisually.com/learn/tech/ai-developers/tool-calling/tool-message)

Web page: https://studyvisually.com/learn/tech/ai-developers/production/validate-tool-arguments
Study this card in the app: https://studyvisually.com/app/learn?category=ai-developers&card=validate-tool-arguments
