Integrations / Mistral

Trace Mistral API calls

To trace Mistral with Tracify, record each chat call as a step and copy the token counts from the response. The mistralai library is not wrapped automatically, so this is a few lines you add once.

Your code

Mistral

Tracify

One run.

  • 01Mistral model name
  • 02Prompt and completion tokens
  • 03Latency and API errors
  • 04Calls grouped into the agent run

Setup

5 steps.

  1. 01

    Install

    Mistral's official library plus the Tracify SDK.

    bash
    pip install tracify-sdk mistralai
  2. 02

    Set your project key

    Create a project in Tracify, copy its API key and put it in the environment where your code runs. The SDK reads it from there, so nothing is hard-coded.

    bash
    export TRACIFY_API_KEY="tracify_sk_live_..."
  3. 03

    Record the call as a step

    Mistral's chat.complete is not the same shape as OpenAI's, so there is no one-line wrapper. Open a step around the call, then copy the model and tokens from the response. Errors are recorded and still reach your code.

    python
    import os
    import tracify
    from mistralai.client import Mistral
    
    mistral = Mistral(api_key=os.environ["MISTRAL_API_KEY"])
    
    def ask(prompt: str) -> str:
        messages = [{"role": "user", "content": prompt}]
        with tracify.span("mistral-chat", type="llm", input=messages) as step:
            response = mistral.chat.complete(model="mistral-large-latest", messages=messages)
            text = response.choices[0].message.content
            step.set(
                model="mistral-large-latest",
                input_tokens=response.usage.prompt_tokens,
                output_tokens=response.usage.completion_tokens,
            )
            step.set_output(text)
            return text
  4. 04

    Put it inside an agent run

    Wrap the function that drives your agent with trace_agent. All Mistral steps inside it join one trace.

    python
    @tracify.trace_agent(name="mistral-agent")
    def run(question: str) -> str:
        draft = ask(question)
        return ask("Improve this answer: " + draft)
  5. 05

    Run it once and look

    Run your code one time, then open the Tracing page of your project. Your Mistral call shows up as a run with its steps in order, plus the tokens, time and any error for each call.

Good to know

Manual, on purpose

The SDK wraps the OpenAI and Anthropic client shapes. Mistral's library has its own, so you record the call yourself. It is the same data, a few lines longer.

Good to know

About cost

Tracify records the tokens the provider returns. A dollar cost is shown when you give the wrapper your own rates with calculate_cost (Python) or calculateCost (TypeScript), so the number always matches your real price plan.

Full details in the documentation. All integrations are listed on the integrations page.

Can I trace streaming with Mistral?
Yes. Keep the step open while you read the stream, then set the tokens from the final chunk before the step ends.
Does it work with Mistral through Azure or Bedrock?
Yes. Record the call as a step the same way. Only the client you call changes.
Is the TypeScript library supported?
Yes, with the span function: open it around the call, set the model and tokens from the response, and return the text.

Send your first trace.

Free to start.