Skip to main content
Lesson 10 of 12 · Async Requests & Advanced Patterns

Tool Combination Patterns

Lesson 10 of 12 Updated July 30, 2026 Advanced
On this page
  1. Combine tools for complex tasks
  2. Pattern 1: Search + Analysis
  3. Pattern 2: Multi-source news
  4. Pattern 3: Multi-source extraction with functions
  5. Pattern 4: Cross-validation
  6. Best practices
  7. Key takeaways

Combine tools for complex tasks

Grok’s tools become truly powerful when you combine them. Web search alone provides raw data. Add code execution, and you can analyze that data. Add your custom functions, and you can act on the results. This lesson covers the most useful combination patterns.

Pattern 1: Search + Analysis

The most common use case combines web search with code execution to collect data and draw conclusions:

response = await client.chat.completions.create(
    model="grok-4.5",
    messages=[{
        "role": "user",
        "content": "Look up the stock prices for Tesla, Apple, and Microsoft over the last 30 days. "
                   "Calculate relative performance and identify the trend."
    }],
    tools=[
        {"type": "web_search"},
        {"type": "code_interpreter"}
    ],
    max_turns=5
)

Grok starts with a web search to get financial data, then uses code execution to calculate performance and plot a chart. Everything happens in a single request thanks to sufficient max_turns.

Pattern 2: Multi-source news

Combining Web Search and X Search offers comprehensive coverage on a topic:

response = await client.chat.completions.create(
    model="grok-4.5",
    messages=[{
        "role": "user",
        "content": "What are the reactions to the announcement of the new AI regulation in Europe? "
                   "Compare official sources and reactions on X."
    }],
    tools=[
        {"type": "web_search"},
        {"type": "x_search"}
    ],
    max_turns=5
)

Web search captures press articles and official statements, while X search captures real-time reactions and opinions.

Pattern 3: Multi-source extraction with functions

For more complex workflows, combine server tools with your custom functions:

tools = [
    {"type": "web_search"},
    {"type": "code_interpreter"},
    {
        "type": "function",
        "function": {
            "name": "save_to_database",
            "description": "Saves structured results to the database",
            "parameters": {
                "type": "object",
                "properties": {
                    "category": {"type": "string"},
                    "data": {"type": "object"},
                    "source_urls": {"type": "array", "items": {"type": "string"}}
                },
                "required": ["category", "data"]
            }
        }
    }
]

async def research_and_save(topic: str) -> str:
    messages = [{"role": "user", "content": f"Research and analyze: {topic}. "
                 "Save the structured results."}]

    while True:
        response = await client.chat.completions.create(
            model="grok-4.5",
            messages=messages,
            tools=tools,
            max_turns=5
        )

        message = response.choices[0].message

        if not message.tool_calls:
            return message.content

        messages.append(message)
        for tc in message.tool_calls:
            if tc.function.name == "save_to_database":
                result = await save_to_db(json.loads(tc.function.arguments))
                messages.append({
                    "role": "tool",
                    "tool_call_id": tc.id,
                    "content": json.dumps(result)
                })

In this pattern, Grok searches for information, analyzes it with code, then calls your function to save the results. The workflow is fully autonomous.

Pattern 4: Cross-validation

Use multiple sources to verify the reliability of information:

response = await client.chat.completions.create(
    model="grok-4.5",
    messages=[{
        "role": "user",
        "content": "Verify this claim by cross-referencing sources: "
                   "'France produced 70% of its electricity from nuclear power in 2024.' "
                   "Compare data from multiple official sources."
    }],
    tools=[
        {"type": "web_search"},
        {"type": "code_interpreter"}
    ],
    max_turns=8
)

With a higher max_turns, Grok can perform multiple successive searches to cross-reference information and use code execution to compare figures.

Best practices

Specify tools in the prompt

Don’t just declare tools in the tools parameter. Explicitly state in your prompt what you expect:

# GOOD: the prompt guides tool usage
"""Search for the data, then use code to generate a comparison table.
Save the final result via save_to_database."""

# BAD: the prompt doesn't mention tools
"Give me information on this topic."

Limit tools to needs

Don’t enable every tool if you only need one. Each tool adds context to the system prompt, consuming tokens and potentially distracting the model.

Key takeaways

  • Search + Code Execution lets you collect and analyze data in a single request
  • Web Search + X Search offers comprehensive coverage (articles + reactions)
  • Custom functions extend capabilities beyond what server tools offer
  • Explicitly guide Grok in your prompt on which tools to use
  • Adapt max_turns to workflow complexity (5-8 for multi-tool combinations)
  • Enable only necessary tools to optimize token consumption