How to use function calling (tools) with an LLM API

Let the model call your own functions with the OpenAI tools format.

Describe your function

Pass a tools list with a JSON schema for each function. When the model decides to call one, run it in your code and send the result back as a tool message.

Only your own function tools are supported; provider-hosted tools such as built-in web search are not offered.

Example

from openai import OpenAI

client = OpenAI(api_key="YOUR_TOKENREX_KEY", base_url="https://tokenrex.ai/api/public")

resp = client.chat.completions.create(
    model="gpt-6-astra",
    messages=[{"role": "user", "content": "Weather in Delhi?"}],
    tools=[{"type": "function", "function": {"name": "get_weather", "parameters": {"type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"]}}}],
)
print(resp.choices[0].message.tool_calls)
Get API keyCost calculatorDocs

Questions

Do I need a separate key for each provider?
No. One TokenRex API key works for every enabled model; change only the model ID.

Related