Choosing a Model
Generally, models withfunction_calling capabilities are suitable for tool use. For complex tools and ambiguous queries, models like nusantara-base, gpt-4o-mini, gemini-1.5-pro, or command-r-plus are recommended as they handle tool selection and parameter inference more robustly.
For more straightforward tools, models like bahasa-llm or gemini-1.5-flash can be effective.
Specifying Tools
Tools (functions) are specified in thetools top-level parameter of your API request. Each tool definition includes:
| Parameter | Description |
|---|---|
type | Must be "function". |
function | An object defining the functionâs metadata, including its name, description, and parameters. |
name | (within function): The name of the tool. Must match the regex ^[a-zA-Z0-9_-]{1,64}$. |
description | (within function): A detailed plaintext description of what the tool does, when it should be used, and how it behaves. This is crucial for the model to understand the toolâs purpose. |
parameters | (within function): A JSON Schema object defining the expected parameters for the tool. This schema describes the input the tool expects. |
Example Simple Tool Definition
Example Simple Tool Definition
get_weather, expects an input object with a required location string and an optional unit string that must be either âcelsiusâ or âfahrenheitâ.Best Practices for Tool Definitions
To get the best performance out of NeosantaraAI when using tools, follow these guidelines:- Provide extremely detailed descriptions. This is by far the most important factor in tool performance. Your descriptions should explain every detail about the tool, including:
- What the tool does.
- When it should be used (and when it shouldnât).
- What each parameter means and how it affects the toolâs behavior.
- Any important caveats or limitations, such as what information the tool does not return if the tool name is unclear. The more context you can give the model about your tools, the better it will be at deciding when and how to use them. Aim for at least 3-4 sentences per tool description, more if the tool is complex.
- Prioritize descriptions over examples. While you can include examples of how to use a tool in its description or in the accompanying prompt, this is less important than having a clear and comprehensive explanation of the toolâs purpose and parameters. Only add examples after youâve fully fleshed out the description.
Example of a Good Tool Description
Example of a Good Tool Description
Example Poor Tool Description
Example Poor Tool Description
ticker parameter means. The poor description is too brief and leaves the model with many open questions about the toolâs behavior and usage.
Controlling Modelâs Output
Forcing Tool Use
In some cases, you may want NeosantaraAI to use a specific tool to answer the userâs question, even if the model thinks it can provide an answer without using a tool. You can do this by specifying the tool in thetool_choice field.
The tool_choice parameter offers four possible options:
auto: (Default whentoolsare provided) Allows the model to decide whether to call any provided tools or not.any: Tells the model that it must use one of the provided tools, but doesnât force a particular tool.{"type": "function", "function": {"name": "your_tool_name"}}: Allows you to force the model to always use a particular tool.none: Prevents the model from using any tools. (Default when notoolsare provided).

tool_choice: "auto" (the default) and add explicit instructions in a user message. For example: "What's the weather like in London? Use the get_weather tool in your response."
JSON Output
Tools do not necessarily need to be client functions â you can use tools anytime you want the model to return JSON output that follows a provided schema. For example, you might use arecord_summary tool with a particular schema to extract structured data from text or images.
When using tools for JSON output:
- You typically provide a single tool definition.
- You should set
tool_choiceto{"type": "function", "function": {"name": "your_tool_name"}}to explicitly instruct the model to use that tool. - The
functionâsparametersdefine the exact JSON schema the model should adhere to.
Chain of Thought
When using tools, NeosantaraAI models often show their âchain of thoughtâ, i.e., the step-by-step reasoning they use to break down the problem and decide which tools to use. This provides insight into the modelâs reasoning process and can help you debug unexpected behavior. For example, given the prompt âWhatâs the weather like in Jakarta right now, and what time is it there?â, a model might implicitly reason:- Identify intent: User wants weather and time for Jakarta.
- Identify tools:
get_weatherandget_timetools are available. - Plan execution: Both operations are independent, so they can be called in parallel.
- Formulate calls: Generate
tool_callsforget_weather(location='Jakarta, ID')andget_time(timezone='Asia/Jakarta').
tool_calls and feed the results back to the model.
Handling Tool Use and Tool Result Content Blocks
NeosantaraAIâs response for tool use will have afinish_reason of tool_calls and one or more tool_calls content blocks within the assistantâs message. These blocks include:
id: A unique identifier for this particular tool call. This will be used to match up the tool results later.type: Always"function".function: An object containing the name of the tool (name) and the arguments (arguments) to be passed to it as a JSON string.
Example API Response with `tool_calls` Content Block
Example API Response with `tool_calls` Content Block
-
Extract the
name,id, andarguments(parsing the JSON string) from thetool_callsblock. - Run the actual tool in your codebase corresponding to that tool name, passing in the parsed tool arguments.
-
Continue the conversation by sending a new message with the
roleoftool, and acontentblock containing the result of your toolâs execution. This message must also include thetool_call_idto link it back to the original tool request.tool_call_id: Theidof thetool_callsrequest this is a result for.content: The result of the tool, as a string. For complex results, itâs often a JSON string.is_error(optional): This field is accepted by the API layer for compatibility with OpenAI SDKs, but the underlying AI modelâs interpretation of an error relies on the content of this string rather than a specific flag. Therefore, ensure yourcontentclearly describes any error that occurred during tool execution.
Important Formatting Requirements:
- Tool messages (
"role": "tool") must always immediately follow their correspondingassistantmessage that contained thetool_calls. You cannot include any other messages between them. - When returning results for parallel tool calls, all
toolmessages must be provided in a singlemessagesarray in the subsequent API call.
Example of Successful Tool Result
Example of Successful Tool Result
Example of Tool Result with Error
Example of Tool Result with Error
If the tool itself throws an error during execution (e.g., a network error when fetching weather data), you should return the error message in the NeosantaraAI will then interpret this content and incorporate it into its response to the user, e.g., âIâm sorry, I was unable to retrieve the current weather because the weather service API is not available. Please try again later.â
content of the tool message. While the is_error: true flag is accepted by the API layer for compatibility, the modelâs understanding of the error relies on the descriptive text within the content field.Troubleshooting
Parallel Tool Calls Not Working
If NeosantaraAI isnât making parallel tool calls when expected, check these common issues: 1. Incorrect Tool Result Formatting The most common issue is formatting tool results incorrectly in the conversation history. This âteachesâ the model to avoid parallel calls. All tool results from a singleassistantâs tool_calls message must be sent in a single tool message (each result in its own content block if there were multiple parallel calls with tool_call_id and content).
2. Weak Prompting
While models generally attempt parallel calls, you can use stronger language in your prompts to encourage it, for example: âPlease use parallel tool calls to get the weather for Jakarta and Bandung at the same time.â
3. Model-Specific Behavior
Different models may have varying propensities for parallel tool use. Experiment with different models to find one that suits your needs.
Invalid Tool Name or Parameters
If the modelâs attempted use of a tool is invalid (e.g., missing required parameters), it usually means that there wasnât enough information for the model to use the tool correctly, or the tool definition was unclear. Your best bet during development is to try the request again with more-detaileddescription values in your tool definitions.
If a tool request is invalid or missing parameters, NeosantaraAI will typically retry 2-3 times with corrections before apologizing to the user. You can also return an error in the tool message by providing an error string in the content field.
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
toolsparameter 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
toolsarray in API requests (tool names, descriptions, and parameter schemas). tool_callsgenerated by the model in assistant messages.toolmessages (containingtool_call_idandcontent) sent by your application.
usage metrics.