Skip to Content
Observability

Mistral AI (tracing)

The Mistral AI Python SDK  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 
  2. Create an API key - Generate one on the API keys page 

Paste this into your coding agent:

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

Setup

Install packages

Terminal
pip install respan-ai respan-instrumentation-mistralai mistralai python-dotenv

Set environment variables

Terminal
export RESPAN_API_KEY="YOUR_RESPAN_API_KEY" export MISTRAL_API_KEY="YOUR_MISTRAL_API_KEY"

Optional:

Terminal
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  and search for the workflow name mistralai_chat_completion.

Configuration

ParameterTypeDefaultDescription
**instrumentor_kwargsdict{}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."}], )
AttributeTypeDescription
customer_identifierstrIdentifies the end user in Respan analytics.
session_identifierstrGroups related messages into a conversation.
trace_group_identifierstrGroups spans by workflow name.
metadatadictCustom 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())