Tool calling
A safe, general pattern for connecting model-proposed actions to application code.
Working scaffold: Tool-call schemas, request fields, and supported models are product-specific. Verify them before adding code samples.
The basic cycle
- Describe a small set of tools and their input schemas to the model.
- Inspect the response to determine whether it contains text, a tool request, or another supported result.
- Validate the requested tool name and every input field in application code.
- Apply authorization and business rules, then execute the approved action.
- Return the tool result using the documented protocol and continue the request cycle if needed.
The model proposes an action; the application remains responsible for deciding whether that action is allowed and for executing it.
Safety and reliability
Use narrowly scoped tools, validate arguments against a schema, require confirmation for consequential actions, and make operations idempotent where possible. Treat tool output as untrusted input. Set timeouts and define how tool errors are represented to the model and the user.
Document the actual maximum calls, response format, supported models, and streaming behavior only after they have been tested.