DEV Community

talor
talor

Posted on

SERP API + LangChain: Build a Web-Connected AI Agent in 10 Lines of Code

LLMs have a knowledge cutoff—we all know this. The simplest way to give your AI real-time search capability is to equip it with a search tool.

Today, we'll use TalorData SERP API + LangChain to build an Agent that decides when to search, all in under 10 lines of code.

What You'll Need

bash
pip install langchain langchain-talordata
Enter fullscreen mode Exit fullscreen mode

Grab your API key from the TalorData dashboard (new users get 1,000 free requests).

The Core Code

python
import os
from langchain.agents import initialize_agent, Tool
from langchain_openai import ChatOpenAI
from langchain_talordata import TalorSerpTool

# Set up API keys
os.environ["TALOR_API_KEY"] = "your-api-key"
os.environ["OPENAI_API_KEY"] = "your-openai-key"

# Initialize the search tool
search_tool = TalorSerpTool()
tools = [Tool(name="Search", func=search_tool.run, description="Search real-time information")]

# Create the Agent
llm = ChatOpenAI(model="gpt-4o-mini")
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)

# Run it
result = agent.run("Who are the major players in the SERP API market in 2026?")
print(result)
Enter fullscreen mode Exit fullscreen mode

What Happens Under the Hood

  1. The Agent receives the question and determines it doesn't have enough knowledge to answer
  2. It automatically calls the Search tool, passing the query to TalorData API
  3. TalorData returns structured JSON results with titles, links, and snippets
  4. The Agent synthesizes the information and delivers a coherent answer

Why TalorData?

  • Sub-second latency – P90 < 1 second, built for real-time AI workloads
  • Pay-per-success – you only pay for successful requests
  • Structured JSON output – no parsing headaches, ready for LLM consumption
  • Multi-engine – Google, Bing, Yandex, and DuckDuckGo from one endpoint

Try It Yourself
The full code is above—copy, paste, and run. New users get 1,000 free requests to test it out.

Docs: https://docs.talordata.com/serp-api/integration

Top comments (0)