How Tool Use Works
NeosantaraAI’s tool use functionality follows a common pattern, similar to OpenAI’s function calling, where you provide the model with descriptions of available tools, and the model decides if and how to use them. Integrate client-side tools with NeosantaraAI in these steps:1
Provide NeosantaraAI with tools and a user prompt
- Define tools with names, descriptions, and input schemas (parameters) in your API request within the
tools
parameter. - Include a user prompt that might require these tools, e.g., “What’s the weather in Jakarta?”
2
NeosantaraAI decides to use a tool
- The model assesses if any tools can help with the user’s query.
- If yes, the model constructs a
tool_calls
object within its response, containing the name of the tool to be called and the arguments (input) for that tool. - The API response will have a
finish_reason
oftool_calls
.
3
Execute the tool and return results
- Your application extracts the tool name and input from NeosantaraAI’s
tool_calls
response. - Your application then executes the actual tool code on your system.
- Return the results to the model in a new
user
message with atool
role, containing thetool_call_id
and thecontent
of the tool’s output.
4
NeosantaraAI uses tool result to formulate a response
- NeosantaraAI analyzes the tool results you provide to craft its final, natural language response to the original user prompt.
Tool Use Examples
Here are a few code examples demonstrating various tool use patterns and techniques. The examples use simple tools for clarity.Single Tool Example
Single Tool Example
get_weather
function with the provided input, and return the result in a new user
message:Parallel Tool Use
Parallel Tool Use
NeosantaraAI can call multiple tools in parallel within a single response, which is useful for tasks that require multiple independent operations. When using parallel tools, all
tool_calls
blocks are included in a single assistant message, and all corresponding tool
results must be provided in the subsequent user message, each with its tool_call_id
.Missing Information
Missing Information
If the user’s prompt doesn’t include enough information to fill all the required parameters for a tool, NeosantaraAI is designed to recognize that a parameter is missing and ask for it.For example, using the
get_weather
tool, if you ask “What’s the weather?” without specifying a location, the model is likely to respond with a clarifying question instead of making a tool call.Sequential Tools
Sequential Tools
Some tasks may require calling multiple tools in sequence, using the output of one tool as the input to another. In such a case, NeosantaraAI will call one tool at a time.Here’s an example of using a The full conversation flow would involve multiple API calls:
get_location_from_ip
tool to get the user’s location based on their IP, then passing that location to the get_weather
tool:- User asks: “What’s the weather like where I am?”
- AI responds (Tool Call 1): Calls
get_location_from_ip
. - Your code executes tool: Gets location (e.g., “Bandung, West Java”).
- Your code sends result to AI: Sends
tool
message with location. - AI responds (Tool Call 2): Calls
get_weather
with “Bandung, West Java”. - Your code executes tool: Gets weather data (e.g., “25°C, Sunny”).
- Your code sends result to AI: Sends
tool
message with weather. - AI responds (Final Answer): “The weather in Bandung, West Java is currently 25°C and sunny.”
JSON Mode
JSON Mode
You can use tools to instruct NeosantaraAI to produce JSON output that follows a specific schema, even if you don’t intend to execute the output through a tool or function. This is often used for structured data extraction or generation.When using tools in this way:NeosantaraAI will return a response containing the structured JSON output within a
- You typically provide a single tool.
- You should set
tool_choice
to{"type": "function", "function": {"name": "your_tool_name"}}
to explicitly instruct the model to use that tool. - The
function
’sparameters
define the exact JSON schema the model should adhere to.
record_summary
tool to describe an image following a particular JSON format. Note that this requires a model with vision capabilities (e.g., vision-emas-2045
).tool_calls
block:Pricing
Tool use requests are priced based on:- The total number of input tokens sent to the model (including the tokens from tool definitions in the
tools
parameter and tool call/result messages). - The number of output tokens generated (including tool calls generated by the model).
- Additional charges may apply for specific capabilities (e.g., vision processing for image inputs).
- The
tools
array in API requests (tool names, descriptions, and parameter schemas). tool_calls
generated by the model in assistant messages.tool
messages (containingtool_call_id
andcontent
) sent by your application.
usage
metrics.