Function calling (also called tool use) lets a model ask your code to run a function instead of answering directly: it looks up a weather API, queries a database, or performs a calculation, then uses the result to finish its answer. The model never runs anything itself; it only returns a request to call a function with certain arguments, and your code decides whether to actually run it.
The request shape
You describe each available function as a JSON Schema: a name, a description, and the shape of its arguments.
tools = [{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather for a city",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "City name, e.g. Tokyo"},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
},
"required": ["city"],
},
},
}]
The model's description and the parameter descriptions are what it uses to decide when and how to call the function, so write them the way you would explain the function to another person, not just a type signature.
Full multi-turn example
A tool call is not the end of the conversation. The model pauses, you run the function, and you send the result back in a new message so the model can continue.
import json
from openai import OpenAI
client = OpenAI(base_url="https://aidiscount.ai/v1", api_key=os.environ["AIDISCOUNT_API_KEY"])
def get_weather(city, unit="celsius"):
return {"city": city, "temperature": 18, "unit": unit, "condition": "cloudy"}
messages = [{"role": "user", "content": "What's the weather in Tokyo right now?"}]
resp = client.chat.completions.create(model="gpt-6-luna", messages=messages, tools=tools)
msg = resp.choices[0].message
messages.append(msg)
if msg.tool_calls:
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),
})
final = client.chat.completions.create(model="gpt-6-luna", messages=messages, tools=tools)
print(final.choices[0].message.content)
Key points this example relies on: append the assistant's tool-call message before the tool result, match each tool_call_id to the call it answers, and send content as a string (encode objects with json.dumps).
Multiple tool calls in one turn
A model can request several tool calls at once, for example looking up the weather in three cities. Loop over msg.tool_calls and append one "role": "tool" message per call, each with its own tool_call_id, before calling the model again.
Anthropic Messages format
The shapes differ: each tool's schema goes in input_schema instead of function.parameters, a tool call arrives as a tool_use content block, and the result is sent back as a tool_result content block inside a user message.
import anthropic
client = anthropic.Anthropic(base_url="https://aidiscount.ai", api_key=os.environ["AIDISCOUNT_API_KEY"])
tools = [{
"name": "get_weather",
"description": "Get the current weather for a city",
"input_schema": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"],
},
}]
messages = [{"role": "user", "content": "What's the weather in Tokyo right now?"}]
resp = client.messages.create(model="claude-opus-4-8", max_tokens=1024, messages=messages, tools=tools)
messages.append({"role": "assistant", "content": resp.content})
tool_use = next(b for b in resp.content if b.type == "tool_use")
result = get_weather(**tool_use.input)
messages.append({
"role": "user",
"content": [{"type": "tool_result", "tool_use_id": tool_use.id, "content": json.dumps(result)}],
})
final = client.messages.create(model="claude-opus-4-8", max_tokens=1024, messages=messages, tools=tools)
print(final.content[0].text)
Forcing or disabling tool use
By default the model decides whether to call a tool. To force one specific tool, pass tool_choice={"type": "function", "function": {"name": "get_weather"}} (OpenAI format) or tool_choice={"type": "tool", "name": "get_weather"} (Anthropic format). To stop the model from calling tools for one request while keeping the definitions, set tool_choice="none" (OpenAI format) or tool_choice={"type": "none"} (Anthropic format).
Common mistakes
- Returning the tool result as the wrong role. OpenAI format expects
"role": "tool"; it will not accept the result as a normal user or assistant message. - Forgetting to send the assistant's tool-call message back. The model needs to see its own tool call in history to connect it to the result.
- Sending the raw Python object instead of a JSON string in
content. Always serialize withjson.dumps. - Not validating arguments before running the function. Treat tool-call arguments as untrusted input from the model; validate types and ranges before acting on them, especially before using them in a database query or shell command.
Picking a model
Most current models on AIDiscount support tool calling; a model's page in the marketplace lists it under capabilities. Current prices for a few tool-capable models:
| Model | Input / 1M tokens | Output / 1M tokens | vs. official price |
|---|---|---|---|
GPT-6 Lunagpt-6-luna | $0.05 | $0.30 | 40% off |
Claude Opus 4.8claude-opus-4-8 | $0.375 | $1.875 | 92.5% off |
DeepSeek V4 Prodeepseek-v4-pro | $0.3857 | $1.1571 | 41.6% off |
Kimi K3kimi-k3 | $1.714 | $8.571 | 42.9% off |
GLM-5.3glm-5.3 | $0.6857 | $2.40 | 45.5% off |
Create a free accountRead the API docs