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 underchoices[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:- Send user message with
toolsarray defining available functions - Model responds with
tool_callsif it needs to use a tool - Execute the tool in your application code
- Add the assistant message (with
tool_calls) and atoolmessage (with the result) to conversation history - Send updated history back to the API
- 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 tohttps://llm-gateway.assemblyai.com/v1/chat/completions with the following parameters:
Request parameters
Message object
Tool object
Tool choice object
Thetool_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 afunction.nameto 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 atool_calls array:
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. Addpost_processing_steps to your request to automatically repair these before they reach your application: