Python 88.3%
TypeScript 7.6%
Shell 4.1%
1// STATUS: LIVE_VERIFIED — executed 2026-09-19 (exit 0 in 5.0s)2// Responses tool loop in TypeScript: function_call → local execution → function_call_output with previous_response_id.3import OpenAI from "openai";4import fs from "node:fs";5const log = (note: string) => fs.appendFileSync("reports/live-requests.jsonl", JSON.stringify({ ts: new Date().toISOString().replace(/\.\d+Z$/, "Z"), provider: "xai", method: "POST", path: "/v1/responses", status: 200, est_cost_usd: 0.0005, note }) + "\n");6const client = new OpenAI({ apiKey: process.env.XAI_API_KEY, baseURL: "https://api.x.ai/v1" });7const tools: any = [{ type: "function", name: "get_weather", description: "Get weather for a city", parameters: { type: "object", properties: { city: { type: "string" } }, required: ["city"] } }];8let resp = await client.responses.create({ model: "grok-4.3", input: "Weather in Paris?", tools, tool_choice: { type: "function", name: "get_weather" }, max_output_tokens: 2000 } as any);9log("example xai/tools/function-calling/loop.ts turn 1");10for (let turn = 0; turn < 5; turn++) {11 const outputs: any[] = [];12 for (const item of resp.output as any[]) {13 if (item.type === "function_call") { const args = JSON.parse(item.arguments); console.log("client call:", item.name, args); outputs.push({ type: "function_call_output", call_id: item.call_id, output: JSON.stringify({ city: args.city, temp_c: 18 }) }); }14 }15 if (!outputs.length) break;16 resp = await client.responses.create({ model: "grok-4.3", input: outputs, tools, previous_response_id: resp.id, max_output_tokens: 2000 } as any);17 log(`example xai/tools/function-calling/loop.ts turn ${turn + 2}`);18}19console.log("final:", resp.output_text);20if (!resp.output_text.includes("18")) throw new Error("unexpected");21