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

# Mistral

> Trace Mistral AI API calls in TypeScript and Python with wrapMistral() or patch_mistral().

<Note>
  Available for: **Python** and **TypeScript**.
</Note>

Wrap your `MistralClient` with `wrapMistral()` (TypeScript) to trace chat and completion calls — or patch the `mistralai` module with `patch_mistral()` (Python).

## Installation

<CodeGroup>
  ```bash TypeScript theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  npm install @zespan/sdk @mistralai/mistralai
  ```

  ```bash Python theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  pip install zespan mistralai
  ```
</CodeGroup>

## Setup

<CodeGroup>
  ```typescript TypeScript theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  import { Mistral } from "@mistralai/mistralai";
  import { zespan } from "@zespan/sdk";

  zespan.init({ apiKey: process.env.ZESPAN_API_KEY! });

  const mistral = zespan.wrapMistral(new Mistral({ apiKey: process.env.MISTRAL_API_KEY! }));
  ```

  ```python Python theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  import os
  import zespan

  zespan.init(api_key=os.environ["ZESPAN_API_KEY"])
  zespan.patch_mistral()

  from mistralai import Mistral  # import after patching
  client = Mistral(api_key=os.environ["MISTRAL_API_KEY"])
  ```
</CodeGroup>

In TypeScript, `wrapMistral()` returns a wrapped client instance. In Python, `patch_mistral()` patches `Mistral.__init__` so every `Mistral(...)` client constructed afterward has its `chat.complete()` and `chat.stream()` methods traced automatically.

## Example

<CodeGroup>
  ```typescript TypeScript theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  const response = await mistral.chat.complete({
    model: "mistral-large-latest",
    messages: [{ role: "user", content: "Explain embeddings briefly." }],
  });
  ```

  ```python Python theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  response = client.chat.complete(
      model="mistral-large-latest",
      messages=[{"role": "user", "content": "Explain embeddings briefly."}],
  )
  ```
</CodeGroup>

`client.chat.stream(...)` (Python) and streaming `chat.stream()` calls (TypeScript) are traced end-to-end in both languages, including time-to-first-token.

## What gets captured

| Field         | Details                                                                  |
| ------------- | ------------------------------------------------------------------------ |
| Model         | `mistral-large-latest`, `mistral-small-latest`, `codestral-latest`, etc. |
| Input tokens  | From `usage.prompt_tokens`                                               |
| Output tokens | From `usage.completion_tokens`                                           |
| Cost          | Calculated from token counts and Mistral pricing                         |
| Latency       | Total request duration                                                   |
| Finish reason | `stop`, `length`, `tool_calls`                                           |
| Tool calls    | Tool name and parsed arguments, both languages                           |
