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

> ## Agent Instructions
> Every page is listed in https://docs.keenable.ai/llms.txt. To pick a product for a job, read https://docs.keenable.ai/choose.md
> Setup steps for agents, covering the SDKs, the MCP server and the CLI: https://docs.keenable.ai/skill.md
> Keenable search and fetch work without an API key. To add them to code, use the official SDK for the project's language: https://docs.keenable.ai/sdks.md
> Without a key, call POST https://api.keenable.ai/v1/search/public or GET https://api.keenable.ai/v1/fetch/public with an X-Keenable-Title header that names your app.
> With a key, send it in the X-API-Key header and read it from the KEENABLE_API_KEY environment variable. Never ask a user to paste a key into chat. Every access method: https://docs.keenable.ai/auth.md
> To get an API key without the console, use device login: https://docs.keenable.ai/authentication.md#device-login
> MCP server: https://api.keenable.ai/mcp (no key, or X-API-Key; keyless Codex uses https://api.keenable.ai/mcp-public), or https://api.keenable.ai/mcp-oauth for OAuth sign-in. These docs are an MCP server at https://docs.keenable.ai/mcp and the OpenAPI spec is at https://docs.keenable.ai/api-reference/openapi.json
> Keenable searches and fetches public web pages. If a question needs no web information, skip the call.
> On 402 with an API key (code insufficient_credits), stop and ask a human to add credits; don't retry. On 429, wait the number of seconds in Retry-After, then retry. Before you ship, follow https://docs.keenable.ai/production.md
> Every error, with its cause and fix, is in https://docs.keenable.ai/errors.md and moving from another search API is covered in https://docs.keenable.ai/migrate.md
> Before you compare Keenable with another search API, read https://docs.keenable.ai/guides/evaluate-search-apis.md

# Web search tools for Mistral models

> Give Mistral models web search and page fetch as function tools: pass the Keenable SDK's TOOLS to chat.complete and run each call, in Python and TypeScript.

A Mistral model decides when to search the web or read a page, and your code runs each call with the Keenable SDK. Mistral's chat API takes tools in the same format the SDK exports, so you pass `TOOLS` as is.

## How the loop works

1. Your code sends the conversation and `TOOLS` to `client.chat.complete`.
2. If the model wants the web, the assistant message holds `tool_calls` (`toolCalls` in TypeScript), each with an `id` and a `function` with `name` and `arguments`.
3. Your code appends the assistant message, runs each call with `run_tool_call`, and appends one `role: "tool"` message per call with the tool `name`, the call `id` and the result.
4. When the assistant message has no tool calls, its `content` is the answer.

## Prerequisites

