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)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
- How to use the OpenAI SDK with a custom base URL
- How to call Claude with the OpenAI SDK
- How to call Gemini with the OpenAI SDK
- How to use the Anthropic Messages API on TokenRex
- How to stream LLM responses
- How to send images to a vision model
- How to generate images with an API
- How to convert text to speech with an API