docs/src/content/en/guides/guide/web-search.mdx
import Steps from "@site/src/components/Steps"; import StepItem from "@site/src/components/StepItem"; import Tabs from "@theme/Tabs"; import TabItem from "@theme/TabItem";
When building a web search agent, you have two main strategies to consider:
v22.13.0 or later installedSome LLM providers include built-in web search capabilities that can be used directly without additional API integrations. OpenAI's and Google's models both offer native search tools that the model can invoke during generation.
<Steps> <StepItem title="Install dependencies"> Install dependencies<Tabs>
<TabItem value="openai" label="Open AI">
```bash npm2yarn
npm install @ai-sdk/openai
```
</TabItem>
<TabItem value="gemini" label="Gemini">
```bash npm2yarn
npm install @ai-sdk/google
```
</TabItem>
</Tabs>
<Tabs>
<TabItem value="openai" label="Open AI">
```ts title="src/mastra/agents/searchAgent.ts"
import { Agent } from '@mastra/core/agent'
export const searchAgent = new Agent({
id: 'search-agent',
name: 'Search Agent',
instructions: 'You are a search agent that can search the web for information.',
model: 'openai/gpt-5.4',
})
```
</TabItem>
<TabItem value="gemini" label="Gemini">
```ts title="src/mastra/agents/searchAgent.ts"
import { Agent } from '@mastra/core/agent'
export const searchAgent = new Agent({
id: 'search-agent',
name: 'Search Agent',
instructions: 'You are a search agent that can search the web for information.',
model: 'google/gemini-2.5-flash',
})
```
</TabItem>
</Tabs>
<Tabs>
<TabItem value="openai" label="Open AI">
```ts title="src/mastra/agents/searchAgent.ts" {1,9-12}
import { openai } from '@ai-sdk/openai'
import { Agent } from '@mastra/core/agent'
export const searchAgent = new Agent({
id: 'search-agent',
name: 'Search Agent',
instructions: 'You are a search agent that can search the web for information.',
model: 'openai/gpt-5.4',
tools: {
webSearch: openai.tools.webSearch(),
},
})
```
</TabItem>
<TabItem value="gemini" label="Gemini">
```ts title="src/mastra/agents/searchAgent.ts" {1,9-14}
import { google } from '@ai-sdk/google'
import { Agent } from '@mastra/core/agent'
export const searchAgent = new Agent({
id: 'search-agent',
name: 'Search Agent',
instructions: 'You are a search agent that can search the web for information.',
model: 'google/gemini-2.5-flash',
tools: {
webSearch: google.tools.googleSearch({
mode: 'MODE_DYNAMIC',
}),
},
})
```
</TabItem>
</Tabs>
```ts title="src/mastra/index.ts" {2, 5}
import { Mastra } from '@mastra/core'
import { searchAgent } from './agents/searchAgent'
export const mastra = new Mastra({
agents: { searchAgent },
})
```
```bash
mastra dev
```
Inside Studio navigate to the **"Search Agent"** and ask it: "What happened last week in AI news?"
For more control over search behavior, you can integrate external search APIs as custom tools. Exa is a search engine built specifically for AI applications, offering semantic search, configurable filters (category, domain, date range), and the ability to retrieve full page contents. The search API is wrapped in a Mastra tool that defines the input schema, output format, and execution logic.
<Steps> <StepItem title="Install dependencies"> Install dependencies```bash npm2yarn
npm install exa-js
```
```ts title="src/mastra/agents/searchAgent.ts"
import { Agent } from '@mastra/core/agent'
export const searchAgent = new Agent({
id: 'search-agent',
name: 'Search Agent',
instructions: 'You are a search agent that can search the web for information.',
model: 'openai/gpt-5.4',
})
```
```ts title="src/mastra/tools/searchTool.ts"
import { createTool } from '@mastra/core/tools'
import z from 'zod'
import Exa from 'exa-js'
export const exa = new Exa(process.env.EXA_API_KEY)
export const webSearch = createTool({
id: 'exa-web-search',
description: 'Search the web',
inputSchema: z.object({
query: z.string().min(1).max(50).describe('The search query'),
}),
outputSchema: z.array(
z.object({
title: z.string().nullable(),
url: z.string(),
content: z.string(),
publishedDate: z.string().optional(),
}),
),
execute: async inputData => {
const { results } = await exa.searchAndContents(inputData.query, {
livecrawl: 'always',
numResults: 2,
})
return results.map(result => ({
title: result.title,
url: result.url,
content: result.text.slice(0, 500),
publishedDate: result.publishedDate,
}))
},
})
```
```ts title="src/mastra/agents/searchAgent.ts"
import { webSearch } from './tools/searchTool'
export const searchAgent = new Agent({
id: 'search-agent',
name: 'Search Agent',
instructions: 'You are a search agent that can search the web for information.',
model: 'openai/gpt-5.4',
tools: {
webSearch,
},
})
```
```ts title="src/mastra/index.ts" {2, 5}
import { Mastra } from '@mastra/core'
import { searchAgent } from './agents/searchAgent'
export const mastra = new Mastra({
agents: { searchAgent },
})
```
```bash
mastra dev
```
Inside Studio navigate to the **"Search Agent"** and ask it: "What happened last week in AI news?"