* A Mistral API key in `MISTRAL_API_KEY`.
* Python 3 or Node.js with `tsx` to run the program.
* No Keenable key. The SDK searches without one. To raise the rate limit, set `KEENABLE_API_KEY` to an [API key](https://docs.keenable.ai/api-keys.md).

## Tool definition (copy verbatim)

Mistral takes each tool as `type: "function"` with a nested `function` object holding `name`, `description` and `parameters`. That is the format of the Keenable SDK's `TOOLS`:

```json Chat Completions format theme={"theme":{"light":"github-light-high-contrast","dark":"dark-plus"}}
[
  {
    "type": "function",
    "function": {
      "name": "keenable_search",
      "description": "Search the web for current information. Returns ranked pages with their title, URL and extracted page text. Use this whenever the answer depends on recent events or facts you are unsure about.",
      "parameters": {
        "type": "object",
        "properties": {
          "query": {
            "type": "string",
            "description": "What to look for, described in natural language rather than keywords."
          },
          "site": {
            "type": "string",
            "description": "Optional. Restrict results to one domain, e.g. 'arxiv.org'."
          },
          "exclude_domains": {
            "type": "array",
            "items": {
              "type": "string"
            },
            "description": "Optional. Never return results from these hosts or their subdomains, e.g. 'pinterest.com'. Hosts only, no paths or wildcards. Up to 1,000."
          },
          "published_after": {
            "type": "string",
            "description": "Optional. Only pages published on or after this date (YYYY-MM-DD)."
          },
          "objective": {
            "type": "string",
            "description": "Optional. Two or three natural sentences, with no field labels and no line breaks, that cover: the exact facts, entities, numbers, dates or names a useful page must contain; task context the query does not state (who, where, when, eligibility, date window); which kinds of sources count or must be excluded; what is already established from earlier steps; and what is still unknown. Every sentence must be self-contained: name the entities, places, dates and figures explicitly, and never refer to 'the task', 'the clue', 'the question', 'above' or earlier results. Do not paste the conversation, and do not include personal, confidential or sensitive information. At most 1,000 characters."
          },
          "disable_cards": {
            "type": "boolean",
            "description": "Optional. true returns ordinary web pages in place of Keenable's cards (results Keenable writes for prices, exchange rates or company facts, whose URLs expire after one day). Default false."
          }
        },
        "required": [
          "query"
        ]
      }
    }
  },
  {
    "type": "function",
    "function": {
      "name": "keenable_fetch",
      "description": "Fetch one web page and return its main content as markdown. Use after keenable_search when a search snippet is not enough.",
      "parameters": {
        "type": "object",
        "properties": {
          "url": {
            "type": "string",
            "description": "The URL of the page to read."
          }
        },
        "required": [
          "url"
        ]
      }
    }
  }
]
```

The loop below passes the SDK's `TOOLS` directly, so the definitions always match the SDK version you installed. If you paste the JSON instead, check the SDK column of the [arguments table](https://docs.keenable.ai/tool-calling.md#tool-arguments): an older `run_tool_call` drops any argument its version doesn't offer.

## Run the loop

Install the Keenable SDK and Mistral's SDK:

```bash TypeScript theme={"theme":{"light":"github-light-high-contrast","dark":"dark-plus"}}
npm install keenable @mistralai/mistralai
```

```bash Python theme={"theme":{"light":"github-light-high-contrast","dark":"dark-plus"}}
pip install keenable mistralai
```

Save this program as `mistral-tools.mts` or `mistral_tools.py`. It asks a question, runs every tool call the model makes, and prints the answer:

```typescript TypeScript theme={"theme":{"light":"github-light-high-contrast","dark":"dark-plus"}}
import { Mistral } from "@mistralai/mistralai";
import type { ChatCompletionRequestMessage } from "@mistralai/mistralai/models/components";
import { Keenable, KeenableError, TOOLS, runToolCall } from "keenable";

const keenable = new Keenable();
const client = new Mistral({ apiKey: process.env.MISTRAL_API_KEY });

const messages: ChatCompletionRequestMessage[] = [
  { role: "user", content: "What changed in the latest Node.js LTS release? Cite your sources." },
];

while (true) {
  const response = await client.chat.complete({ model: "mistral-large-latest", messages, tools: TOOLS });
  const message = response.choices[0]?.message;
  if (!message?.toolCalls?.length) {
    console.log(message?.content);
    break;
  }

  messages.push({ ...message, role: "assistant" });
  for (const call of message.toolCalls) {
    let content: string;
    try {
      content = await runToolCall(keenable, call.function.name, call.function.arguments);
    } catch (error) {
      content = `Tool call failed: ${error instanceof KeenableError ? error.message : error}`;
    }
    messages.push({ role: "tool", name: call.function.name, toolCallId: call.id, content });
  }
}
```

```python Python theme={"theme":{"light":"github-light-high-contrast","dark":"dark-plus"}}
import os

from keenable import TOOLS, Keenable, KeenableError, run_tool_call
from mistralai.client import Mistral

keenable = Keenable()
client = Mistral(api_key=os.environ["MISTRAL_API_KEY"])

messages = [
    {"role": "user", "content": "What changed in the latest Node.js LTS release? Cite your sources."}
]

while True:
    response = client.chat.complete(model="mistral-large-latest", messages=messages, tools=TOOLS)
    message = response.choices[0].message
    if not message.tool_calls:
        print(message.content)
        break

    messages.append(message)
    for call in message.tool_calls:
        try:
            content = run_tool_call(keenable, call.function.name, call.function.arguments)
        except KeenableError as exc:
            content = f"Tool call failed: {exc}"
        messages.append(
            {"role": "tool", "name": call.function.name, "tool_call_id": call.id, "content": content}
        )
```

Run it with `npx tsx mistral-tools.mts` or `python mistral_tools.py`. The program prints the model's final answer. The question asks for sources, so the answer names the pages the model read.

In Python, Mistral's SDK imports from `mistralai.client`. The TypeScript SDK uses camelCase fields: `toolCalls` and `toolCallId`. If a tool call fails, the loop sends the error message as the tool result so the model can retry. [Errors inside a tool call](https://docs.keenable.ai/tool-calling.md#errors-inside-a-tool-call) lists what `run_tool_call` raises.

## Differences from other providers

| Compared with | What changes on Mistral |
| - | - |
| [Anthropic](https://docs.keenable.ai/tool-calling/anthropic.md) | No mapping: `TOOLS` goes in unchanged. Each result is its own `tool` message, and there is no error flag, so the error goes in the message text. |
| [OpenAI](https://docs.keenable.ai/tool-calling/openai.md) | Mistral uses the Chat Completions message shape, not the Responses API's flat tools and `function_call_output` items. You keep the whole conversation on your side. |
| [Gemini](https://docs.keenable.ai/tool-calling/gemini.md) | Arguments can arrive as a JSON string or an object. `run_tool_call` takes either. |
| [Open models](https://docs.keenable.ai/tool-calling/open-models.md) | Same tool format. Mistral's `tool_choice` takes `auto` (the default), `any` or `none`, and `parallel_tool_calls: false` limits the model to one call per turn. |


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.