Skip to main content
Lesson 15 of 16 · Integrated Tools: Web, X, Code

Practical Limits and Best Practices

Lesson 15 of 16 Updated July 30, 2026 Developer
On this page
  1. Getting the Most Out of Code Execution
  2. Model Selection
  3. Temperature Setting
  4. Structuring Your Prompts
  5. Error Handling
  6. Validating Results
  7. Combining Code Execution with Web Search
  8. Key Takeaways

Getting the Most Out of Code Execution

Now that you understand the sandbox’s technical constraints, this lesson focuses on best practices for reliable results and optimized use of Code Execution. These recommendations come from production use and xAI guidance.

Model Selection

xAI recommends using grok-4.20-0309-reasoning for code generation. This model generates more structured, better-commented code with fewer errors than non-reasoning models. The difference is particularly visible on complex problems requiring multi-step reasoning.

Temperature Setting

For mathematical calculations and data analysis, use low temperatures:

response = client.responses.create(
    model="grok-4.20-0309-reasoning",
    input="Calculate the weighted standard deviation of these values: ...",
    tools=[client.tools.code_execution()],
    temperature=0.1
)
  • 0.0 to 0.1: pure mathematical calculations, deterministic results
  • 0.1 to 0.3: data analysis with interpretation
  • 0.3 to 0.7: creative code generation (avoid for critical calculations)

A high temperature can produce different algorithmic approaches on each call, making results non-reproducible.

Structuring Your Prompts

Include Data in the Prompt

Since the sandbox has no network access, you must include any data to be analyzed in the prompt:

data = """
Month,Sales,Costs
Jan,15000,8000
Feb,18000,9500
Mar,22000,11000
Apr,19000,10000
"""

response = client.responses.create(
    model="grok-4.20-0309-reasoning",
    input=f"Analyze this sales data and calculate monthly margins:\n{data}",
    tools=[client.tools.code_execution()],
    temperature=0.1
)

Specify the Expected Output Format

The model produces better results when you specify what you expect:

input_text = """
Data: [2.5, 3.1, 4.7, 2.8, 5.2, 3.9, 4.1]

Calculate:
1. The mean and median
2. The standard deviation
3. The coefficient of variation
4. Identify outliers (IQR method)

Present the results in a formatted table.
"""

Break Down Complex Problems

For multi-step analyses, structure your prompt sequentially:

input_text = """
Step 1: Load this portfolio return data
Step 2: Calculate the annualized return
Step 3: Calculate volatility (annualized standard deviation)
Step 4: Calculate the Sharpe ratio (risk-free rate = 4%)
Step 5: Present a summary with all indicators
"""

Error Handling

The model may encounter execution errors. In this case, it usually attempts to correct its code and re-execute. This means:

  • Multiple invocations may be billed for a single request
  • The final result may take longer to arrive
  • In rare cases, the model may fail to correct the error

To minimize errors, be precise in your instructions and provide data in a clear, structured format (CSV, JSON, tables).

Validating Results

Code Execution is a powerful tool, but not infallible. For critical calculations:

  • Verify results on a known subset
  • Ask the model to explain its methodology
  • Compare with results obtained by other means
  • Request the source code to review if necessary

The model often includes the executed code in its response, allowing you to verify the logic.

An advanced use case involves combining the two tools:

response = client.responses.create(
    model="grok-4.20-0309-reasoning",
    input="Find the current stock prices for Apple, Google, and Microsoft, then calculate their correlation over the last month",
    tools=[
        {"type": "web_search"},
        {"type": "code_interpreter"}
    ],
    temperature=0.1
)

The model uses Web Search to retrieve data, then Code Execution to analyze it. This workflow is fully automatic.

Key Takeaways

  • Use grok-4.20-0309-reasoning for the best code quality
  • Low temperature (0.0–0.3) for calculations, higher for creative code
  • Include data in the prompt (the sandbox has no network access)
  • Structure your requests: expected format, sequential steps
  • Validate critical results—the model is not infallible
  • The Web Search + Code Execution combination is a powerful workflow