> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://respan.ai/docs/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://respan.ai/docs/_mcp/server.

# Mistral AI (tracing)

> Trace Mistral AI Python SDK chat, multi-turn, and async calls with Respan.

The [Mistral AI Python SDK](https://github.com/mistralai/client-python) is the official client for Mistral models. `respan-instrumentation-mistralai` activates the Mistral AI OpenInference wrapper and normalizes emitted spans to the Respan tracing pipeline.

#### Set up Respan

1. **Sign up** - Create an account at [platform.respan.ai](https://platform.respan.ai)
2. **Create an API key** - Generate one on the [API keys page](https://platform.respan.ai/platform/gateway/api-keys)

#### Use Respan Gateway

See [Mistral AI gateway setup](/docs/integrations/gateway/mistral-ai) to route Mistral model calls through the Respan gateway.

#### Example projects

* [Respan example projects](https://github.com/respanai/respan-example-projects)

## Setup

#### Install packages

```bash
pip install respan-ai respan-instrumentation-mistralai mistralai python-dotenv
```

#### Set environment variables

```bash
export RESPAN_API_KEY="YOUR_RESPAN_API_KEY"
export MISTRAL_API_KEY="YOUR_MISTRAL_API_KEY"
```

Optional:

```bash
export RESPAN_BASE_URL="https://api.respan.ai/api"
export MISTRALAI_MODEL="mistral/mistral-small"
```

#### Initialize and run

```python
import os

from dotenv import load_dotenv
from mistralai.client import Mistral
from respan import Respan, workflow
from respan_instrumentation_mistralai import MistralAIInstrumentor

load_dotenv()

respan = Respan(
    api_key=os.environ["RESPAN_API_KEY"],
    base_url=os.getenv("RESPAN_BASE_URL", "https://api.respan.ai/api"),
    instrumentations=[MistralAIInstrumentor()],
)


@workflow(name="mistralai_chat_completion")
def run_chat() -> str:
    with Mistral(api_key=os.environ["MISTRAL_API_KEY"]) as client:
        response = client.chat.complete(
            model=os.getenv("MISTRALAI_MODEL", "mistral/mistral-small"),
            messages=[
                {
                    "role": "user",
                    "content": "Reply with one concise sentence about tracing.",
                }
            ],
        )
        return str(response.choices[0].message.content or "")


try:
    print(run_chat())
finally:
    respan.shutdown()
```

#### View your trace

Open the [Traces page](https://platform.respan.ai/platform/traces) and search for the workflow name `mistralai_chat_completion`.

## Configuration

| Parameter               | Type   | Default | Description                                                                              |
| ----------------------- | ------ | ------- | ---------------------------------------------------------------------------------------- |
| `**instrumentor_kwargs` | `dict` | `{}`    | Extra keyword arguments forwarded to the upstream Mistral AI OpenInference instrumentor. |

## Attributes

Attach customer identifiers, thread IDs, workflow names, and metadata to Mistral AI calls with `propagate_attributes`.

```python
from respan import propagate_attributes

with propagate_attributes(
    customer_identifier="user_123",
    thread_identifier="conversation_456",
    trace_group_identifier="mistralai_support_chat.workflow",
    metadata={"plan": "pro", "workflow_name": "mistralai_support_chat.workflow"},
):
    response = client.chat.complete(
        model="mistral/mistral-small",
        messages=[{"role": "user", "content": "Summarize our support policy."}],
    )
```

| Attribute                | Type   | Description                                          |
| ------------------------ | ------ | ---------------------------------------------------- |
| `customer_identifier`    | `str`  | Identifies the end user in Respan analytics.         |
| `thread_identifier`      | `str`  | Groups related messages into a conversation.         |
| `trace_group_identifier` | `str`  | Groups spans by workflow name.                       |
| `metadata`               | `dict` | Custom key-value pairs merged with default metadata. |

## Examples

### Multi-turn chat

```python
response = client.chat.complete(
    model="mistral/mistral-small",
    messages=[
        {"role": "user", "content": "Explain tracing in one sentence."},
        {"role": "assistant", "content": "Tracing records how a request moves through a system."},
        {"role": "user", "content": "Now make that explanation shorter."},
    ],
)
print(response.choices[0].message.content)
```

### Async chat

```python
import asyncio
import os

from mistralai.client import Mistral


async def main() -> None:
    async with Mistral(api_key=os.environ["MISTRAL_API_KEY"]) as client:
        response = await client.chat.complete_async(
            model="mistral/mistral-small",
            messages=[{"role": "user", "content": "Write a short haiku about traces."}],
        )
        print(response.choices[0].message.content)


asyncio.run(main())
```