Quickstart: send your first trace

This quickstart instruments a Python script with the Mistral SDK, sends one traced request, and shows you the trace in the Studio Trace Explorer. A trace is a tree of spans: each span is one step of the request, with its input, output, latency, and token counts.

You need Python 3.10 or later, uv, and a Mistral API key.

Install the SDK

Install the SDK

Create a project and add the Mistral SDK with its telemetry extra:

uv init first-trace && cd first-trace
uv add "mistralai[telemetry]"
export MISTRAL_API_KEY="your-api-key"

Send a traced request

Send a traced request

Replace the content of main.py with this script. configure_telemetry turns on tracing for the client. The script then wraps one chat request and one simulated tool call in a parent span, so the trace has several steps:

import os

from mistralai.client import Mistral
from mistralai.extra.observability import configure_telemetry, get_telemetry_tracer

client = Mistral(api_key=os.environ["MISTRAL_API_KEY"])
configure_telemetry(client)
tracer = get_telemetry_tracer(client, "first-trace")

with tracer.start_as_current_span("invoke_agent"):
    response = client.chat.complete(
        model="mistral-small-latest",
        messages=[{"role": "user", "content": "Explain distributed tracing in one sentence."}],
    )
    print(response.choices[0].message.content)

    with tracer.start_as_current_span("execute_tool web_search") as tool_span:
        tool_span.set_attribute("gen_ai.operation.name", "execute_tool")
        tool_span.set_attribute("gen_ai.tool.name", "web_search")
        tool_span.set_attribute("gen_ai.tool.call.arguments", "distributed tracing")

Run it:

uv run main.py

The script prints the model's answer. The SDK traces the chat request automatically. Tool calls aren't traced automatically, which is why the script adds the execute_tool span itself.

You should see

You should see

Open the Trace Explorer in Studio. The trace appears within a few seconds. Click it to open the span tree:

  • invoke_agent, the parent span you created.
  • Under it, the chat request, with the prompt, the answer, the model, the latency, and the input and output token counts.
  • The execute_tool web_search span, with the tool name and arguments you set.

Spans are redacted on the client before export: by default, the SDK masks recognized secrets and personal data, such as API keys and email addresses, and keeps the rest of the content. See Data redaction.

If no trace appears, check that:

  1. You called configure_telemetry(client) before the first request.
  2. Your organization is on the Enterprise tier, and an Org Admin hasn't disabled observability.
  3. Your workspace role is Org Admin, Workspace Admin, or Observability Viewer.

Traces are kept for 30 days.

Going further

Going further