Tools and structured output

Let the model call your functions, or answer in the exact JSON you need.

Function calling

Describe your functions in tools. The model answers with the calls it wants; your code runs them and sends the results back.

const reply = await client.chat.completions.create({
  model: "anthropic/claude-sonnet-5.5",
  max_tokens: 1024,
  messages: [{ role: "user", content: "What's the price of $NOVA?" }],
  tools: [
    {
      type: "function",
      function: {
        name: "token_price",
        description: "The latest price of a token on BNB Chain, in USD",
        parameters: {
          type: "object",
          properties: { ticker: { type: "string" } },
          required: ["ticker"],
        },
      },
    },
  ],
});

const call = reply.choices[0].message.tool_calls?.[0];

Tools work the same on Responses and on Messages, in each format's own shape. Filter Models by Tools to see which models support them.

Structured output

Ask for JSON that matches your schema with response_format:

{
  "response_format": {
    "type": "json_schema",
    "json_schema": {
      "name": "trade_summary",
      "strict": true,
      "schema": {
        "type": "object",
        "properties": {
          "trades": { "type": "integer" },
          "net_bnb": { "type": "number" }
        },
        "required": ["trades", "net_bnb"],
        "additionalProperties": false
      }
    }
  }
}

Tools we don't serve

Every agent's calls run on one shared account, so tools that keep files, memory or a sandbox there, or that run other models inside a call, are refused with 400 tool_not_served:

  • hosted code execution, shells and code interpreters
  • file search, files, memory and the text editor tool
  • image generation and tools that call other models
  • X search, which bills per post with no limit a request can set

Tools your code runs on its own machine always work. Web search and web fetch are served: see Web search.

Cap tool loops

Hosted tools like web search run in a loop, and the reserve covers every possible step. Set max_tool_calls (or the tool's max_uses) to keep it small. A loop with no step limit is refused.

On this page