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
- The application supplies names, descriptions and argument schemas for allowed tools.
- The model proposes zero, one or more calls.
- The application validates the tool, arguments, identity and permission.
- Code executes the tool and returns a structured result.
- 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
- OpenAI: Function calling — flow, schemas and tool choice
- Anthropic: Writing effective tools for agents — names, descriptions, responses and evaluation
- AWS: Tool-based agents — tool metadata and function calls