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

Practical Use Cases

Lesson 16 of 16 Updated July 30, 2026 Developer
On this page
  1. Code Execution in Production
  2. Financial Analysis
  3. Statistical Analysis
  4. Scientific Computing
  5. Data Processing and Cleaning
  6. Complete Workflow Architecture
  7. Key Takeaways
  8. Test Your Knowledge

Code Execution in Production

This final lesson covers practical use cases for Code Execution to give you a clear picture of what you can build. Each example illustrates an application domain with the associated prompt and logic.

Financial Analysis

Portfolio Optimization

Code Execution is particularly well-suited for financial modeling. The sandbox includes NumPy and SciPy, which handle the necessary optimization calculations:

input_text = """
Portfolio of 5 assets with the following monthly returns (12 months):

Asset A: [2.1, -0.5, 1.8, 3.2, -1.1, 2.5, 0.8, 1.2, -0.3, 2.8, 1.5, 0.9]
Asset B: [1.5, 0.2, 2.1, 1.8, 0.5, 1.2, 2.3, -0.1, 1.6, 0.8, 1.9, 2.2]
Asset C: [-0.3, 3.5, -1.2, 4.1, -0.8, 2.9, -0.5, 3.2, -1.0, 3.8, -0.2, 2.7]
Asset D: [0.8, 0.9, 0.7, 1.1, 0.6, 0.8, 1.0, 0.5, 0.9, 0.7, 1.2, 0.8]
Asset E: [3.5, -2.1, 4.2, -1.5, 3.8, -0.9, 2.7, -1.8, 3.1, -0.5, 4.5, -1.2]

Calculate:
1. The correlation matrix between assets
2. The minimum variance portfolio
3. The optimal portfolio (maximum Sharpe ratio, risk-free rate = 4% annual)
4. The efficient frontier (10 points)

Present allocations as percentages.
"""

The model will write code using NumPy for the correlation matrix and SciPy for constrained optimization.

Sharpe Ratio and Risk Indicators

input_text = """
Daily returns of a fund over 20 days:
[0.12, -0.05, 0.08, 0.15, -0.03, 0.22, -0.11, 0.06, 0.09, -0.07,
 0.14, 0.03, -0.08, 0.18, 0.01, -0.04, 0.11, 0.07, -0.02, 0.13]

Calculate the Sharpe ratio, Sortino ratio, Value at Risk (95%),
and maximum drawdown. Annual risk-free rate = 4%.
"""

Statistical Analysis

Hypothesis Testing

SciPy lets you perform rigorous statistical tests:

input_text = """
Group A (treatment): [85, 88, 91, 87, 93, 89, 86, 92, 90, 88]
Group B (control): [78, 82, 80, 85, 79, 83, 81, 77, 84, 80]

Perform a Student's t-test to determine if the difference between
the two groups is statistically significant (alpha = 0.05).
Include hypotheses, test statistic, p-value, and conclusion.
"""

Regression and Prediction

input_text = """
Monthly sales data (in thousands of euros):
Month (x): [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
Sales (y): [45, 52, 48, 61, 55, 67, 72, 68, 75, 80, 78, 85]

1. Perform linear regression
2. Calculate R² and standard error
3. Predict sales for months 13, 14, 15
4. Test if the trend is significant
"""

Scientific Computing

Solving Differential Equations

input_text = """
Solve the second-order differential equation:
y'' + 2y' + 5y = 0
Initial conditions: y(0) = 1, y'(0) = 0
Interval: t from 0 to 10

Plot the solution and identify the behavior (damped oscillation,
critical, overdamped).
"""

Monte Carlo Simulations

input_text = """
Monte Carlo simulation to estimate the probability that a project
is profitable.

Parameters (normal distributions):
- Monthly revenue: mean = €50,000, std dev = €10,000
- Monthly fixed costs: €30,000
- Variable costs: 15% to 25% of revenue (uniform)
- Project duration: 12 months

Run 10,000 simulations. Calculate the probability of profitability
and the net profit distribution. Present the 5th, 50th, and 95th percentiles.
"""

Data Processing and Cleaning

input_text = """
Raw data (CSV with common issues):

name,age,salary,city
Alice,32,45000,Paris
Bob,,52000,Lyon
Charlie,28,NA,Marseille
Diana,45,62000,paris
Eve,33,48000,LYON
Frank,-5,55000,Toulouse
Grace,29,999999,Bordeaux

1. Identify and handle missing values
2. Correct inconsistencies (negative age, outlier salary)
3. Normalize city names (consistent casing)
4. Calculate descriptive statistics on cleaned data
5. Document each transformation performed
"""

Complete Workflow Architecture

For production applications, combine tools in a structured workflow:

  1. Web Search retrieves up-to-date data (prices, indicators, news)
  2. Code Execution analyzes retrieved data (calculations, statistics, charts)
  3. Your application formats and presents results to the user

This pattern applies to many domains: finance, competitive intelligence, market analysis, automated reporting.

Key Takeaways

  • Code Execution excels at financial analysis (Sharpe, portfolio optimization, VaR)
  • Statistical tests and regressions are a natural use case with SciPy
  • Monte Carlo simulations work well within the sandbox memory limits
  • Data cleaning with Pandas is a practical daily use case
  • Combine Web Search + Code Execution for complete automated workflows
  • Provide structured data and precise instructions for better results

Test Your Knowledge

Web, X, code: Grok’s server tools in five checks.

1. What are the two categories of Grok tools?

Answer: Integrated tools, executed server-side by xAI (web search, X search, code execution), and your custom functions, executed by your code—two worlds that combine.

2. How does the execution flow of an integrated tool work?

Answer: In five server-side steps: the model decides, the tool executes at xAI, results return to the model, which responds—you receive the final answer with traces and citations.

3. What do Web Search and X Search filters allow?

Answer: Framing sources: allowed domains on the web, handles and dates on X—freshness and reliability are controlled, and citations document every response.

4. What can Code Execution do?

Answer: Execute Python in a sandbox—common libraries available, strict security constraints: calculations, transformations, and verifications are delegated to exact code.

5. What should you monitor regarding costs?

Answer: Each tool invocation is billed in addition to tokens: target searches, limit depths, and activate only tools useful to the request.

Server capabilities ready out of the box, controlled by your filters and budget: integrated tools make Grok an agent—you remain the director.