ScavioScavio
ProductPricingDocs
Sign InGet Started
  1. Home
  2. Tutorials
  3. How to Add Search to a LangGraph Agent
Tutorial

How to Add Search to a LangGraph Agent

Integrate a web search API as a LangGraph tool node. Give your agent live search results with structured JSON instead of raw HTML scraping.

Get Free API KeyAPI Docs

LangGraph agents need access to live web data to answer current questions, verify facts, and ground their responses. Adding a search tool to a LangGraph graph gives the agent the ability to query Google, Reddit, YouTube, and other platforms on demand. This tutorial shows how to define a Scavio search tool, register it as a LangGraph tool node, and wire it into your agent graph. The agent will be able to call the search tool during execution and receive structured JSON results that it can reason over.

Prerequisites

  • Python 3.10+ installed
  • langgraph and langchain-core packages installed
  • A Scavio API key from scavio.dev
  • Basic understanding of LangGraph state graphs

Walkthrough

Step 1: Define the search tool function

Create a tool function that wraps the Scavio API and returns structured results.

Python
import os, requests
from langchain_core.tools import tool

API_KEY = os.environ["SCAVIO_API_KEY"]

@tool
def web_search(query: str) -> str:
    """Search the web for current information."""
    resp = requests.post("https://api.scavio.dev/api/v1/search",
        headers={"x-api-key": API_KEY},
        json={"platform": "google", "query": query})
    results = resp.json().get("organic_results", [])[:5]
    return "\n".join(f"- {r['title']}: {r.get('snippet', '')}" for r in results)

Step 2: Create the agent state and graph

Set up the LangGraph state graph with the search tool bound to the agent node.

Python
from langgraph.graph import StateGraph, MessagesState
from langgraph.prebuilt import ToolNode
from langchain_openai import ChatOpenAI

tools = [web_search]
model = ChatOpenAI(model="gpt-4o-mini").bind_tools(tools)

def agent(state: MessagesState):
    return {"messages": [model.invoke(state["messages"])]}

tool_node = ToolNode(tools)

Step 3: Wire the graph

Connect agent and tool nodes with conditional routing based on tool calls.

Python
from langgraph.graph import END

def should_continue(state: MessagesState):
    last = state["messages"][-1]
    return "tools" if last.tool_calls else END

graph = StateGraph(MessagesState)
graph.add_node("agent", agent)
graph.add_node("tools", tool_node)
graph.set_entry_point("agent")
graph.add_conditional_edges("agent", should_continue)
graph.add_edge("tools", "agent")
app = graph.compile()

Step 4: Run the agent with a search query

Invoke the agent with a question that requires live web data.

Python
from langchain_core.messages import HumanMessage

result = app.invoke({
    "messages": [HumanMessage(content="What are the top CRM tools for startups in 2026?")]
})
print(result["messages"][-1].content)

Python Example

Python
import os, requests
from langchain_core.tools import tool

@tool
def web_search(query: str) -> str:
    """Search the web for current information."""
    resp = requests.post("https://api.scavio.dev/api/v1/search",
        headers={"x-api-key": os.environ["SCAVIO_API_KEY"]},
        json={"platform": "google", "query": query})
    results = resp.json().get("organic_results", [])[:5]
    return "\n".join(f"- {r['title']}: {r.get('snippet', '')}" for r in results)

print(web_search.invoke("best CRM 2026"))

JavaScript Example

JavaScript
const H = {"x-api-key": process.env.SCAVIO_API_KEY, "Content-Type": "application/json"};
async function webSearch(query) {
  const r = await fetch("https://api.scavio.dev/api/v1/search", {
    method: "POST", headers: H,
    body: JSON.stringify({platform: "google", query})
  });
  const results = (await r.json()).organic_results || [];
  return results.slice(0,5).map(r => r.title + ": " + (r.snippet||"")).join("\n");
}
console.log(await webSearch("best CRM 2026"));

Expected Output

JSON
A LangGraph agent that can call a web search tool during execution, receiving structured search results and using them to answer questions with current data.

Related Tutorials

  • How to Add Real-Time Search to LangChain with langchain-scavio

Frequently Asked Questions

Most developers complete this tutorial in 15 to 30 minutes. You will need a Scavio API key (free tier works) and a working Python or JavaScript environment.

Python 3.10+ installed. langgraph and langchain-core packages installed. A Scavio API key from scavio.dev. Basic understanding of LangGraph state graphs. A Scavio API key gives you 50 free credits on signup.

Yes. The free tier includes 50 credits on signup, which is more than enough to complete this tutorial and prototype a working solution.

Scavio has a native LangChain package (langchain-scavio), an MCP server, and a plain REST API that works with any HTTP client. This tutorial uses LangChain, but you can adapt to your framework of choice.

Related Resources

Best Of

Best Search API for LangGraph Agents in 2026

Read more
Use Case

Pi Coding Agent Web Search Integration

Read more
Best Of

Best AI Agent Web Search Tools in 2026

Read more
Solution

Add Live Search Nodes to LangGraph Agent Workflows

Read more
Solution

Add Unified Search to Multi-Agent Systems with Scavio

Read more
Use Case

OpenWebUI Search Integration with Search API

Read more

Start Building

Integrate a web search API as a LangGraph tool node. Give your agent live search results with structured JSON instead of raw HTML scraping.

Get Free API KeyRead the Docs
ScavioScavio

Real-time search API for AI agents. Search every platform, not just Google.

Product

  • Features
  • Pricing
  • Dashboard
  • Affiliates

Developers

  • Documentation
  • API Reference
  • Quickstart
  • MCP Integration
  • Python SDK

Alternatives

  • Tavily Alternative
  • SerpAPI Alternative
  • Firecrawl Alternative
  • Exa Alternative

Tools

  • JSON Formatter
  • cURL to Code
  • Token Counter
  • All Tools

© 2026 Scavio. All rights reserved.

Featured on TAAFT
Terms of ServicePrivacy Policy