Function calling (also called "tool use") is how you turn an LLM into an agent: the model returns a structured request to call one of your functions, your code runs the function, and the result is fed back to the model. Plugsky implements the OpenAI shape, so LangChain, LlamaIndex, Vercel AI SDK, and the official OpenAI libraries all work unchanged.
The function calling loop
from openai import OpenAI
import json
client = OpenAI(base_url="https://api.plugsky.com/v1", api_key="sk-live-...")
tools = [{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get current weather for a city",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"]
}
}
}]
def run_loop(user_msg):
messages = [{"role": "user", "content": user_msg}]
while True:
r = client.chat.completions.create(model="plugsky-pro", messages=messages, tools=tools)
msg = r.choices[0].message
if not msg.tool_calls:
return msg.content
messages.append(msg)
for call in msg.tool_calls:
args = json.loads(call.function.arguments)
result = get_weather(**args) # your function
messages.append({"role":"tool","tool_call_id":call.id,"content":json.dumps(result)})
print(run_loop("What's the weather in Dubai?"))
Tool definition
Tools are JSON Schema objects. Plugsky validates the parameters against your schema before sending the call back to your code:
type: "object"withpropertiesandrequired- String, number, integer, boolean, array, enum, nested objects
- Optional
descriptionon every field — the LLM uses these to decide what to call - Optional
enumto constrain string values to a fixed set
Parallel calls and JSON mode
Plugsky supports:
- Parallel function calls: the model can return multiple tool calls in one response, and your code dispatches them concurrently
- tool_choice: force the model to call a specific tool, or "auto", or "none"
- response_format: {"type":"json_object"} for guaranteed JSON output
- Strict mode: with
"strict": trueon a tool, the model is forced to use exactly the schema you provide (no extra fields)
Building agents
Combine function calling with the AI agents API for a managed agent runtime with memory, retries, and audit logs. Or roll your own — the loop above works for any number of tools and steps.
Frequently asked questions
How many tools can I define?
No hard limit. In practice, 5-20 tools is the sweet spot. More than 20 and the model struggles to pick the right one.
Can I force the model to call a specific tool?
Yes. Set tool_choice: {"type": "function", "function": {"name": "tool_name"}}.
Does streaming work with function calling?
Yes. Use stream: true and the model will stream the text portion, then emit the tool call when ready.
What happens if my tool throws an error?
Catch it in your code and return an error message as the tool result. Plugsky will let the model see the error and try again or surface it to the user.
Build your first agent
Tools, function calling, JSON mode, parallel calls — same API as OpenAI, 18+ models.
Start $5 trial → Open the agent builder