How tool calling works
- Send a user message and tool definitions.
- Read the model’s response, which can contain text or one or more tool calls.
- Execute each returned call once in your application.
- Send a result for every call, using its call ID.
- Read the model’s next response. It can contain a final answer or more tool calls.
Define tools
A tool definition names a function, explains when to use it, and describes its arguments with JSON Schema. The calculator below defines anadd function with two numeric arguments.
Tool-writing tips
Describe units and allowed values in your schemas. Validate model-provided arguments and check permissions before running functions that access external systems.Serialize functions
Chat Completions nests a function definition underfunction. Responses puts name, description, and parameters directly on the tool object. Each procedure below uses the format for its API.
Call the model
You need a Baseten API key inBASETEN_API_KEY and the OpenAI Python SDK, installed with pip install openai.
- Chat Completions
- Responses
To execute a tool and return its result:Run the following script. It uses Model APIs by default. For an OpenAI-compatible dedicated deployment, set
BASETEN_BASE_URL to its production base URL and BASETEN_MODEL to the served model name. For more information, see Call your model.The first request requires a tool call. The follow-up returns every result and selects none so the model produces a final answer:6.28. The model’s final wording can vary. For more information, see the Chat Completions reference and Responses reference.
Control tool selection
Settool_choice to choose whether the model can call tools:
auto: The model can return text or tool calls.required: The model must return at least one tool call.none: The model returns text without calling a tool.
- Chat Completions
- Responses
To select a specific function:Configure the request’s
tool_choice field:Request fields
Parse and execute tool calls
Chat Completions returns calls inmessage.tool_calls; Responses returns function_call items in response.output. Arguments are JSON strings. Decode and validate them before executing the named function.
Full loop: send tool output back for a final answer
Return one result for every call ID. Keep the assistant message for Chat Completions, or all output items for Responses, together with those results. The complete procedures above execute each call once and reuse its result in the follow-up. For an agent that allows further tool calls, repeat this cycle with a bounded number of turns. Before executing calls or accepting final text, check for refusal and incomplete or failed generation. A response with no tool calls is a final answer only when generation completed successfully and answer text is present. For more information, see Inference errors.Practical tips
Use JSON Schema constraints for required fields and allowed values. Check model support before requesting parallel calls. For functions with side effects, use application-level idempotency so a retried conversation doesn’t repeat an operation.Related
- Chains: Orchestrate multi-step workflows.
- Web search: Let Baseten execute supported tools server-side.