Create a minimal working Mistral AI chat completion example.
Use when starting a new Mistral integration, testing your setup,
or learning basic Mistral API patterns.
Trigger with phrases like "mistral hello world", "mistral example",
"mistral quick start", "simple mistral code", "mistral chat".
Minimal working examples demonstrating Mistral AI chat completions, streaming, multi-turn conversation, and JSON mode. Uses the official @mistralai/mistralai TypeScript SDK and mistralai Python SDK.
Prerequisites
Completed mistral-install-auth setup
Valid MISTRAL_API_KEY environment variable set
Node.js 18+ or Python 3.9+
Instructions
Step 1: Basic Chat Completion
TypeScript (hello-mistral.ts)
import { Mistral } from '@mistralai/mistralai';
const client = new Mistral({ apiKey: process.env.MISTRAL_API_KEY });
async function main() {
const response = await client.chat.complete({
model: 'mistral-small-latest',
messages: [
{ role: 'user', content: 'Say "Hello, World!" in a creative way.' },
],
});
console.log(response.choices?.[0]?.message?.content);
console.log('Tokens used:', response.usage);
}
main().catch(console.error);
Python (hello_mistral.py)
import os
from mistralai import Mistral
client = Mistral(api_key=os.environ["MISTRAL_API_KEY"])
response = client.chat.complete(
model="mistral-small-latest",
messages=[
{"role": "user", "content": "Say 'Hello, World!' in a creative way."}
],
)
print(response.choices[0].message.content)
print(f"Tokens: {response.usage}")
Streaming delivers the first token in ~200ms instead of waiting 1-2s for the full response.
TypeScript
import { Mistral } from '@mistralai/mistralai';
const client = new Mistral({ apiKey: process.env.MISTRAL_API_KEY });
async function streamChat() {
const stream = await client.chat.stream({
model: 'mistral-small-latest',
messages: [
{ role: 'user', content: 'Tell me a short story about AI.' },
],
});
for await (const event of stream) {
const content = event.data?.choices?.[0]?.delta?.content;
if (content) process.stdout.write(content);
}
console.log(); // newline
}
streamChat().catch(console.error);
Python
stream = client.chat.stream(
model="mistral-small-latest",
messages=[{"role": "user", "content": "Tell me a short story about AI."}],
)
for event in stream:
content = event.data.choices[0].delta.content
if content:
print(content, end="", flush=True)
print()