TOOLS as is.
How the loop works
- Your code sends the conversation and
TOOLStoclient.chat.complete. - If the model wants the web, the assistant message holds
tool_calls(toolCallsin TypeScript), each with anidand afunctionwithnameandarguments. - Your code appends the assistant message, runs each call with
run_tool_call, and appends onerole: "tool"message per call with the toolname, the callidand the result. - When the assistant message has no tool calls, its
contentis the answer.
Prerequisites
- A Mistral API key in
MISTRAL_API_KEY. - Python 3 or Node.js with
tsxto run the program. - No Keenable key. The SDK searches without one. To raise the rate limit, set
KEENABLE_API_KEYto an API key.
Tool definition (copy verbatim)
Mistral takes each tool astype: "function" with a nested function object holding name, description and parameters. That is the format of the Keenable SDK’s TOOLS:
Chat Completions format
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: an older run_tool_call drops any argument its version doesn’t offer.
Run the loop
Install the Keenable SDK and Mistral’s SDK:mistral-tools.mts or mistral_tools.py. It asks a question, runs every tool call the model makes, and prints the answer:
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 lists what run_tool_call raises.