> ## Documentation Index
> Fetch the complete documentation index at: https://www.meilisearch.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# AI SDK

> Give AI agents a Meilisearch search tool using the Vercel AI SDK and @meilisearch/ai-sdk.

[`@meilisearch/ai-sdk`](https://github.com/meilisearch/ai-sdk) adds Meilisearch search tools to the [Vercel AI SDK](https://ai-sdk.dev), letting an LLM decide when to search your data and how to use the results.

## Requirements

* A Meilisearch project with at least one index containing documents
* A [search API key](/docs/capabilities/security/how_to/manage_api_keys) for that project
* An API key from an LLM provider (this example uses OpenAI, but any provider supported by the AI SDK works)

## Install

```bash theme={null}
npm install ai @ai-sdk/openai @meilisearch/ai-sdk
```

Add your credentials to your environment:

```bash .env theme={null}
MEILISEARCH_URL=https://your-project.meilisearch.io
MEILISEARCH_KEY=your-search-api-key
OPENAI_API_KEY=your-openai-api-key
```

## Give an agent a search tool

```ts theme={null}
import { generateText, stepCountIs } from "ai";
import { openai } from "@ai-sdk/openai";
import { meilisearchSearch } from "@meilisearch/ai-sdk";

const { text } = await generateText({
  model: openai("gpt-5.4-mini"),
  prompt: "Find two movies about time travel, then summarize the difference.",
  tools: {
    search: meilisearchSearch({
      host: "MEILISEARCH_URL",
      apiKey: "MEILISEARCH_KEY",
      indexUid: "movies",
      description: "Search the movies database",
    }),
  },
  stopWhen: stepCountIs(5),
});

console.log(text);
```

The model decides whether and how many times to call `search` before answering. The `description` field tells it what the index contains and when to use it.

<Card title="AI SDK demo" icon="github" href="https://github.com/meilisearch/ai-sdk-demo">
  Next.js chatbox example application
</Card>

## Going further

<CardGroup cols={2}>
  <Card title="Getting started with agentic search" href="/docs/capabilities/agentic_search/getting_started">
    Build a multi-turn chatbot with streaming responses.
  </Card>

  <Card title="Display source documents" href="/docs/capabilities/agentic_search/how_to/display_source_documents">
    Show users which documents an agent used to answer.
  </Card>

  <Card title="Configure guardrails" href="/docs/capabilities/agentic_search/how_to/configure_guardrails">
    Keep agents on topic and grounded in your data.
  </Card>

  <Card title="Handle errors and fallbacks" href="/docs/capabilities/agentic_search/how_to/handle_errors_and_fallbacks">
    Make your agent resilient to search and LLM failures.
  </Card>
</CardGroup>

For the full list of available tools, see the [SDK repository](https://github.com/meilisearch/ai-sdk). If you need natural-language admin access instead of an app-embedded agent, see the [MCP integration](/docs/getting_started/integrations/mcp).
