Learning resourcesReviewed · xAI

xAI API function calling guide (Grok tool use)

This is xAI's documentation page on function calling in the Grok API. It is for developers who want a model to request calls to their own code. The main catch is that it is one reference page tied to xAI's API and the grok-4.7 model name, and it gives no pricing, rate limits or licence terms. It explains a five-step loop: define tools with JSON Schema, send them, receive a tool call, run the function yourself, and return the result. It shows four client routes: the Vercel AI SDK, the OpenAI Python SDK, the xAI Python SDK and curl. The three SDK examples show the function being run and the conversation continuing. The curl example shows only the first request. It also covers Pydantic schemas, mixing custom tools with built-in web and X search, tool choice, parallel calls and schema rules.

Researched 15 Nov 2026

In short.

This is xAI's documentation page on function calling in the Grok API. It is for developers who want a model to request calls to their own code. The main catch is that it is a single reference page tied to xAI's API and the grok-4.7 model name, and it gives no pricing, rate limits or licence terms. The model never runs your code. It returns a tool call naming a function and giving JSON arguments, your program runs the function and sends back the result, and the model then continues. The page shows four client routes: the Vercel AI SDK, the OpenAI Python SDK pointed at [https://api.x.ai/v1], the xAI Python SDK, and curl. The three SDK examples show the function being run and the conversation continuing. The Vercel AI SDK does this loop for you. The curl example shows only the first request with a tool definition, not the step that sends back a result. The page also documents schemas generated from Pydantic, mixing custom tools with the built-in web_search and x_search tools, tool_choice values, parallel calls, a limit of 350 tools per request, rules for the root schema and a streaming caveat. Nothing was executed in this analysis, so every behaviour below is as the page describes it, not as observed.

Researched and written by an AI model and checked by a second AI model. How it works

The analysis covers the complete selected page at [https://docs.x.ai/developers/tools/function-calling.md], which is one captured file, selected-page.txt (14,178 bytes). The page links to an Advanced Usage page. That linked page is outside the selected scope and was not read. No licence file or notice was captured with the page. No code samples were run and no API calls were made, so the page's described behaviour, the accuracy of its samples, model availability, pricing and rate limits are not tested or established.

The overview

What this resource is

This is a single documentation page titled Function Calling, in the Tools section of xAI's developer docs, captured from [https://docs.x.ai/developers/tools/function-calling.md]. It teaches one feature of the xAI API: letting a Grok model request calls to custom functions that you define and run yourself.

It is a guide, not software you install. The page names no version number or author and shows no release date. Every example that explicitly selects a model uses grok-4.7. Several schema and handler snippets name no model at all.

Overall assessment

For a developer who already has, or plans to get, access to the xAI API, this page is a compact and practical starting point. It gives a short numbered workflow, samples in four styles that look ready to run, and precise rules for tool definitions, including a documented 400 error for invalid root schemas.

Its limits come from being one page. It says nothing about pricing, rate limits, how to get an API key, or how reliably the model produces valid arguments. Beyond the example requests that use grok-4.7, it gives no list of which models support function calling. Treat it as a reference for how the feature works, not as an evaluation of tool-use quality. Nothing here was run, so the samples are unverified.

Who it is for

The page suits developers building applications or agents on xAI's Grok models who want those models to look things up or take actions, such as querying a database, calling a weather or internal API, or saving results. The examples are small (temperature, cloud ceiling, save_to_database), so a beginner who can write basic Python or JavaScript and read JSON can follow them.

It assumes you can work with JSON Schema, environment variables and an HTTP API, so non-programmers will not get much from it. Experienced developers can use it as a reference for concrete details: the tool_choice values, the 350-tool limit, the root-schema rules and the fact that a streamed function call arrives in one chunk. The page does not compare xAI's function calling with other providers, so cross-provider compatibility and comparisons are unknown from this page.

What it covers

The page covers eight topics. First is the five-step loop: define tools with a name, description and JSON Schema parameters; include them in the request; receive a tool_call when the model needs external data; run the function locally; and return the result so the model can continue.

Second is a Quick Start in four forms: the Vercel AI SDK (streamText with a tool helper, a zod inputSchema and stopWhen: stepCountIs(5)), the OpenAI Python SDK pointed at xAI, a curl request to /v1/responses, and the xAI Python SDK (xai_sdk). Third is building parameter schemas from Pydantic models with model_json_schema(). Fourth is handling calls with a map from function names to functions. Fifth is combining custom functions with the built-in web_search and x_search tools. Sixth is the tool_choice values: auto, required, none, or an object that forces a specific function. Seventh is parallel function calling, which is on by default and can be turned off with parallel_tool_calls: false. Last are a tool schema reference and a complete Vercel AI SDK example with two tools and streamed events (text-delta, tool-call, tool-result).

Using it

What you need

What you need depends on the route you pick. The curl example needs no SDK. It uses curl, network access to [https://api.x.ai/v1/responses] and an API key sent as a Bearer token from the XAI_API_KEY environment variable. The OpenAI Python SDK example needs the openai package, sets base_url to [https://api.x.ai/v1] and reads the key from XAI_API_KEY. The xAI Python SDK example needs xai_sdk and reads the key from XAI_API_KEY to pass to Client. The Vercel AI SDK example needs @ai-sdk/xai, ai and zod, but it does not show how the API key is supplied, so that setting is unknown from this page. You need Pydantic only for the schema-generation examples in the Defining Tools with Pydantic section.

The page does not say how to get an API key, which account type or plan is required, which Grok models other than grok-4.7 support function calling, or which package versions the samples were written for. These details are unknown from this evidence.

How it works

The model does not execute anything. You send a request that includes tool definitions. If the model decides it needs a tool, it returns a call naming the function and giving its arguments as JSON. Your program runs the function and sends the result back, and the model uses that result to write its answer.

In the OpenAI Python SDK flow, the response output contains an item of type function_call with a name, an arguments field holding a JSON string, and a call_id. You reply with an item of type function_call_output that carries the same call_id and a string output, and you pass previous_response_id so the server links the turns. In the xAI SDK flow, response.tool_calls holds the calls. You append the response to the chat, append a tool_result for each call, and call chat.sample() again. The Vercel AI SDK runs this loop for you, up to the step count you set.

A tool definition has three fields. name is required and must be unique, and a request can include at most 350 tools. description is optional and treated as empty if left out, though a clear one helps the model choose. parameters is a required JSON Schema. The root of parameters must be an object, or an anyOf or oneOf in which every branch is an object. Any other root is rejected with a 400 error naming the tool, because it cannot be compiled into a tool-call grammar.

When you mix tools, the page states that built-in tools (web_search, x_search) execute automatically on SpaceXSI servers, while custom functions execute in your application: custom tools pause execution and hand control back to you. A warning says that with streaming, a function call arrives whole in one chunk rather than spread across chunks.

Setup

The page implies a setup path that differs by route. For the curl, OpenAI Python SDK and xAI Python SDK routes, set the XAI_API_KEY environment variable that those samples read, define one or more tools, and send a request with grok-4.7 and the tools list. Then write the loop that runs each function the model asks for and returns its result. The OpenAI Python SDK sample points the client at xAI by setting base_url to [https://api.x.ai/v1]. The curl sample shows only the first request, not the follow-up that returns a result. For the Vercel AI SDK route, you attach an execute function to each tool and the SDK runs the loop automatically, up to the step limit set with stopWhen: stepCountIs(5). That sample does not show how the key is configured. The page does not say whether code written for the OpenAI SDK against other services works here unchanged.

The page gives no install commands, version pins or steps for creating a key. For complete examples of tool loops that mix server-side and client-side tools, it points to an Advanced Usage page, which this analysis did not read.

Worked example

The page's central example asks, What is the temperature in San Francisco? It defines get_temperature with a required location string and an optional unit, which can be celsius or fahrenheit. The model returns a call for that function. The sample code returns a hard-coded result (59 degrees Fahrenheit or 15 Celsius), and the model then writes the final answer. A second example adds get_ceiling and asks about Denver. The complete Vercel AI SDK example defines temperature and cloud-ceiling tools and prints tool-call and tool-result events as they stream.

The weather values are hard-coded stand-ins, and one comment says to call a real weather API in production. These samples show the control flow, not a real integration. Nothing was run here, so the outputs are not confirmed.

Assessment & evidence

Limitations and cautions

Scope: the page covers only how function calling works. The page says clear descriptions help the model choose tools, but provides no detailed description-writing guidance or measured reliability. It gives no guidance on retries, timeouts or error handling beyond a check for unknown function names, and it does not say how often the model picks the wrong tool or produces malformed arguments.

Streaming: a function call arrives in one chunk, so you cannot show partial arguments as they form.

Schema rules: the root schema must be an object or a union of objects. A scalar, an array or a union with a non-object branch fails with a 400 error. A request can include at most 350 tools.

Safety of your own code: the model chooses the function name and arguments, so treat them as untrusted input. Only the OpenAI SDK sample checks for an unknown function name. The xAI SDK sample calls tools_map[name](**args) directly. Validate arguments and limit what each function can do, especially for tools that write data, such as the page's save_to_database example. This is the analysis's own advice, not something the page says.

Parallel calls: the page says to process all calls before continuing. However, the OpenAI SDK loop sends a separate follow-up request for each call and reassigns response each time. With several parallel calls, this pattern may not match the single-batch flow the text describes. The page does not show a multi-call version for that SDK, so test it before relying on it.

Inconsistencies: the Quick Start examples default unit to fahrenheit, while the schema reference example defaults it to celsius. The tool_choice table shows a forced-function object of the form {"type": "function", "function": {"name": "..."}}, but none of the page's code samples use tool_choice. The page therefore does not show how to pass a forced choice in each client route. Check this against live behaviour or the API reference.

Why this assessment

The assessment rests entirely on the text of the one captured page. The page is clearly organised, states concrete rules and gives code in several client styles, so it reads as a usable guide. Those rules include the 350-tool cap, the object-root requirement and its 400 error, function calls arriving whole when streamed, and parallel calls being on by default. The doubts above come from gaps in what the page says, not from anything shown to be wrong in a run.

No code was executed, no API calls were made and no outside documentation was consulted. The findings describe what the page claims, not what the service does.

Alternatives and related routes

Within the page, you can choose between four client routes. The Vercel AI SDK handles the loop of running tools and continuing for you. The OpenAI Python SDK route uses the openai package with base_url set to [https://api.x.ai/v1]. The xAI Python SDK route uses the xai_sdk package. Raw curl shows the HTTP request directly. The page states that built-in tools such as web_search and x_search execute automatically on SpaceXSI servers, while custom functions execute in your application, so the built-in tools need no function of your own. The page shows them only alongside a custom function. Using them without any custom tools is not demonstrated.

The page does not compare xAI's function calling with other providers or API formats. Cross-provider compatibility and comparisons are therefore unknown from this page, and no other provider's documentation was reviewed.

Evidence base

One captured file supports this report: the full text of the selected page, selected-page.txt (14,178 bytes), from [https://docs.x.ai/developers/tools/function-calling.md]. It includes prose, two warning notes, a tool-choice table, a schema reference table and code samples in JavaScript (Vercel AI SDK), Python (OpenAI SDK and xAI SDK), bash (curl) and JSON. All statements about parameters, limits and behaviour come from that text. No independent test, benchmark or second source backs them up.

Access & freshness

Costs

The page states no prices, token charges, free tier, rate limits or billing terms for function calling, for grok-4.7 or for the built-in web_search and x_search tools. The cost is unknown from this evidence. Tool definitions and tool results are normally part of the request text a model has to process, but this page does not say how they are billed. Check xAI's pricing information directly before relying on any cost estimate.

Rights and licence

The captured page contains no licence, copyright notice or terms of reuse for its text or code samples. This evidence therefore does not establish whether you may copy the samples into your own projects or redistribute the page. Use of the API itself is presumably covered by xAI's own terms, but those terms are not on this page and were not reviewed. Read them before building a commercial product.

Freshness

The page was captured on 15 November 2026. It shows no publication date, last-updated date or changelog, so its age is unknown. Its use of the model name grok-4.7 shows it was written for that model generation, but it is not established whether that model, the endpoints or the SDK helpers are still current. API documentation changes often, so compare the samples with xAI's current docs before building on them.

Suggested next steps

If you already have xAI API access, work through the Quick Start in your preferred client with a stub function, as the page does. Confirm that the model returns a call and then uses your result. Then replace the stub with a real function and add argument validation and error handling.

Before committing, check xAI's current pricing and rate limits, confirm that your chosen model supports tool calling, and read the API terms. If the model may return several tool calls at once, test your loop against that case. If you need the full loop that mixes built-in and custom tools, read the Advanced Usage page the guide links to, which this analysis did not cover.

Evidence for this report

selected-page.txtselected · checked 2026-11-15