Skip to main content

Overview

Tool calling allows you to define functions that the model can call to access external data or perform actions. This enables the model to interact with databases, APIs, and other external systems. Tool call objects follow an OpenAI-compatible structure nested under choices[i].message.tool_calls, so the same parser works across Claude, OpenAI, and Gemini. finish_reason is passed through provider-native (tool_use / end_turn for Claude, tool_calls / stop for OpenAI), so handle both families when branching on it.

Defining tools

Define functions the model can call to access external data or functionality:

Handling tool calls

Execute tools and send results back to continue the conversation:

Tool call flow

The complete tool calling process:
  1. Send user message with tools array defining available functions
  2. Model responds with tool_calls if it needs to use a tool
  3. Execute the tool in your application code
  4. Add the assistant message (with tool_calls) and a tool message (with the result) to conversation history
  5. Send updated history back to the API
  6. Model incorporates results and either calls more tools or provides final answer

Message types

When using tools, structure your conversation history with these message types to track the complete interaction flow:
  • user - Messages from the user
  • assistant - Messages from the AI model
  • system - System instructions or context
  • tool - Results from executing a tool call

API reference

Request

The LLM Gateway accepts POST requests to https://llm-gateway.assemblyai.com/v1/chat/completions with the following parameters:

Request parameters

Message object

Tool object

Tool choice object

The tool_choice parameter can be:
  • "none" - The model will not call any tools
  • "auto" - The model can choose whether to call tools
  • An object with type: "function" and a function.name to force calling a specific function

Response

The API returns a JSON response. When the model wants to call a tool:

Response fields

Tool call object

When the model wants to call a tool, the response includes a tool_calls array:
Models occasionally return malformed JSON in tool_calls[i].function.arguments. Add post_processing_steps: [{"type": "json-repair"}] to your request to automatically repair common JSON errors. See Post-processing.

Error response

If an error occurs, the API returns an error response:

Common error codes

Handling malformed tool call arguments

LLMs occasionally return malformed JSON in tool call arguments. Add post_processing_steps to your request to automatically repair these before they reach your application:
See Post-processing for details.