How to use the Mistral AI API

Mistral AI offers high-performance European models including Mistral Large, Pixtral for vision, and Codestral for code. This guide covers the API, function calling, and choosing the right Mistral model for your task.

Mistral's model lineup

Mistral offers a tiered lineup. Mistral Large is their most capable model, competitive with GPTGPTGenerative Pre-trained Transformer — the model architecture and family name behind OpenAI's most famous models, from GPT-2 to GPT-5.Learn more →-4o on reasoning and coding. Mistral Small is a fast, cost-effective model for classification and summarisation tasks. Codestral is a code-specialist model trained on 80+ programming languages and is one of the best code completionCode CompletionAn AI feature that predicts and suggests the next tokens of code as a developer types, ranging from single-token suggestions to entire function implementations.Learn more → models available. Pixtral adds vision capabilities.

Mistral operates data centres in Europe, which makes it the preferred choice for EU companies with GDPR-sensitive data. Their API is also OpenAI-compatible, making migration from OpenAI a simple base URL change.

Get started

Create an account at console.mistral.ai. Generate an API key and store it as `MISTRAL_API_KEY`. Install the SDK: `pip install mistralai` (Python) or `npm install @mistralai/mistralai` (Node.js).

Make a call: `from mistralai import Mistral; client = Mistral(api_key=os.environ['MISTRAL_API_KEY']); response = client.chat.complete(model='mistral-small-latest', messages=[{'role': 'user', 'content': 'Hello!'}]); print(response.choices[0].message.content)`.

OpenAI compatibility

Mistral's API is fully OpenAI-compatible. Use the OpenAI SDK by changing the base URL: `client = OpenAI(base_url='https://api.mistral.ai/v1', api_key=os.environ['MISTRAL_API_KEY'])`. All existing OpenAI SDK code works without further changes — just change the model name to a Mistral model ID.

Mistral model IDs: `mistral-large-latest`, `mistral-small-latest`, `codestral-latest`, `pixtral-large-latest`. Pinned versions are available for production stability (e.g. `mistral-large-2407`).

Function calling and structured output

Mistral Large and Small both support function callingFunction CallingA capability that allows language models to return structured calls to developer-defined functions or tools, enabling AI systems to interact with external APIs, databases, and services.Learn more → with the same JSON Schema format as OpenAI. Mistral also supports the `response_format: {type: 'json_object'}` parameter for guaranteed JSON output, and structured outputStructured OutputConstraining a language model to produce output that conforms to a predefined format or schema, such as valid JSON, rather than free-form text.Learn more → via `response_format: {type: 'json_schema', json_schema: {...}}` for schema-constrained responses.

For JSON extraction tasks, Mistral Small is a cost-effective choice — it reliably produces valid JSON at a fraction of Mistral Large's cost. Use Large only when the task requires complex reasoning or nuanced language understanding.

Codestral for code completion

Codestral excels at fill-in-the-middle (FIM) completion — the paradigm used by IDE plugins where the model sees code before and after the cursor. Use the FIM endpoint: `client.fim.complete(model='codestral-latest', promptPromptThe input text sent to a language model — the question, instruction, or context that triggers a response.Learn more →='def fibonacci(n):\n ', suffix='\n return result')`. Codestral fills in the middle section.

For VS Code integration, use the Continue extension with Codestral as the tab autocomplete model. Configure it in `.continue/config.json` with `provider: 'mistral'` and `model: 'codestral-latest'`. This gives Copilot-quality completions for 80+ languages.