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_calling is supported for the endpoint; with unknown, verify the route yourself first.
  • Aether forwards the tool definitions; your application validates arguments and executes the functions.
  • Return one role: tool message per call with the returned tool_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.

The cycle

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");
}