When the model can do more than answer

Tool calling and functions for AI agents

A tool call is a proposal from the model to the application. Your code must validate the arguments, check permission, execute and return the result.

The five-step tool loop

  1. The application supplies names, descriptions and argument schemas for allowed tools.
  2. The model proposes zero, one or more calls.
  3. The application validates the tool, arguments, identity and permission.
  4. Code executes the tool and returns a structured result.
  5. The model uses that result in its next answer or proposes another call.
Important: The model has not executed anything merely because it produced a function call. The application controls whether and how it runs.

Write the description as a contract

Name: get_customer
Purpose: Retrieve one existing customer when an exact customer ID is known.

Use when:
- the user asks about one specific customer

Do not use when:
- customer ID is missing; ask for clarification
- the task requires free-text search; use search_customers

Arguments:
- customer_id: string, required, must match ^C-[0-9]{6}$

Returns:
- id, name, status and last-updated timestamp

Errors:
- NOT_FOUND means no match; do not guess another ID.

Keep the tool surface small

Overlapping tools and vague parameters increase misrouting. Use clear names, strict schemas and the smallest useful result. Separate read from write tools, and put approvals before external or irreversible actions.

Evaluate the route, not only the answer

Test whether the agent chooses the right tool, sends valid arguments, handles empty results and uses the returned data correctly. Continue with human approval and MCP.

Sources