---
title: "Mistral AI (tracing)"
description: "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.

<Accordion title="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)
</Accordion>

Paste this into your coding agent:

```text
Help me set up Respan tracing with the Mistral AI integration.
Follow https://www.respan.ai/docs/integrations/mistral-tracing.md
```

## Setup

<Steps>
<Step title="Install packages">

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

</Step>

<Step title="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"
```

</Step>

<Step title="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()
```

</Step>

<Step title="View your trace">

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

</Step>
</Steps>

## 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",
    session_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. |
| `session_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())
```
