How to beginner · 3 min read

How to use web search tool in CrewAI

Quick answer
Use the CrewAI Python SDK to access the web search tool by initializing the client with your API key and calling the search method with your query. This returns relevant web results you can process or display in your application.

PREREQUISITES

  • Python 3.8+
  • CrewAI API key
  • pip install crewai

Setup

Install the crewai Python package and set your API key as an environment variable for secure authentication.

  • Run pip install crewai to install the SDK.
  • Set your API key in your environment: export CREWAI_API_KEY='your_api_key_here' (Linux/macOS) or set CREWAI_API_KEY=your_api_key_here (Windows).
bash
pip install crewai

Step by step

Use the following Python code to perform a web search using CrewAI's web search tool. This example initializes the client, sends a search query, and prints the top results.

python
import os
from crewai import CrewAI

# Initialize CrewAI client with API key from environment
client = CrewAI(api_key=os.environ["CREWAI_API_KEY"])

# Perform a web search query
query = "Latest AI technology trends 2026"
results = client.web_search.search(query=query, max_results=5)

# Print the titles and URLs of the top results
for i, result in enumerate(results, 1):
    print(f"{i}. {result.title} - {result.url}")
output
1. AI Technology Trends in 2026 - https://example.com/ai-trends-2026
2. Top AI Innovations This Year - https://example.com/top-ai-innovations
3. How AI is Changing Tech in 2026 - https://example.com/ai-changing-tech
4. Emerging AI Tools and Platforms - https://example.com/emerging-ai-tools
5. AI Industry Forecast 2026 - https://example.com/ai-industry-forecast

Common variations

You can customize the search by adjusting parameters such as max_results or using asynchronous calls if supported by the SDK. You can also switch to different CrewAI models or tools if your use case requires it.

python
import asyncio
import os
from crewai import CrewAI

async def async_search():
    client = CrewAI(api_key=os.environ["CREWAI_API_KEY"])
    results = await client.web_search.search_async(query="AI ethics 2026", max_results=3)
    for r in results:
        print(f"{r.title} - {r.url}")

asyncio.run(async_search())
output
AI Ethics in 2026 - https://example.com/ai-ethics
Responsible AI Development - https://example.com/responsible-ai
AI Governance Trends - https://example.com/ai-governance

Troubleshooting

  • If you get authentication errors, verify your CREWAI_API_KEY environment variable is set correctly.
  • If no results return, check your query syntax or increase max_results.
  • For network issues, ensure your internet connection is stable and the CrewAI service endpoint is reachable.

Key Takeaways

  • Initialize CrewAI client with your API key from environment variables for secure access.
  • Use the web_search.search method to perform web queries and retrieve relevant results.
  • Customize search parameters like max_results and use async methods for improved performance.
  • Always handle authentication and network errors by verifying environment setup and connectivity.
Verified 2026-04
Verify ↗