Skip to content
Free$1 of credit for every new organization
All articles

Guide

Tool calling across Claude, GPT and Gemini

Define your tools once in the OpenAI format and use them with models from Anthropic, OpenAI and Google. The loop, the pitfalls, and how to switch models safely.

· 3 min read · Usefully team


Tool calling (or function calling) is how a model asks your code to do something: look up an order, query a database, fetch the weather. You describe the tools, the model decides when to call one and with what arguments, your code runs it, and the result goes back into the conversation. Every major provider supports it, but each one has its own request format.

Through Usefully you write tools once, in the OpenAI chat completions format, and use them with any model that supports tool calling, whether it's from Anthropic, OpenAI or Google. Switching models is a change of model ID, not a rewrite of your agent.

Define a tool

A tool is a name, a description and a JSON Schema for its parameters. The description matters more than people expect: it's what the model reads to decide whether to call the tool.

tools = [{
    "type": "function",
    "function": {
        "name": "get_order_status",
        "description": "Look up the current status of a customer's order by its order number.",
        "parameters": {
            "type": "object",
            "properties": {
                "order_id": {"type": "string", "description": "Order number, e.g. A-10442"},
            },
            "required": ["order_id"],
        },
    },
}]

Run the loop

The model may answer directly or return one or more tool_calls. When it does, run each one, append the results as tool messages, and call the model again. Repeat until it answers without calling a tool, with a cap on the number of rounds.

import json
from openai import OpenAI

client = OpenAI(base_url="https://api.usefully.info/v1", api_key=USEFULLY_API_KEY)

def run(messages: list, model: str = "anthropic/claude-sonnet-5-5") -> str:
    for _ in range(5):  # never loop forever
        reply = client.chat.completions.create(model=model, messages=messages, tools=tools)
        msg = reply.choices[0].message
        if not msg.tool_calls:
            return msg.content
        messages.append(msg)
        for call in msg.tool_calls:
            args = json.loads(call.function.arguments)
            result = TOOLS[call.function.name](**args)   # your own functions
            messages.append({"role": "tool", "tool_call_id": call.id, "content": json.dumps(result)})
    return "Sorry, I couldn't finish that."

To try the same agent on a different model, change model, for example to openai/gpt-5 or google/gemini-2.5-flash. Check the models page first: it has a filter for models that support tools.

Differences you'll still notice

A common format doesn't make models behave identically. When you switch, test for these:

  • Parallel calls. Some models return several tool calls at once; others call one at a time. Make your loop handle both, as the example above does.
  • Eagerness. Models differ in how readily they call a tool versus answering from memory. If a model skips a tool it should use, say so in the system prompt: "Always look up order status; never guess."
  • Argument quality. Validate arguments before running anything. A model may send a field in the wrong format or omit an optional one you rely on.
  • Text alongside calls. Some models include a sentence of text with a tool call. Don't assume content is empty when tool_calls is present.

Safety rules for tools

  • Treat arguments as untrusted input. The model chose them, possibly influenced by text a user or a web page supplied. Validate and authorize as you would for any API request.
  • Keep destructive actions behind confirmation. A tool that sends email or issues a refund should prepare the action and let a person approve it.
  • Return errors as results. If a tool fails, send back a short error message as the tool result. Models handle "order not found" well; they handle a crashed loop badly.
  • Limit rounds. Five rounds is plenty for most agents and bounds the cost of a confused model.

Cost

Each round of the loop is a separate request, billed at the model's token prices, and the conversation (including tool results) is re-sent each time. Large tool results inflate input tokens quickly, so return only the fields the model needs. Logs show every round with its tokens and cost.

Tool calling uses the standard tools and tool_choice fields; see the API reference. For structured output without running anything, see structured JSON output.

More articles

Get started

Your first request is five minutes away.

  • $1 of free credit to start
  • No card, no subscription
  • Keep the SDK you already use
  • Real people at support@usefully.info
Tool calling across Claude, GPT and Gemini · Usefully