Set up
Wrap an instance ofMistral from @mistralai/mistralai. The SDK recognizes it automatically.
- Standalone functions. Every method also exists as a function that takes the client as an argument, such as
chatComplete(client, request)from@mistralai/mistralai/funcs/. These do not go through the wrapped client. Call the client’s methods instead. - Streams read with
getReader(). Iterate a stream withfor await, as above, so the SDK can meter it.
OpenAI client whose baseURL is https://api.mistral.ai/v1 instead. Calls are then reported under openai.* event names, with the same metric names as below. See OpenAI-compatible endpoints.
What is reported
Every response from the Mistral API includes ausage object with the number of tokens the call consumed. The wrapper reads it and reports it to Stigg as the metrics below. For details, see Mistral’s API reference.
Each method has its own event name, so a meter that filters on one does not count the others. To meter several together, filter on each name, or set one
eventName for them.
The
Mistral client returns usage in camelCase, such as promptTokens. The SDK reports it under the field names of Mistral’s API, such as prompt_tokens, so the metrics are the same whether you use the Mistral client or the OpenAI client. total_tokens includes the other two, so price the total or the parts, never both.
Other Mistral APIs, such as conversations (beta.conversations), embeddings, OCR and audio, measure usage differently and are not metered. To meter one of them, write your own recipe.
Every usage field the vendor returns is also included, with a raw. prefix. See How usage is reported.