The 5-Step Execution Flow
On this page
How Grok Decides to Use a Tool
When you enable one or more tools in your API request, the model doesn’t necessarily use them. It follows a structured five-step decision process to determine if a tool is needed, which one to use, and how to integrate the results into its response.
Understanding this flow helps you anticipate the model’s behavior and optimize your prompts for accurate results.
The 5 Steps of the Flow
Step 1: Request Analysis
The model starts by analyzing the user’s message to identify what is being asked. It evaluates the nature of the question: Is it a factual request requiring recent data? A complex calculation? An opinion question that requires no tools?
This analysis is critical. If the model already has the information in its training data and no real-time data is required, it may choose to respond directly without activating a tool.
Step 2: Call Decision
Based on its analysis, the model decides whether to call a tool or respond directly. This decision hinges on three factors. First, the nature of the question: a query about current events naturally points toward Web Search, while a question about what people are saying right now on a topic points toward X Search. Second, the scope you defined: the model chooses only from tools enabled in your tools array—an absent tool simply doesn’t exist for it. Finally, its own confidence: if it believes it can answer correctly from memory, it saves the call, which is the desirable behavior most of the time.
You retain control over this decision via the prompt: “Search for the latest information on…” clearly points toward Web Search, while a neutral formulation leaves the model to decide on its own.
Step 3: Execution
Processing differs by tool category:
- Built-in tool: Execution is automatic and transparent. The xAI server performs the search or code execution, and the result is transmitted directly to the model. You don’t see this step in the API response
- Function Calling: The model returns an object describing the function to call and its arguments. Your code must execute the function and return the result via a new request
Step 4: Result Processing
Once results are obtained, the model evaluates them before responding—and this is where the flow becomes truly agentic. If the results suffice, it moves directly to formulating the final response. If they are insufficient—search too narrow, results off-topic—it can issue another call with adjusted parameters, widening or refining its query, without you having to intervene. And for a truly complex question, it chains several tools: a web search to collect figures, then Code Execution to analyze them, for example.
This iterative behavior is valuable for response quality, but it has a direct consequence on the bill: each tool call is billed separately, and a question that triggers three searches costs three invocations. Keep this mechanism in mind when estimating your costs.
Step 5: Final Response
The model formulates its response by integrating the collected information. If web or X sources were consulted, the response automatically includes citations with the original URLs. These citations are accessible in the citations field of the response object.
Practical Implications
Cumulative Costs
The model can make multiple tool calls for a single user request. Each call is billed independently in addition to token consumption. An ambiguous prompt can generate unnecessary calls and increase your costs.
Control via Prompt
You cannot force the model to use a specific tool, but you can strongly guide it via your system prompt or user message. Clear instructions reduce unnecessary calls.
Non-Deterministic Behavior
The choice to use a tool or not can vary between two identical calls, especially with high temperature settings. For reproducible results, use a low temperature (0.0 to 0.3).
Key Takeaways
- The model follows a 5-step flow: analysis, decision, execution, processing, response
- It only requests a tool if it deems it necessary—it’s not automatic
- Multiple calls are possible and increase costs
- A precise prompt reduces unnecessary calls and improves relevance
- Citations are automatically included when sources have been consulted