Skip to main content
Function calling lets a model select a function and produce its arguments. Your application executes the function and returns its result to the model for a final answer. For tools that Baseten executes within a request, use server-side web search.

How tool calling works

  1. Send a user message and tool definitions.
  2. Read the model’s response, which can contain text or one or more tool calls.
  3. Execute each returned call once in your application.
  4. Send a result for every call, using its call ID.
  5. Read the model’s next response. It can contain a final answer or more tool calls.
Check your model’s tool support before starting. For dedicated deployments, check the engine or server configuration.

Define tools

A tool definition names a function, explains when to use it, and describes its arguments with JSON Schema. The calculator below defines an add 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 under function. 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 in BASETEN_API_KEY and the OpenAI Python SDK, installed with pip install openai.
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:
The numeric tool result is 6.28. The model’s final wording can vary. For more information, see the Chat Completions reference and Responses reference.

Control tool selection

Set tool_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.
To force a named function, use the shape for your API:
To select a specific function:Configure the request’s tool_choice field:
Request fields

Parse and execute tool calls

Chat Completions returns calls in message.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.
  • Chains: Orchestrate multi-step workflows.
  • Web search: Let Baseten execute supported tools server-side.