Python 88.3%
TypeScript 7.6%
Shell 4.1%
1#!/usr/bin/env python32# STATUS: LIVE_VERIFIED — executed 2026-09-19 (exit 0 in 5.0s)3# Generic agentic loop on xAI Responses: client-side function_call items are executed locally and returned with previous_response_id;4# server-side items (web_search_call, custom_tool_call, code_interpreter_call, mcp_call, file_search_call) need no action.5import json, os, pathlib, sys6ROOT = pathlib.Path(__file__).resolve().parents[3]; sys.path.insert(0, str(ROOT))7from scripts import live # noqa: E4028from openai import OpenAI # noqa: E402910client = OpenAI(api_key=os.environ["XAI_API_KEY"], base_url="https://api.x.ai/v1")11TOOLS = [{"type": "function", "name": "get_weather", "description": "Get weather for a city", "parameters": {"type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"]}}]12SERVER_ITEMS = {"web_search_call", "x_search_call", "custom_tool_call", "code_interpreter_call", "file_search_call", "mcp_call", "image_generation_call"}13resp = client.responses.create(model="grok-4.3", input="Weather in Paris?", tools=TOOLS, tool_choice={"type": "function", "name": "get_weather"}, max_output_tokens=2000)14live.log_request("xai", "POST", "/v1/responses", 200, 0.0005, "example xai/responses/tool_loop.py turn 1")15for turn in range(5):16 outputs = []17 for item in resp.output:18 if item.type == "function_call":19 args = json.loads(item.arguments); print("client call:", item.name, args)20 outputs.append({"type": "function_call_output", "call_id": item.call_id, "output": json.dumps({"city": args["city"], "temp_c": 18})})21 elif item.type in SERVER_ITEMS:22 print("server call:", item.type)23 if not outputs:24 break25 resp = client.responses.create(model="grok-4.3", input=outputs, tools=TOOLS, previous_response_id=resp.id, max_output_tokens=2000)26 live.log_request("xai", "POST", "/v1/responses", 200, 0.0005, f"example xai/responses/tool_loop.py turn {turn+2}")27print("final:", resp.output_text)28assert "18" in resp.output_text29