The quality of your tool definitions, system prompt, and selection controls determines how reliably a model calls functions. These practices apply to every function-calling model on Together AI. Examples use GLM-5.1 (Documentation Index
Fetch the complete documentation index at: https://docs.together.ai/llms.txt
Use this file to discover all available pages before exploring further.
zai-org/GLM-5.1), the recommended function-calling model, but the same patterns work across the serverless and dedicated endpoint catalogs.
Write clear descriptions
The function description is the single biggest factor in tool-calling accuracy. It is the only context the model has for deciding when to call a tool and how to fill its arguments. Treat each description as a short spec:- State what the tool does, and when to use it (and when not to).
- Describe what each parameter means, its expected format, and how it changes the result.
- Note caveats and limits: what the tool does not return, and any edge cases.
- Describe what the output represents, so the model knows how to use the result.
type, function.name, function.description, and function.parameters (a JSON Schema object). It has no separate field for input examples, so fold any examples into the prose you already control.
Make invalid states unrepresentable
Use the JSON Schema inparameters to constrain what the model can produce, rather than validating after the fact.
- Use specific types and enums: Give every parameter a type (
string,integer,boolean), and anenumwhen the valid values are a fixed set. The model picks from the list instead of inventing a value. - Mark required fields: List the parameters the model must supply in
required. Leave genuinely optional ones out. - Avoid representable invalid states: A
toggle_light(on: bool, off: bool)signature allowson=true, off=true. Replace it with a singlestateenum of["on", "off"]so the contradiction cannot occur. - Close the schema: Set
"additionalProperties": falseon the parameters object so the model cannot add fields you do not handle.
"strict": true to the function definition. Together’s API accepts it and constrains the generated arguments to match your schema.
Python
Keep the active tool set small
The more tools you expose at once, the more chances the model has to pick the wrong one. Keep the set passed intools focused on the current task. Aim for fewer than 20 active tools as a soft target, and evaluate accuracy as you add more.
- Consolidate related operations: Instead of
create_ticket,update_ticket, andclose_ticket, expose onemanage_tickettool with anactionenum. Fewer, more capable tools reduce selection ambiguity. - Namespace tool names: When tools span multiple services, prefix the name with the service:
github_list_prs,slack_send_message. This keeps selection unambiguous as the library grows. - Scope tools to context: If you have a large catalog of tools, pass only the subset relevant to the current conversation rather than all of them on every request.
get_current_weather, not get current weather).
Offload work from the model to your code
Don’t ask the model to produce information your application already has.- Drop arguments you already know: If you already hold an
order_idfrom an earlier step, don’t define it as a parameter. Exposesubmit_refund()with no arguments and pass theorder_idin your own code when you execute the call. - Combine always-sequential calls: If you always call
mark_location()right afterquery_location(), merge the marking logic into the query tool. One round trip is more reliable than two.
Guide the model with the system prompt
The system prompt sets the policy the model follows when deciding whether and how to call a tool.- Give the model a role:
You are a travel planning assistant with access to weather and restaurant tools. - State when to use each tool, and when not to. Tell the model exactly what to do for the cases you care about.
- Forbid guessing:
Do not guess values. If a required detail is missing, ask the user for it before calling a tool. - Encourage clarification: Instruct the model to ask a follow-up question when the request is ambiguous, rather than calling a tool with assumed arguments.
Control tool selection with tool_choice
The tool_choice parameter decides whether the model may call a tool on a given request. See tool_choice options for the full reference.
"auto"(default): the model decides whether to call a tool or reply with text."required": the model must call at least one tool.- A specific tool: pass
{"type": "function", "function": {"name": "get_current_stock_price"}}to force that tool regardless of phrasing. "none": the model replies with text only.
Handle responses and errors robustly
Tool calls come back inmessage.tool_calls, not message.content (which is often null on a tool-calling turn). Build the loop defensively:
- Check
finish_reason: It is"tool_calls"when the model wants you to run a tool, and"stop"for a normal text reply. Branch on it instead of assuming a tool was called. - Parse arguments as JSON:
function.argumentsis a JSON-encoded string. Parse it inside a try/except, and handle the case where the model produces malformed or incomplete JSON. - Return informative tool errors: When a tool fails, return a clear error message in the
toolmessage content (for example,{"error": "No stock found for symbol XYZ"}) so the model can recover or explain the failure to the user, rather than throwing. - Validate high-consequence calls: Before executing a tool with real side effects (placing an order, sending a refund, deleting data), confirm the call with the user.
Tune for reliable calls
- Lower the temperature: A low
temperature(for example,0) makes tool selection and argument generation more deterministic. Raise it only if you need more varied behavior. - Stream when latency matters: Tool calls stream incrementally through
delta.tool_calls. Use streaming to start handling a call before the full response arrives. - Watch your token budget: Tool descriptions and schemas count toward input tokens. If you approach the limit, tighten descriptions or split a large tool set into smaller, task-specific groups.