Mistral API Integration Guide

Moving a Mistral-style integration to a new base URL takes less work than most teams expect. This guide covers the base URL change, the model id, streaming, tools, token limits, retries and the errors you are most likely to hit, with code you can run as it is.

Last updated

Summary

  • The change is one base URL and one key; the request and response format stays the same.
  • Use the model id uncensored and check it with a single test request before you change anything else.
  • Streaming and function calling work through the same chat completions endpoint, so existing parsers keep working.
  • Costs are $0.15 per 1M input tokens and $0.50 per 1M output tokens, billed from prepaid credit.

Understanding the Base URL Change

Your code sends requests to a base URL and attaches a key. To move to another OpenAI-compatible endpoint, you change those two values and nothing else. The path /v1/chat/completions stays the same, so the request body and the response body keep their shape.

Keep the old base URL in a config value rather than in the code. That way you can switch back in one step if a problem shows up in production.

SDK Compatibility: OpenAI vs Mistral Clients

Most client libraries accept a custom base URL. The official OpenAI SDK, LangChain and LlamaIndex all do, and a plain HTTP client works as well. Set the base URL to https://mistralapibaseurl.com/v1 and pass the new key.

  • Check the SDK version, because very old releases ignore the base URL setting.
  • Check that the client sends the key in the Authorization header as a bearer token.

Configuring the Model ID

Send the model id uncensored. It is the only model the service offers, so the id does not change between requests and you do not need model selection logic.

Run one request with a short prompt. A reply and a usage block mean the base URL, the key and the model id are all correct.

Implementing Streaming Responses

Set stream: true to receive the answer as server-sent events. Each event carries a small piece of text, and the final event carries the usage numbers.

Show the chunks as they arrive and keep the full text in a buffer, so the message is stored once the stream ends. Measure the time to the first token; that figure shows how responsive the chat feels to a user.

Enabling Function Calling and Tools

Pass the tools array in the same format you already use. The model returns a tool call with a function name and JSON arguments. Run the function in your code, send the result back as a tool message and read the final answer.

  1. Define one tool with two required arguments and test it alone.
  2. Test a case where two tool calls are needed in sequence.
  3. Validate the arguments before you run anything that changes data.

Managing Context Windows and Tokens

The context window is 100,000 tokens. Long chats fill it, so trim old turns or summarise them before the limit is reached. Each request can return up to 16,000 output tokens, and the default is 2,048, so set max_tokens for the answers you expect.

Count tokens from the usage field of each response. Estimates from word counts are often off by a wide margin for code and non-English text.

Handling Rate Limits and Retries

Each key allows 300 requests per minute and 8 requests at the same time. When you go over the limit you get a 429 response. Retry only the 429 and the 5xx responses, with exponential backoff and a cap on the total wait.

Never retry a 400 or a 401, because the same request fails the same way again. Fix the request or the key first.

Debugging Common API Errors

StatusUsual causeWhat to do
400Malformed JSON or an unknown fieldCheck the request body against the format you send
401Missing or wrong keyCopy the key again and check the Authorization header
402Credit used upTop up the balance; top-ups start at $20
429Rate limit reachedBack off and retry later

Log the status code and the request id for each failure, so you can trace a problem quickly.

Migrating from Competitor APIs

Plan the move in three stages: a test environment, a small share of live traffic, and then the full switch. Keep both base URLs in configuration during the middle stage and compare the answers and the latency on the same prompts.

Compare cost on your own usage, not on list prices. Multiply the input and output tokens from the usage field by $0.15 and $0.50 per 1M tokens and set that against your current bill.

Common questions

Do I need to change my request format?

No. The endpoint follows the chat completions format, so the same message and tool structures work. Only the base URL and the key change.

Which model id should I use?

Use uncensored. It is the only model on the service, so the id stays the same in every request.

How do I get a key to test with?

Sign up with Google or with an email and a password. The key appears on screen, and the account starts with $0.50 of trial credit valid for 7 days. No card is needed.

How do I pay for usage?

Top up with USDT on TRC20 or USDC on Base, starting at $20. Bonus credit applies from $50, and the credit does not expire.

Access your key

Sign up, retrieve your key, and update your base URL. That’s the entire process.

Start now