thelastapi
Get started free

Function calling across GPT, Claude and Gemini with one API

Run function calling across GPT, Claude, and Gemini with one OpenAI-compatible endpoint, a single tools schema, and unified cost tracking.

On this page 8 sections

You can run function calling across GPT, Claude, Gemini, and over 400 other models through one OpenAI-compatible endpoint using the standard tools schema. Send your tool definitions to https://thelastapi.dev/v1/ai/chat/completions or point the standard OpenAI SDK to VIBIZ_AI_BASE_URL. You switch models simply by changing the model parameter, and the gateway handles the underlying formatting while returning identical tool call response objects.

This guide walks through defining tools once, executing function calls across models from different labs, tracking token costs, and handling safety limits.

The problem with provider-specific tool schemas

Each AI lab initially developed its own conventions for function calling. One lab expected functions nested under specific parameters, another expected tools wrapped in separate content blocks, and response formats differed across providers.

Building an application that dynamically routes prompts to different models usually forces you to maintain custom adapters or juggle multiple client SDKs. That creates real maintenance friction:

  • Duplicate schemas: You must translate your JSON schemas whenever you test an alternative model.
  • Scattered error handling: Different labs return parsing issues, missing arguments, and tool call failures in completely different shapes.
  • Key proliferation: Every lab requires its own credentials, billing setup, and usage monitoring.

A unified gateway solves this by exposing the OpenAI chat completions shape for all models. As explained in our guide on how a unified AI API works, you can keep your existing OpenAI client and run models from Anthropic, Google, and OpenAI using the exact same request body.

Defining tools once with the OpenAI SDK

Because thelastapi exposes an OpenAI-compatible gateway at /v1/ai, you can use the official openai package directly. When you create a project or run vibiz dev, the local environment receives VIBIZ_AI_BASE_URL and VIBIZ_AI_API_KEY, alongside the standard OPENAI_BASE_URL and OPENAI_API_KEY aliases.

Here is how to define a function tool and execute it across OpenAI, Anthropic, and Google models using the identical schema:

TypeScript
import OpenAI from 'openai';

const openai = new OpenAI({
  baseURL: process.env.VIBIZ_AI_BASE_URL,
  apiKey: process.env.VIBIZ_AI_API_KEY,
});

const tools: OpenAI.ChatCompletionTool[] = [
  {
    type: 'function',
    function: {
      name: 'get_current_weather',
      description: 'Get the current weather for a given location',
      parameters: {
        type: 'object',
        properties: {
          location: {
            type: 'string',
            description: 'The city and state, e.g. San Francisco, CA',
          },
          unit: {
            type: 'string',
            enum: ['celsius', 'fahrenheit'],
          },
        },
        required: ['location'],
      },
    },
  },
];

const models = [
  'vibiz/fast',     // Runs on GPT-6 Luna (OpenAI)
  'vibiz/smart',    // Runs on Claude Sonnet 5.5 (Anthropic)
  'vibiz/balanced', // Runs on Gemini 3.8 Flash (Google)
];

for (const model of models) {
  const response = await openai.chat.completions.create({
    model,
    messages: [{ role: 'user', content: 'What is the weather in Boston?' }],
    tools,
    tool_choice: 'auto',
  });

  const choice = response.choices[0];
  const toolCalls = choice.message.tool_calls;

  if (toolCalls && toolCalls.length > 0) {
    for (const toolCall of toolCalls) {
      console.log(`[${model}] Tool requested:`, toolCall.function.name);
      console.log(`[${model}] Arguments:`, toolCall.function.arguments);
    }
  }
}

If your application uses the platform client library @vibiz/app, you do not even need to supply environment variable names manually. Passing new OpenAI(vibiz.ai) automatically configures the client with the project credentials. For more details on accessing diverse models through code, see our article on how to access multiple AI models with a single API.

Calling tools with HTTP requests

If you are not using Node.js or Python, you can send standard HTTP requests with curl or any HTTP client. The tools array and the returned tool_calls structure adhere strictly to standard chat completion conventions:

Terminal
curl https://thelastapi.dev/v1/ai/chat/completions \
  -H "Authorization: Bearer $VIBIZ_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "vibiz/smart",
    "messages": [
      {"role": "user", "content": "What is the weather in Chicago?"}
    ],
    "tools": [
      {
        "type": "function",
        "function": {
          "name": "get_current_weather",
          "description": "Get current weather",
          "parameters": {
            "type": "object",
            "properties": {
              "location": {"type": "string"}
            },
            "required": ["location"]
          }
        }
      }
    ]
  }'

The gateway returns a standard completion payload. In addition to choices[0].message.tool_calls, every JSON response includes served_model confirming the exact model that executed, and cost_micro_usd, the exact cost of the call in micro-USD:

