Tool calling
Let supported models request functions from your application.
Tool calling lets a model produce structured arguments for a function your application controls. Your code remains responsible for validating the arguments, running the function, and sending its result back to the model.
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.EUROUTER_API_KEY,
baseURL: "https://api.eurouter.ai/api/v1",
});
const messages = [{ role: "user", content: "What is the weather in Amsterdam?" }];
const first = await client.chat.completions.create({
model: "mistral-large-3",
messages,
tools: [
{
type: "function",
function: {
name: "get_weather",
description: "Get the current weather for a city",
parameters: {
type: "object",
properties: {
city: { type: "string" },
},
required: ["city"],
additionalProperties: false,
},
},
},
],
});
const assistantMessage = first.choices[0].message;
messages.push(assistantMessage);
for (const toolCall of assistantMessage.tool_calls ?? []) {
const args = JSON.parse(toolCall.function.arguments);
// Validate args before calling your own trusted function.
const result = await getWeather(args.city);
messages.push({
role: "tool",
tool_call_id: toolCall.id,
content: JSON.stringify(result),
});
}
const final = await client.chat.completions.create({
model: "mistral-large-3",
messages,
});
console.log(final.choices[0].message.content);Safety checklist
- Treat tool arguments as untrusted input and validate them against a schema.
- Use an allowlist of tools; never execute a function name supplied directly by the model.
- Require explicit user confirmation before destructive or costly actions.
- Apply authorization checks in the tool implementation, not in the prompt.
- Set timeouts and record tool outcomes for debugging.
Tool support varies by model. Check the model catalog before relying on it in production.