AI developers · Part 03
Tool calling
Describe functions with a JSON schema and let the model ask your code to run them.
Last updated
- What a tool isA tool is a function in your code that you describe to the model. The model can ask you to run it, but it never runs anything itself.
- Function schemaYou describe each tool with a name, a description and a JSON schema for its parameters.
- Tool descriptionThe description is the model's only clue about when to use a tool. Vague descriptions lead to wrong tool choices.
- The tool_calls replyWhen the model wants a tool, it replies with tool_calls (a function name plus JSON arguments) and its content is null.
- Arguments are a stringfunction.arguments is a JSON string, not an object. You must JSON.parse it, and it can be malformed or wrong.
- The tool messageAfter running the tool, you append the assistant's tool-call message and then a tool message with the result, linked by tool_call_id.
- Calling the model againTool calling takes two model calls: one where it asks for the tool, and another (with the result added) where it writes the answer.
- tool_choiceA parameter that controls tool use: "auto" (model decides), "none" (no tools), "required" (must call one), or a specific function.
- Parallel tool callsThe model can request several tool calls in one reply. Run them all, then send back one tool message per call.
- Tools give data and actionsTools do two jobs: fetch data the model doesn't have (an order status) and take actions it can't take (create a Jira ticket).