Client-side tool-call cycle
Complete a bounded Chat Completions tool loop and return results using tool_call_id.
Also available as Markdown · all agent guides in one file
Key points
- Rely on tool calls only where
tool_callingissupportedfor the endpoint; withunknown, verify the route yourself first. - Aether forwards the tool definitions; your application validates arguments and executes the functions.
- Return one
role: toolmessage per call with the returnedtool_call_id, and limit the number of rounds.
This example requires a regular API key and a chat model whose capabilities["/v1/chat/completions"].tool_calling is supported; with unknown, verify the route yourself first.
- Aether forwards the tool definitions; your application validates arguments and executes the functions. It does not execute arbitrary local functions for you.
parallel_tool_callstells you whether several calls per turn are reliable; the loop below handles several either way.- The enabled sandbox can rehearse one deterministic call-and-result cycle without a provider (see the sandbox document); that does not prove a live model's tool support.
The cycle
- Send messages and tools. Inspect
choices[0].message.tool_callsafter a successful response. - Append the entire assistant message including its
tool_callsto the conversation. Validate every call against your allowlist and argument schema before running it. - Append one
role: toolmessage per call, withtool_call_idequal to the returnedcall.idandcontentas a string (JSON.stringifyfor objects). - Return all results for a turn before requesting the next assistant turn.
- Send the updated messages and tool definitions again. Repeat until the assistant returns a final response; limit the number of rounds and handle truncated or failed turns explicitly.
Example
Save as tools.mjs and run node tools.mjs with Node.js 20 or later. The only allowed function returns the current UTC time.
Side effects
For tools with external effects, enforce permissions and any required user approval in your application.const apiKey = process.env.AETHER_API_KEY;
const model = process.env.AETHER_MODEL;
if (!apiKey || !model) throw new Error("Set AETHER_API_KEY and AETHER_MODEL");
const tools = [{ type: "function", function: {
name: "get_utc_time", description: "Read the current UTC time.",
parameters: { type: "object", properties: {}, additionalProperties: false },
} }];
const messages = [{ role: "user", content: "Call get_utc_time and tell me the UTC time." }];
for (let round = 0; round < 4; round++) {
const response = await fetch("https://api.aetherapi.dev/v1/chat/completions", {
method: "POST",
headers: { Authorization: "Bearer " + apiKey, "Content-Type": "application/json" },
body: JSON.stringify({ model, messages, tools }),
});
const result = await response.json();
if (!response.ok || result.error) throw new Error("Request failed: " + (result.error?.code || response.status));
const choice = result.choices?.[0];
const message = choice?.message;
if (!message) throw new Error("Missing assistant message");
if (!["stop", "tool_calls"].includes(choice.finish_reason)) throw new Error("Incomplete assistant turn");
const calls = message.tool_calls || [];
if (!calls.length) {
if (choice.finish_reason !== "stop") throw new Error("Missing tool calls");
console.log(message.content);
break;
}
if (choice.finish_reason !== "tool_calls") throw new Error("Unexpected tool-call finish reason");
// Validate the whole batch before executing any function.
for (const call of calls) {
if (!call.id || call.type !== "function" || call.function?.name !== "get_utc_time") {
throw new Error("Unknown tool call");
}
const args = JSON.parse(call.function.arguments);
if (!args || Array.isArray(args) || typeof args !== "object" || Object.keys(args).length) {
throw new Error("get_utc_time expects an empty object");
}
}
messages.push(message);
for (const call of calls) {
messages.push({ role: "tool", tool_call_id: call.id,
content: JSON.stringify({ utc: new Date().toISOString() }) });
}
if (round === 3) throw new Error("Tool round limit reached");
}