JSON
{
  "id": "chatcmpl_abc123",
  "object": "chat.completion",
  "created": 1714000000,
  "model": "vibiz/smart",
  "served_model": "anthropic/claude-sonnet-5.5",
  "cost_micro_usd": 680,
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": null,
        "tool_calls": [
          {
            "id": "call_xyz789",
            "type": "function",
            "function": {
              "name": "get_current_weather",
              "arguments": "{\"location\":\"Chicago\"}"
            }
          }
        ]
      },
      "finish_reason": "tool_calls"
    }
  ]
}

Model tiers and pricing for function calling

Different models offer different trade-offs between schema precision, reasoning depth, and cost. You can call thelastapi tiers or target specific provider IDs directly.

When you make a request, exactly that model runs. If a provider experiences downtime, the gateway returns model_unavailable or provider_unavailable rather than swapping to a different model behind your back.

Tier / Model IDBacked byBest suited forInput / Output per million tokens
vibiz/fastGPT-6 Luna (OpenAI)Standard tools, high volume, low latency$0.10 / $0.50
vibiz/smart-miniClaude Haiku 5.5 (Anthropic)Fast, structured tool extraction$0.10 / $0.50
vibiz/balancedGemini 3.8 Flash (Google)High context windows (1M tokens) with tools$0.825 / $4.125
vibiz/smartClaude Sonnet 5.5 (Anthropic)Complex multi-step agent reasoning$2.00 / $10.00
vibiz/reasoningGPT-6.1 Terra (OpenAI)Deep planning before tool invocation$1.00 / $5.00
vibiz/proClaude Opus 5.5 (Anthropic)Demanding multi-tool workflows$4.00 / $20.00

You can also specify specific lab model names such as moonshotai/kimi-k3.1-pro or anthropic/claude-opus-5.5. If you send an unrecognized model name, the gateway returns a 400 invalid_request error with param: "model". You can review the complete catalog in the AI documentation.

Guardrails, limits, and loop prevention

Autonomous agents executing function calls can sometimes get stuck in loops, issuing dozens of requests in seconds. The gateway includes automatic safeguards to manage usage:

  1. Daily account spend ceiling: New accounts have a default limit of $5 per UTC day across AI and actions. An owner can adjust this in the console or by sending PATCH /v1/billing {"daily_spend_limit_usd": 50}. Requests exceeding this limit return a 429 rate_limited error with details.reason: "daily_spend".
  2. Project AI budgets: Each project defaults to a $1 daily AI budget. You can adjust this with the CLI command vibiz ai budget 10 --project prj_123 or via PATCH /v1/projects/:id/ai {"daily_budget_usd": 10}. When exceeded, the gateway answers 429 with details.reason: "project_budget".
  3. Per-user tracking: Pass an end-user identifier in the user field of your request (or use X-Vibiz-User). This breaks down your usage by user and allows you to set an end_user_daily_budget_usd cap so a single user cannot exhaust your budget.
  4. Rate limits: Account keys are capped at 300 requests and 400,000 tokens per minute. Project keys default to 120 requests and 200,000 tokens per minute.

To inspect recent spend across models or tools, run:

Terminal
vibiz usage --by model
vibiz usage --by tool --period 7d

More details on limits and alerts are available on the Usage and limits page.

Handling errors and streaming tool calls

If you stream tool execution with server-sent events (stream: true), the tool call arguments arrive incrementally in choice.delta.tool_calls. You can learn more about streaming mechanics in our guide on streaming chat completions with server-sent events.

When things go wrong, the gateway returns structured errors that standard SDKs can parse:

  • context_length_exceeded (400): The combined prompt, tool schema, and response exceeded the model's context window.
  • rate_limited (429): Triggered when request limits, token caps, or daily budgets are hit. Inspect details.reason to identify whether it was daily_spend, project_budget, or requests_per_minute.
  • model_unavailable (503): The provider model cannot be reached. The response includes details.retry_after_seconds.

If an error occurs mid-stream during an SSE session, it is transmitted as a final SSE event: data: {"error": {...}}.

Next steps

To test function calling in your terminal, create a new project with vibiz init "Tool Agent" --resources ai and run vibiz dev to generate your local credentials. Read the AI documentation for full schema specifications, or explore how to configure provider keys in the console if you plan to bring your own credentials.

Frequently asked questions

Can I stream responses while using function calling across models?

Yes, set `stream: true` in your request to receive tool call arguments incrementally via standard server-sent events (`delta.tool_calls`). If an error occurs midway through the stream, the gateway emits a final SSE event containing the structured error payload.

Can I use my own Google API key for Gemini tool calls?

Yes, run `GEMINI_API_KEY=... vibiz ai keys set google` to route Google models directly through your Google account. When using your own key, those calls cost 0 on thelastapi and are billed directly by Google.

What happens if a model generates invalid arguments for a tool?

The gateway passes through the exact arguments string generated by the underlying model without altering it. Your application is responsible for parsing the JSON arguments and validating them against your business logic.

Does the gateway fall back to another model if tool calling fails?

No, the gateway never quietly falls back to another model. If the requested model is unavailable, the gateway returns a `503 model_unavailable` or `503 provider_unavailable` error with