# Monitor a Mistral Support Agent With Sentry

> Trace Mistral calls automatically in a TypeScript support assistant. Inspect tool requests, tokens, and streaming, then optionally trace your application’s tools.

**URL:** https://sentry.io/cookbook/monitor-mistral-with-sentry/

---

Trace Mistral’s model calls in a working support assistant. Inspect prompts, tool requests, tokens, and streamed replies, then add application spans when you need them.

**Time:** 15–20 minutes | **Difficulty:** Intermediate

## What You'll Learn

- Built a support assistant with order lookups and follow-up questions

- Traced Mistral calls automatically with the native integration

- Inspected tool requests, model latency, tokens, and streamed replies

- Optionally added agent and tool spans to diagnose an application failure

**Topics:** Agent Tracing, Error Monitoring

**SDKs:** Node.js

## Before You Start

**Accounts**

- A [Sentry project](https://sentry.io/signup/) and its DSN

- A [Mistral API key](https://console.mistral.ai/) with access to a model that supports tool calling

**Example Project**

- Clone [Sentry agent tracing examples](https://github.com/getsentry/sentry-agent-tracing-examples).

- The example includes Sentry JavaScript SDK v11, the Mistral SDK, `.env.example`, and `tsconfig.json`.

- Source: [Mistral support agent](https://github.com/getsentry/sentry-agent-tracing-examples/tree/main/mistral-support)

**Local Setup**

- Node.js 24.12 or later and pnpm

- Open `mistral-support/` in the cloned repository and run `pnpm install`.

- Run `cp .env.example .env`, then set `SENTRY_DSN` and `MISTRAL_API_KEY` in `.env`.

## Steps

### 1. Configure Sentry

Open `instrument.ts` in the example project. Sentry’s native Mistral integration is enabled by default when tracing is on. It traces `chat.complete` and `chat.stream` automatically. Record model inputs and outputs for the fictional data, and sample every call while you verify the setup. HTTP body and local-variable capture stay off; model content comes from the AI integration.

```typescript
import * as Sentry from "@sentry/node";

Sentry.init({
  dsn: process.env.SENTRY_DSN,
  tracesSampleRate: 1.0,
  dataCollection: {
    genAI: { inputs: true, outputs: true },
    httpBodies: [],
    stackFrameVariables: false,
  },
});
```

[Agent tracing in Node.js](https://docs.sentry.io/platforms/javascript/guides/node/agent-tracing/)

### 2. Review the Order-Lookup Tool

Open `orders.ts`. It defines the tool available to Mistral and the lookup that runs in your app. Two in-memory records stand in for an order service, with a simulated 250 ms delay. This is ordinary application code. The integration records the model’s tool request and the result passed back to Mistral; measuring the local function is an optional addition later.

```typescript
import { setTimeout } from "node:timers/promises";
import type { Tool, ToolCall } from "@mistralai/mistralai/models/components";

const orders = new Map([
  ["ORD-1001", { status: "shipped", item: "Kettle", delivery: "Thursday" }],
  ["ORD-1002", { status: "processing", item: "Mug", delivery: "Next week" }],
]);

export const orderTool = {
  type: "function",
  function: {
    name: "lookup_order",
    description: "Look up the current status of an order by its ID.",
    parameters: {
      type: "object",
      properties: { orderId: { type: "string", description: "For example, ORD-1001" } },
      required: ["orderId"],
      additionalProperties: false,
    },
  },
} satisfies Tool;

export async function lookupOrder(call: ToolCall, failLookup: boolean): Promise<string> {
  if (call.function.name !== "lookup_order") throw new Error("Unknown tool");
  const args: unknown = typeof call.function.arguments === "string"
    ? JSON.parse(call.function.arguments)
    : call.function.arguments;
  if (typeof args !== "object" || args === null ||
      !("orderId" in args) || typeof args.orderId !== "string") {
    throw new Error("Missing orderId");
  }

  await setTimeout(250);
  if (failLookup) throw new Error("Order service unavailable (demo)");
  return JSON.stringify(orders.get(args.orderId) ?? { status: "not_found" });
}
```

### 3. Follow the Agent Workflow

Open `assistant.ts`. Mistral chooses the order ID, your app runs the lookup, and a second model call streams the answer. The native integration traces both SDK calls automatically. This example requires one tool call per turn. Completed turns remain in history for follow-up questions; failed turns leave that history unchanged.

```typescript
import { Mistral } from "@mistralai/mistralai";
import type { ChatCompletionRequest } from "@mistralai/mistralai/models/components";
import { orderTool, lookupOrder } from "./orders.ts";

const mistral = new Mistral({ apiKey: process.env.MISTRAL_API_KEY });
const model = process.env.MISTRAL_MODEL || "mistral-small-latest";

export const history: ChatCompletionRequest["messages"] = [{
  role: "system",
  content: "You help with order status. Use lookup_order for each question. " +
    "Use only the returned order data. Keep answers to two short sentences.",
}];

export async function answer(question: string, failLookup = false): Promise<string> {
  const messages: ChatCompletionRequest["messages"] = [...history, { role: "user", content: question }];
  const response = await mistral.chat.complete({
    model, messages, tools: [orderTool],
    toolChoice: "any", parallelToolCalls: false,
  });
  const message = response.choices?.[0]?.message;
  const call = message?.toolCalls?.[0];
  if (!message || !call?.id || message.toolCalls?.length !== 1) {
    throw new Error("Expected one order lookup");
  }

  messages.push({ role: "assistant", content: message.content, toolCalls: message.toolCalls });
  const result = await lookupOrder(call, failLookup);
  messages.push({ role: "tool", toolCallId: call.id, content: result });

  const stream = await mistral.chat.stream({ model, messages });
  let reply = "";
  for await (const event of stream) {
    const content = event.data.choices?.[0]?.delta.content;
    const text = typeof content === "string" ? content : (content ?? [])
      .filter((part) => part.type === "text")
      .map((part) => part.text).join("");
    reply += text;
    process.stdout.write(text);
  }
  process.stdout.write("\n");
  history.push(...messages.slice(history.length), { role: "assistant", content: reply });
  return reply;
}
```

[Mistral tool calling](https://docs.mistral.ai/capabilities/function_calling/)

### 4. Start a Support Conversation

Run these commands from the example project. The start script loads `instrument.ts` before `app.ts` imports the Mistral SDK. The terminal stays open for follow-up questions. The conversation ID groups related model calls in Sentry, and flushing after each turn sends events while the app stays running. No manual spans are needed for this setup.

- Ask **Where is order ORD-1001?** The reply should use the shipped status and Thursday delivery from the tool result.

- Ask **What item is in that order?** The assistant should use the previous turn to identify ORD-1001, look it up, and answer Kettle.

- Leave the terminal open while you inspect the conversation. Type `/quit` when you finish.

**App**

```typescript
import { randomUUID } from "node:crypto";
import { createInterface } from "node:readline/promises";
import * as Sentry from "@sentry/node";
import { answer } from "./assistant.ts";

const terminal = createInterface({ input: process.stdin, output: process.stdout });
const conversationId = randomUUID();
Sentry.setConversationId(conversationId);
console.log(`Conversation: ${conversationId}`);
console.log("Ask about ORD-1001 or ORD-1002. Commands: /fail, /quit");
let failNextTurn = false;

try {
  while (true) {
    const question = (await terminal.question("You: ")).trim();
    if (question === "/quit") break;
    if (!question) continue;
    if (question === "/fail") {
      failNextTurn = true;
      console.log("The next order lookup will fail.");
      continue;
    }
    try {
      process.stdout.write("Assistant: ");
      await answer(question, failNextTurn);
    } catch (error) {
      const message = error instanceof Error ? error.message : String(error);
      console.error(`\nRequest failed: ${message}. Ask again to retry.`);
    } finally {
      failNextTurn = false;
      await Sentry.flush(2000);
    }
  }
} finally {
  terminal.close();
  await Sentry.flush(2000);
}
```

**Run**

```bash
pnpm typecheck
pnpm start
```

### 5. Read the Support Conversation

Open [Explore > Agents](https://sentry.io/orgredirect/organizations/:orgslug/explore/agents/), select your project, and find the conversation ID printed in the terminal. Choose **Transcript** to read the questions, answers, and follow-up as one conversation. The conversation ID groups related model calls even when they belong to separate traces. The basic setup is complete; the remaining steps are optional.

Screenshots show the native Mistral SDK tested through OpenRouter. The setup in this recipe uses Mistral directly.

- **Conversation:** check that the first answer uses the shipped status and Thursday delivery, and that the follow-up identifies the item as Kettle.

- **Model details:** in **Timeline**, select the first model call and open **Output**, then return to **Transcript** to keep its details beside the conversation. The sidebar shows the tool request, duration, and token counts. The second call’s **Input** contains the tool result.

- **Local lookup:** this setup has no tool execution span. The optional steps add its duration, result, and error details.

[Explore agent conversations](https://docs.sentry.io/product/agents/conversations/)

### 6. Add Agent and Tool Spans (Optional)

Use this extension when you want one trace for the whole turn and timing for your application’s lookup. Open the included `tracing.ts`, then apply the edits in the **Connect** tab. The agent span groups the turn; the tool span measures the local lookup. The two Mistral spans still come from the integration. The wrapper captures errors inside the active agent span so an issue can link to that trace.

- Type `/quit`, restart with the command from step 4, and ask about ORD-1001 again. Restarting creates a new conversation ID.

**Tracing helpers**

```typescript
import * as Sentry from "@sentry/node";
import type { ToolCall } from "@mistralai/mistralai/models/components";
import { lookupOrder as runLookupOrder } from "./orders.ts";

export function traceTurn<T>(run: () => Promise<T>): Promise<T> {
  return Sentry.startSpan(
    {
      name: "invoke_agent Order Support",
      op: "gen_ai.invoke_agent",
      attributes: {
        "gen_ai.operation.name": "invoke_agent",
        "gen_ai.agent.name": "Order Support",
      },
    },
    async () => {
      try {
        return await run();
      } catch (error) {
        Sentry.captureException(error);
        throw error;
      }
    },
  );
}

export function lookupOrder(call: ToolCall, failLookup: boolean): Promise<string> {
  return Sentry.startSpan(
    {
      name: "execute_tool lookup_order",
      op: "gen_ai.execute_tool",
      attributes: {
        "gen_ai.operation.name": "execute_tool",
        "gen_ai.tool.name": "lookup_order",
        "gen_ai.tool.call.id": call.id,
        "gen_ai.tool.call.arguments": typeof call.function.arguments === "string"
          ? call.function.arguments : JSON.stringify(call.function.arguments),
      },
    },
    async (span) => {
      try {
        const result = await runLookupOrder(call, failLookup);
        span.setAttribute("gen_ai.tool.call.result", result);
        return result;
      } catch (error) {
        span.setStatus({ code: 2, message: "internal_error" });
        span.setAttribute("error.type", error instanceof Error ? error.name : "Error");
        throw error;
      }
    },
  );
}
```

**Connect**

```typescript
// assistant.ts: replace the import from orders.ts with these two imports.
import { orderTool } from "./orders.ts";
import { lookupOrder } from "./tracing.ts";

// app.ts: add this import.
import { traceTurn } from "./tracing.ts";

// app.ts: replace await answer(question, failNextTurn) inside the try block.
await traceTurn(() => answer(question, failNextTurn));
```

[Add application context to AI traces](https://docs.sentry.io/platforms/javascript/guides/node/agent-tracing/manual-instrumentation/)

### 7. Inspect the Tool Execution (Optional)

After you add the spans in step 6, open the new conversation’s **Timeline**. Select **lookup_order** and open **Output** to inspect the returned order. The screenshot shows the local function’s duration and result alongside the automatic model calls.

- Open the new trace: invoke_agent Order Support contains two automatic model spans and one manual execute_tool lookup_order span.

- Inspect the lookup’s arguments, returned order, and roughly 250 ms duration. The manual argument/result attributes use fictional data; omit or redact them for sensitive records.

### 8. Debug a Tool Failure (Optional)

Complete step 6 first, then enter `/fail` at the **You:** prompt, then ask **Where is order ORD-1002?** The `/fail` command makes the next lookup throw. The first Mistral call should complete, but the tool fails before the second model call starts. The optional wrapper records the application error, and the terminal stays open for a retry.

- Open [Issues](https://sentry.io/orgredirect/organizations/:orgslug/issues/) and find **Order service unavailable (demo)**. Follow its trace link.

- Find the failed execute_tool lookup_order span and the stack frame in orders.ts. There should be no second model span because answer generation never started.

- Ask **Where is order ORD-1002?** again. The failure resets after one turn. The retry should include a successful lookup and a second model call that streams the answer.

- Compare the failed turn and retry in the same conversation. The lookup failed in application code; the first model call succeeded.

[Connect captured errors to traces](https://docs.sentry.io/platforms/javascript/guides/node/usage/)

## Summary

**Your Mistral Calls Are Visible.**

Inspect model inputs, tool requests, streamed replies, and usage with the native integration, then add application spans when you need to debug work between model calls.

## Pro Tips

- Use one conversation ID for follow-up questions. It groups related model calls even before you add a parent agent span.

- Start with the automatic model spans. Add manual spans where you need to measure application work or group a complete turn.

- Consume the complete response stream so the integration can record the final output and any token usage returned by Mistral.

## Common Pitfalls

- Treating a model’s tool request as proof the tool ran. Check the returned data in the next model call, or add the optional tool span to measure execution.

- Expecting prompt-recording options to control optional manual attributes. Redact tool arguments and results separately when you add them.

- Loading Sentry after Mistral, or also enabling Mistral’s OpenTelemetry instrumentation. Keep the --import startup command and avoid recording the same model call twice.

## FAQ

**Do I need manual spans for the Mistral integration to work?**

No. The integration traces Mistral SDK calls automatically, including tool-call data and streaming. The optional agent span groups an entire turn, and the optional tool span measures a function that your application executes outside the Mistral SDK.

**Do I need Vercel AI SDK or AI Gateway?**

No. This recipe calls Mistral directly through @mistralai/mistralai. For an application built with Vercel AI SDK, use Sentry’s Vercel AI integration to trace that framework.

**Can I keep prompts and responses out of Sentry?**

Yes. Set dataCollection.genAI.inputs and dataCollection.genAI.outputs to false in instrument.ts. The example also disables HTTP body and local-variable capture. If you add the optional tracing.ts module, omit or redact gen_ai.tool.call.arguments and gen_ai.tool.call.result there too. The SDK’s data collection options do not control those manual attributes. Model metadata, timing, and token usage remain available.

**Why is the estimated cost missing or zero?**

Sentry needs token counts and a model name that matches its pricing data. Newly released, custom, or unrecognized models may have no estimate. See the [model cost documentation](https://docs.sentry.io/product/agents/costs/).

**Can I use this with my real order service?**

Replace the in-memory lookup and simulated delay in orders.ts. Validate model-generated arguments and enforce the signed-in user’s access to each order before returning data. This terminal demo has no authentication and uses fictional orders only.

## Next Steps

- [Monitor Agent Spend](/cookbook/monitor-ai-agent-spend-with-dashboards-and-alerts/): Compare token usage and estimated costs across models and conversations.

- [Trace an Eve Agent](/cookbook/send-eve-agent-traces-to-sentry/): Send model and tool traces from an Eve application to Sentry.

---

*Source: [sentry.io/cookbook/monitor-mistral-with-sentry/](https://sentry.io/cookbook/monitor-mistral-with-sentry/)*
