How to Intermediate · 3 min read

How to give AI agent web search capability

Quick answer
To give an AI agent web search capability, integrate a web search API (like Bing Search or Google Custom Search) to fetch real-time results, then feed those results as context to a large language model (LLM) such as gpt-4o or claude-3-5-sonnet-20241022. This enables the agent to answer queries with fresh, relevant information beyond its training data.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install openai>=1.0
  • Access to a web search API (e.g., Bing Search API or Google Custom Search API)
  • pip install requests

Setup

Install necessary Python packages and set environment variables for your OpenAI API key and web search API key.

  • Use pip install openai requests to install dependencies.
  • Set OPENAI_API_KEY and SEARCH_API_KEY in your environment.
bash
pip install openai requests

Step by step

This example shows how to query a web search API, then pass the search results as context to an LLM to generate an informed answer.

python
import os
import requests
from openai import OpenAI

# Initialize OpenAI client
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

# Function to perform web search using Bing Search API
# Replace endpoint and headers as per your search API provider

def web_search(query):
    endpoint = "https://api.bing.microsoft.com/v7.0/search"
    headers = {"Ocp-Apim-Subscription-Key": os.environ["SEARCH_API_KEY"]}
    params = {"q": query, "count": 3}
    response = requests.get(endpoint, headers=headers, params=params)
    response.raise_for_status()
    results = response.json()
    snippets = []
    for item in results.get("webPages", {}).get("value", []):
        snippets.append(item["snippet"])
    return "\n".join(snippets)

# Compose prompt with search results

def create_prompt(query, search_results):
    return f"You are an AI assistant with access to recent web search results.\nSearch results:\n{search_results}\n\nAnswer the question based on the above information:\n{query}"

# Main function

def main():
    user_query = "What are the latest advancements in AI for 2026?"
    search_results = web_search(user_query)
    prompt = create_prompt(user_query, search_results)

    response = client.chat.completions.create(
        model="gpt-4o",
        messages=[{"role": "user", "content": prompt}]
    )
    print("AI agent answer:")
    print(response.choices[0].message.content)

if __name__ == "__main__":
    main()
output
AI agent answer:
The latest advancements in AI for 2026 include breakthroughs in large language models, improved multimodal understanding, and more efficient training techniques...

Common variations

You can adapt this pattern by:

  • Using claude-3-5-sonnet-20241022 or gemini-1.5-pro instead of gpt-4o.
  • Implementing async calls with httpx or aiohttp for faster search and LLM requests.
  • Streaming LLM responses for real-time output.
  • Using different search APIs like Google Custom Search or SerpAPI depending on your needs.

Troubleshooting

  • If you get HTTP 401 errors, verify your API keys and environment variables.
  • If search results are empty, check your query parameters and API usage limits.
  • For slow responses, consider caching search results or using async requests.
  • If the LLM ignores search context, ensure the prompt clearly instructs it to use the provided information.

Key Takeaways

  • Integrate a web search API to fetch real-time data for your AI agent.
  • Feed search results as context in the prompt to an LLM like gpt-4o for up-to-date answers.
  • Use environment variables for all API keys to keep credentials secure.
  • Consider async and streaming for better performance and user experience.
  • Clear prompt instructions ensure the LLM uses the search data effectively.
Verified 2026-04 · gpt-4o, claude-3-5-sonnet-20241022, gemini-1.5-pro
Verify ↗