Skip to main content
Function calling, also called tool calling, lets a model decide when to call an external function and what arguments to pass. The model does not execute the function. Your application executes it, sends the result back, and asks the model to produce the final answer. Use function calling to build assistants that can query databases, check order status, fetch weather, search internal systems, or trigger controlled actions.

Workflow

  1. Send the user message and a list of available tools.
  2. The model returns one or more tool calls with JSON arguments.
  3. Your application validates and executes the tool calls.
  4. Send the tool results back as tool messages.
  5. The model uses the results to generate the final response.

Define tools

Declare available tools in the tools array. Each function uses JSON Schema for its arguments.
tool_choice controls tool selection:
  • auto: the model decides whether to call a tool.
  • none: tool calling is disabled for this request.
  • {"type":"function","function":{"name":"get_weather"}}: force a specific function.

Read the tool call

When the model wants to call a tool, it returns tool_calls:
Parse function.arguments as JSON and keep the id. You need that ID when returning the tool result.

Return tool results

After your application runs get_weather, append a tool message using the matching tool_call_id:
Then send the full conversation back to the model so it can produce the final answer.

Complete Python example

Replace sk-your-key in API_KEY with your actual key. The example bypasses system proxy environment variables and connects directly to Moxus AI.

Complete Node.js example

Replace sk-your-key in API_KEY with your actual key. This example executes only the declared get_weather function. Do not execute arbitrary function names or arguments returned by a model.

Notes

  • Validate arguments before executing a function.
  • Return tool results as strings, usually serialized JSON.
  • A model may request multiple tool calls in one response.
  • Tool calling support depends on the selected model.
  • Every additional round consumes tokens, so keep tool outputs concise.

Next steps