@anvia/mistral

Mistral completion and embedding models.

@anvia/mistral adapts the Mistral SDK into Anvia's provider-neutral interfaces. It supports completions (streaming and non-streaming) and embeddings.

Install

pnpm add @anvia/mistral

The Mistral SDK (@mistralai/mistralai) is a transitive dependency.

Quick Start

import { AgentBuilder } from "@anvia/core";
import { MistralClient } from "@anvia/mistral";

const client = new MistralClient({ apiKey });
const model = client.completionModel("mistral-large-latest");

const agent = new AgentBuilder("support", model)
  .instructions("Answer clearly.")
  .build();

const response = await agent.prompt("What is Anvia?").send();
console.log(response.output);

Client Configuration

type MistralClientOptions = {
  apiKey?: string;        // required unless `client` is provided
  serverURL?: string;     // custom endpoint
  client?: Mistral;       // pre-configured Mistral SDK instance
};

Provider clients only use explicit constructor values. They do not read environment variables.

// Standard
const client = new MistralClient({ apiKey });

// Custom endpoint
const client = new MistralClient({
  apiKey,
  serverURL: "https://my-proxy.com",
});

// Pre-configured SDK instance
const client = new MistralClient({ client: existingMistralInstance });

Completion Models

// Default: mistral-large-latest
const model = client.completionModel();

// Specific model
const model = client.completionModel("mistral-small-latest");

Supports streaming and non-streaming through the standard StreamingCompletionModel interface.

Streaming with Agents

const agent = new AgentBuilder("writer", model)
  .instructions("Write concise summaries.")
  .build();

for await (const event of agent.prompt("Summarize this topic.").stream()) {
  if (event.type === "text_delta") {
    process.stdout.write(event.text);
  }
}

Embedding Models

const embeddingModel = client.embeddingModel("mistral-embed");

const embeddings = await embeddingModel.embedTexts(["hello", "world"]);
OptionDefaultPurpose
maxBatchSize512Texts per API call

Tool Use

Mistral models support tool use through the standard Anvia tool system:

import { createTool } from "@anvia/core";
import { z } from "zod";

const search = createTool({
  name: "search_docs",
  description: "Search documentation.",
  input: z.object({ query: z.string() }),
  output: z.object({ results: z.array(z.string()) }),
  async execute({ query }) {
    return { results: [`Result for: ${query}`] };
  },
});

const agent = new AgentBuilder("support", model)
  .tool(search)
  .defaultMaxTurns(3)
  .build();

Model Listing

const models = await client.listModels();
console.log(models.data.map((m) => m.id));

Fetches available Mistral models and returns a normalized ModelList.

Error Handling

  • Constructor throws when neither client nor apiKey is supplied
  • listModels() throws ModelListingError when the provider request fails
  • Model operations throw Mistral SDK errors directly