A hands-on tutorial walks through building a lightweight AI agent in JavaScript that can decide when to search the web and use those results to answer questions. It uses OpenRouter's free Nemotron 3.5 Lightning model for tool-calling decisions and SearchApi for retrieving Google search results, implementing the classic agent loop pattern: LLM decides to use a tool, the app executes it, results feed back into the model until a final answer emerges. The tutorial covers setup, tool schema definition, the search_web() implementation, and the agent loop code, plus ideas for extending it with additional search capabilities like shopping, maps, news, and YouTube.

11m read timeFrom amitmerchant.com
Post cover image
Table of contents
What Makes Something an AI Agent?What We’re Going to BuildPrerequisitesConnecting to the LLMGive the AI a Search ToolImplement search_web() with SearchApiThe Agent LoopRun the AgentWhy This Is an AgentTaking the Agent FurtherIn Closing

Questions this post answers

How do I build an AI agent in JavaScript that can search the web for real-time information?

Use OpenRouter's tool-calling API with a free model like Nemotron 3.5 Lightning, defining a search_web function schema the model can call. When the model requests the tool, your JavaScript code executes a fetch to SearchApi's Google search endpoint, extracts the top organic results, and feeds them back into the conversation as a tool message so the model can generate a grounded final answer. daily.dev surfaces practical patterns like this for developers building their own tool-using AI agents.

What is an agent loop in the context of LLM tool calling?

An agent loop is the repeated cycle where an LLM receives a prompt, decides whether it needs a tool, generates a structured tool call, waits for the application to execute that tool and return results, then decides whether to call another tool or produce a final answer. The loop continues until the model has enough information, distinguishing an agent from a single LLM call with no ability to act on external data. Developers comparing agent architectures can track patterns like the agent loop on daily.dev.

Why use SearchApi instead of building custom web scraping for an AI agent's search tool?

SearchApi provides structured Google search results through a simple API call, letting an agent ground its answers in current web data without building or maintaining scraping infrastructure. A free tier allows up to 100 requests, and the response can be trimmed to just title, link, and snippet fields before being sent back to the LLM to keep token usage low. daily.dev helps developers weighing search API options for their own AI agent projects.

4.3K Impressions