DEVSAIO

Function calling (tool use) explained with a working example

How models call your functions: define tools, run them, send results back. A complete Python example with a weather tool.

DEVSAIO team · 2 min read
Share on X

Function calling lets a model ask your code to do something: look up an order, check the weather, query a database. The model never runs code itself; it returns a structured request, you run it, and you send the result back.

The loop

  1. You send the user's message plus a list of tools (name, description, JSON schema of arguments).
  2. The model replies with a tool call: which function and what arguments.
  3. Your code runs the function.
  4. You send the result back as a tool message.
  5. The model writes the final answer using the result.

Complete example (OpenAI format)

python
import json, os
from openai import OpenAI

client = OpenAI(base_url="https://devsaio.lol/v1", api_key=os.environ["DEVSAIO_API_KEY"])

def get_weather(city: str) -> dict:
    # call a real weather API here
    return {"city": city, "temp_c": 31, "conditions": "sunny"}

tools = [{
    "type": "function",
    "function": {
        "name": "get_weather",
        "description": "Get current weather for a city",
        "parameters": {
            "type": "object",
            "properties": {"city": {"type": "string", "description": "City name"}},
            "required": ["city"],
        },
    },
}]

messages = [{"role": "user", "content": "Should I wear a jacket in Karachi today?"}]
r = client.chat.completions.create(model="claude-sonnet-4-6", messages=messages, tools=tools)
msg = r.choices[0].message

if msg.tool_calls:
    messages.append(msg)
    for call in msg.tool_calls:
        args = json.loads(call.function.arguments)
        result = get_weather(**args)
        messages.append({"role": "tool", "tool_call_id": call.id, "content": json.dumps(result)})
    r = client.chat.completions.create(model="claude-sonnet-4-6", messages=messages, tools=tools)

print(r.choices[0].message.content)

Tips for reliable tool use

  • Write good descriptions. The model decides when to call a tool from its description.
  • Keep schemas small and strict. Use enum for fixed choices and mark required fields.
  • Validate arguments before running anything; never trust them blindly, especially for actions like refunds or deletes.
  • Loop until there are no more tool calls, with a maximum number of steps to avoid runaway loops.
  • Return errors as results ({"error": "city not found"}) so the model can recover gracefully.

Function calling is the foundation of AI agents. Most strong models on DEVSAIO support it; check the model's capabilities on the model page.

Try it with one API key

Claude, GPT, Gemini, DeepSeek, image and video models through one OpenAI- and Anthropic-compatible endpoint. Prepaid, pay only for what you use.

Keep reading