Skip to navigation

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.

  1. Sign up - Create an account at platform.respan.ai
  2. Create an API key - Generate one on the API keys page

See Mistral AI gateway setup to route Mistral model calls through the Respan gateway.

Setup

1

Install packages

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

Set environment variables

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

Optional:

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

Initialize and run

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()
4

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.

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."}],
)
AttributeTypeDescription
customer_identifierstrIdentifies the end user in Respan analytics.
thread_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

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

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())