返回博客
EnglishGuide

Function Calling Across AI Providers: A Portable Tool-Calling Guide

Function calling lets a model request a typed operation such as searching a database, creating a ticket, or looking up an order. The model must never receive unrestricted access to your infr

C
Crazyrouter Team
September 28, 2026 / 256 次浏览
分享:
Function Calling Across AI Providers: A Portable Tool-Calling Guide

Function Calling Across AI Providers: A Portable Tool-Calling Guide#

Function calling lets a model request a typed operation such as searching a database, creating a ticket, or looking up an order. The model must never receive unrestricted access to your infrastructure. Your application validates the proposed function, checks authorization, executes a narrow operation, and returns a bounded result. This guide is for developers who need an implementation path, not a product slogan. It covers the concept, alternatives, a tested API pattern, pricing, production controls, and FAQ answers.

What is Function Calling Across AI Providers?#

Function calling lets a model request a typed operation such as searching a database, creating a ticket, or looking up an order. The model must never receive unrestricted access to your infrastructure. Your application validates the proposed function, checks authorization, executes a narrow operation, and returns a bounded result. In a real application, the model is only one component. You also need authentication, input limits, output validation, telemetry, retries, and a clear policy for data retention. Keep these controls in application code rather than asking the model to enforce them.

Function Calling Across AI Providers vs alternatives#

OpenAI-compatible APIs usually represent tools in a familiar JSON schema, Anthropic uses content blocks, and Gemini uses function declarations and parts. The concepts are portable, but the wire format is not. Normalize every provider response into an internal object containing name, arguments, call ID, result, and status.

Decision areaDirect providerSelf-hosted modelCrazyrouter
SetupFast for one providerHighest operations burdenFast multi-model setup
Model choiceOne ecosystemYour deployed weights627+ model catalog
FailoverUsually application-builtApplication-builtCentralized route options plus application policy
BillingSeparate provider accountsGPU and operationsOne pay-as-you-go account

How to use it with code#

The following smoke-test pattern was verified against https://cn.crazyrouter.com/v1 on 2026-09-28. The gateway returned HTTP 200 for /v1/models and for a chat completion request. Use an environment variable in production and replace the model with one shown in the live model list.

python
import json
from openai import OpenAI
client=OpenAI(base_url="https://cn.crazyrouter.com/v1",api_key="YOUR_KEY")
tools=[{"type":"function","function":{"name":"get_weather","description":"Read weather","parameters":{"type":"object","properties":{"city":{"type":"string"}},"required":["city"]}}}]
r=client.chat.completions.create(model="gpt-5-mini",messages=[{"role":"user","content":"Weather in Tokyo?"}],tools=tools)
call=r.choices[0].message.tool_calls[0]
print(call.function.name,json.loads(call.function.arguments))
bash
curl https://cn.crazyrouter.com/v1/chat/completions \
  -H "Authorization: Bearer $CRAZYROUTER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model":"gpt-5-mini","messages":[{"role":"user","content":"Reply with a health check"}],"max_tokens":40}'

For Node.js, the same OpenAI-compatible route can be configured with baseURL: "https://cn.crazyrouter.com/v1". Do not expose the key in browser JavaScript. Put requests behind your server, attach a tenant ID, and enforce a budget before sending long prompts or launching asynchronous media jobs.

Pricing breakdown#

RoutePricing modelBest fit
Official providerProvider input/output ratesOne-provider production
Self-hosted open modelGPU, storage, and operationsPredictable high volume or strict data control
CrazyrouterPay as you go; verify live ratesOne key, many models, fast comparison

Prices and availability change, so check the live Crazyrouter pricing page before forecasting a launch. Do not promise a fixed model price in application code.

The practical calculation is cost per successful task. Include retries, failed generations, moderation checks, retrieval calls, and storage. A cheaper model that needs two retries or produces invalid JSON may be more expensive than a stronger model that succeeds once. Crazyrouter is useful for comparing models through one key; always confirm the current model name and rate on the live pricing page.

Production checklist#

  • Keep API keys in a secret manager or runtime environment.
  • Set timeouts and bounded exponential backoff for transient failures.
  • Validate JSON, tool arguments, and generated URLs outside the model.
  • Record request ID, model, latency, token usage, status, and estimated cost.
  • Apply per-user and per-tenant quotas before the provider call.
  • Redact personal data from logs and minimize what leaves your system.
  • Maintain a fallback or human-review path for high-impact actions.

FAQ#

Is this suitable for production?#

Yes, when the integration has authentication, quotas, observability, validation, and a rollback path. A successful demo alone is not a production readiness test.

Is the official API cheaper than a gateway?#

It depends on the model, volume, region, and gateway pricing. Compare effective cost per successful task, not a general assumption. Check current rates before committing.

Can I switch models later?#

Yes, if application logic is separated from model selection and you test output quality, schema behavior, latency, and safety after every route change.

How do I reduce AI API costs?#

Use smaller models for routine tasks, cap output tokens, summarize repeated context, cache deterministic work, batch non-urgent jobs, and stop unbounded retries.

Summary#

Function calling lets a model request a typed operation such as searching a database, creating a ticket, or looking up an order. The model must never receive unrestricted access to your infrastructure. Your application validates the proposed function, checks authorization, executes a narrow operation, and returns a bounded result. Start with a narrow workflow, a versioned evaluation set, and a measured fallback. Crazyrouter provides one API surface for comparing many models while you keep product policy and security in your own application.

Implementation Guides

主题

Guide

相关文章

Gemini Free Plan Guide 2026: Limits, Pricing, and When to UpgradeGuide

Gemini Free Plan Guide 2026: Limits, Pricing, and When to Upgrade

Complete developer guide to the Gemini free plan in 2026, including limits, pricing, model access, and when to upgrade to Gemini Advanced or API access.

Mar 17
Function Calling Across AI Providers in 2026: A Practical API GuideGuide

Function Calling Across AI Providers in 2026: A Practical API Guide

Learn how to design portable function calling across OpenAI-compatible, Anthropic, Gemini, and open-model APIs with schemas, validation, retries, and routing.

Sep 19
Why is ChatGPT Not Working? Complete Troubleshooting Guide 2026Guide

Why is ChatGPT Not Working? Complete Troubleshooting Guide 2026

"ChatGPT down or not responding? Here's a complete troubleshooting guide covering common issues, fixes, and reliable alternatives for uninterrupted AI access."

Feb 27
Claude API Free Tier in 2026: Does Anthropic Offer One, and What the Cheapest Way to Start Actually IsGuide

Claude API Free Tier in 2026: Does Anthropic Offer One, and What the Cheapest Way to Start Actually Is

Anthropic has no free tier for the Claude API — the free plan is the claude.ai chat app. Here is what access actually exists in 2026: occasional Console trial credits, cloud provider credits, and prepaid gateways like Crazyrouter at 35% below list, with the limits of each.

Sep 30
Seedance and ByteDance Video AI 2026: API Queues, Costs, and FallbacksGuide

Seedance and ByteDance Video AI 2026: API Queues, Costs, and Fallbacks

A developer guide to Seedance video AI integration with asynchronous queues, cost controls, prompt versioning, and model fallbacks.

Sep 6
"Kimi K2 Thinking Guide: Reasoning Workflows, API Calls, and Production Evaluation"Guide

"Kimi K2 Thinking Guide: Reasoning Workflows, API Calls, and Production Evaluation"

"Learn how to evaluate Kimi K2 Thinking for reasoning, coding, tool use, and long-context tasks, with OpenAI-compatible examples and production cost guidance."

Sep 20