How to Add Real-Time Web Search to Your LLM Using Tavily

by SkillAiNest

Large language models are smart. But they are not always well informed.

They can write code, summarize books, and explain complex topics, but they struggle with real-time realities.

Their knowledge ends at their training cutoff, which means they can’t tell you what happened last week or even last year.

That’s where web search comes in.

By connecting a model to the Search API Tavilyyou can give your LLM access to current, factual information from the Internet. This makes your AI assistant, chatbot, or pipeline more accurate and context-aware.

This guide will show you how to enable real-time web search in your LLM workflow using Twily and LangChain.

Please note that Twily is a paid tool (with a free tier) and is popular in the Lingchain community. I’m not affiliated with the product – it’s only used in a course on AI agents I’m taking and seemed like a useful example.

What we will cover:

Why Include Web Search in an LLM?

When you ask a model the question “What are the best AI frameworks in 2025?” It tries to predict the answer from its training data. If this data stops in 2023, it may contain a list of outdated tools.

By integrating web search, you give the model a way to see things before responding.

This process is called recovery-array generation (RAG). It combines two steps: retrieving relevant data and generating responses based on it.

Handles the recovery part from recovery. It searches the web for the most relevant content and returns it as a clean, structured summary that LLMs can easily use. The result is an AI that looks intelligent and remains accurate.

How does it work?

Tavily is a purpose-built web search API designed for AI applications.

Unlike traditional search engines that return links, returns short, relevant summaries with context. It focuses on providing comprehensive information that models can understand without complex analysis.

tavily API is simple and fast. You can use it directly from Python, Node.js, or through the Langchain integration.

It also supports advanced filtering, topic targeting, and maximum results control to help you optimize the quantity and quality of retrieved data.

To arrange tavily

First, sign up at Tavily.com and get an API key. Tavily is not a free tool but comes with 1000 free credits to play with us.

Then install the required packages:

pip install -qU langchain langchain-openai langchain-tavily

Once installed, export your API key so your requests can be authenticated.

export TAVILY_API_KEY="your_api_key"

You are now ready to easily integrate with the language model via LangChain.

Lingchen Makes it easy to associate multiple tools with your model. In this example, we’ll create an agent that simply uses it as its search background.

from langchain.agents import create_agent
from langchain_openai import ChatOpenAI
from langchain_tavily import TavilySearch


tavily_search = TavilySearch(max_results=5, topic="general")


agent = create_agent(
    model=ChatOpenAI(model="gpt-5"),
    tools=(tavily_search),
    system_prompt="You are a helpful research assistant. Use web search to find accurate, up-to-date information."
)

response = agent.invoke({
    "messages": ({"role": "user", "content": "What is the most popular sport in the world? Include only Wikipedia sources."})
})
print(response)

This example creates a conversational agent powered by OpenAI’s GPT model and the Tivoli search tool. The agent reads the user’s query, easily uses it to fetch relevant web data, and returns the most recent response.

system_prompt Provides clear instructions to the model to rely on web results for factual accuracy. You can customize it to limit or expand how dependent the agent search is.

How Search Works

  1. User submits a question. The agent receives the message and determines that it needs external information.

  2. Easily performs a search. It queries the web for relevant results, summarizing content in readable chunks with source links.

  3. LLM reads abstracts. The model uses these pieces as context and produces a final answer that includes real-world facts.

This model transforms your LLM from a static knowledge base to a dynamic assistant that stays current with live data.

Using tavily without a longchain

You can also use it directly with Python if you want more control over the flow.

from tavily import TavilyClient
from openai import OpenAI

tavily = TavilyClient(api_key="your_api_key")
client = OpenAI()

def answer_with_tavily(question):
    search_results = tavily.search(question)
    snippets = "\n".join((r("content") for r in search_results("results")))
    prompt = f"Use the following search results to answer the question:\n\n{snippets}\n\nQuestion: {question}"
    response = client.responses.create(model="gpt-4o-mini", input=prompt)
    return response.output_text
print(answer_with_tavily("What are the biggest AI startups of 2025?"))

This example sends search summaries directly to the LLM prompt. It’s simple, flexible, and works without a longchain.

Improving search quality

You can make Twili results more relevant by adjusting some parameters.

  • max_results: How many pieces to return? Lower values ​​make responses faster and more focused.

  • Title: Helps narrow down the type of content you want (like “technology”, “science”, or “finance”).

  • Filters: Used to limit results to certain domains or exclude unwanted ones.

For example:

tavily_search = TavilySearch(max_results=3, topic="technology")

This setup tells Tully to return only three tech-related results, ideal for focused queries.

Building a search-aware chatbot

Once you’re easily connected, you can create a chatbot that automatically uses search when needed.

For example, if a query contains words like “latest”, “today”, or “news”, the agent can easily trigger a search.

def smart_chatbot(question):
    if any(word in question.lower() for word in ("today", "latest", "recent", "news")):
        return answer_with_tavily(question)
    else:
        return client.responses.create(model="gpt-4o-mini", input=question).output_text

This makes your chatbot dynamic, using real-time data when necessary, but keeping simple responses fast.

Real world applications

Search-driven LLMs are used everywhere.

Research assistants use them to pull up recent papers, marketing teams use them to track trends, and analysts use them to gather competitive insights. Developers build knowledge agents that can search for documents or regulations automatically.

By combining Twiley’s structured search results with the reasoning power of LLM, you can create tools that are both accurate and conversational.

Why Tavily is a Good Fit

Traditional search APIs return unstructured HTML or raw snippets that models struggle to read.

Tavily is better for AI. It cleans, summarizes and filters before returning it. The output is concise, readable and safe to use directly in your notation or rag pipelines.

It also decreases deception Because the model has a realistic, foundational context to work with. This makes it ideal for production AI systems that require reliability as much as creativity.

Web search is not the only option. There are other options like Raga web browser, Axa, etc. Here is one Complete list with their pros and cons.

The result

Large language models are powerful, but they don’t live on the Internet. Without searching, they guess. With Twiley, they know.

By easily integrating into your LLM workflow, you bridge the gap between static intelligence and real-time knowledge. Whether you’re building a chatbot, a research tool, or an AI assistant, adding it gives your model access to the world’s most current information.

The combination of Langchain, OpenAI, and Twily turns any LLM into a connected, informed, and reliable AI researcher, who can finally answer questions about today, not tomorrow.

Hope you enjoyed this article. Sign up for my free newsletter turingtalks.ai For more tutorials on AI. You can too Visit my website.

You may also like

Leave a Comment

At Skillainest, we believe the future belongs to those who embrace AI, upgrade their skills, and stay ahead of the curve.

Get latest news

Subscribe my Newsletter for new blog posts, tips & new photos. Let's stay updated!

@2025 Skillainest.Designed and Developed by Pro